Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By newbie123

Thread: textfields - cursor

  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Question textfields - cursor

    I have a very simple problem, but I can't seem to find a solution.

    I have a textfield. I want the cursor to be in the textfield (so the user doesn't have to click on the textfield to enter text).

    This has to be doable.
    dulithchinthaka likes this.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    Java Code:
    requestFocusInWindow()
    db

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default thanks

    Thanks for your help, Darryl. I still have not gotten it to work with your suggestion, but I think the problem lies with the fact that the component in question is in a JPanel (that itself might not have focus).

    I'll let you knwo when it is working.

  4. #4
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: textfields - cursor

    O.K. I have code that does what I want it to, but I'd like to make sure that I understand why it works.

    <code>

    import java.awt.*; import java.awt.event.*;
    import javax.swing.*; import javax.swing.event.*;

    class Test extends JFrame {

    String problemType = "Focus Issue";

    Test() {

    JFrame frame = new JFrame( problemType );
    frame.setSize( 200, 100 );
    frame.getContentPane().setLayout( new FlowLayout() );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    JTextField textField = new JTextField( 5 );
    textField.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent event ) {
    }
    });

    final JButton button = new JButton( "Button" );
    button.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent event ) {
    }
    });

    //frame.getContentPane().add( textField );
    //frame.getContentPane().add( button );

    JPanel panel = new JPanel();
    panel.setLayout( new FlowLayout() );
    panel.add( textField );
    panel.add( button );
    frame.getContentPane().add( panel );
    //button.requestFocusInWindow();

    frame.addWindowFocusListener(new WindowAdapter() {
    public void windowGainedFocus(WindowEvent e) {
    button.requestFocusInWindow();
    }
    });

    frame.setVisible(true);
    }


    public static void main( String[] args ) {
    SwingUtilities.invokeLater( new Runnable() {
    public void run() { new Test(); }
    });
    }
    }
    </code>

    1. If I leave out the block that adds the focus listener to the frame, focus is not given to the button. Is this because the frame is not conscious of focus until it is told to look for it?

    2. Is the reason I can't have the panel deal with focus is because it doesn't inherit from Window (where addWindowFocusListener() is defined)?

    Any clarity would be greatly appreciated.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: textfields - cursor

    The key is that you can't set focus on a component until the GUI is visible. The simple solution is:
    Java Code:
    frame.setVisible(true);
    button.requestFocusInWindow();
    Also, use the square brackets ([])to wrap the code tags.

  6. #6
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: textfields - cursor

    Thanks for the clarification, Camickr. That makes sense.

  7. #7
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: textfields - cursor

    I can't get over the fact that my solution seems to be a total hack. I REALLY don't understand how this focus stuff works. I have read and reread the tutorial at the Oracle website, and it is not too helpful. There is also essentially no discussion of focus in the "Core Java" books.

    Can someone recommend a resource that will explain this area?

Similar Threads

  1. applet and textfields
    By poajavaweb in forum Java Applets
    Replies: 4
    Last Post: 01-28-2011, 08:56 PM
  2. adding textfields
    By themanepalli in forum Java Applets
    Replies: 1
    Last Post: 12-28-2010, 03:56 AM
  3. change the mouse cursor - crosshair cursor
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 09-28-2009, 01:57 AM
  4. Buttons and TextFields
    By Xystus777 in forum New To Java
    Replies: 27
    Last Post: 03-17-2009, 01:26 AM
  5. information from a textfields
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-28-2007, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •