Results 1 to 1 of 1
  1. #1
    jwxie is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default can i use requestFocus

    I am given an assignment but I am totally new to Java (i have been programming in C++ and Python for two years).

    So we are doing GUI and basically we extended JFrame and added a couple fields.

    Say we have a field named "Text 1" and "Text 2". When user press enter with the cursor in Text 1, move the focus to Text 2. I tried to add

    Java Code:
    	private JTextField textfield1() {
    		textfield1 = new JTextField();
    		textfield1.setPreferredSize(new Dimension(200, 20));
    		textfield1.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						textfield1text = textfield1.getText().trim();
    						textfield1.setText(textfield1text);
    						System.out.println(textfield1text);
    
    						textfield1.requestFocus();
    					}
    				});
    return textfield1;
    But that doesn't work at all.
    I noticed that requestFocus is not recommended, and instead one should use requestFocusWindows. But I tried that too. Upon some readings it seems like I have to do keyboard action & listener??? But my teacher said it only requires 1 line....

    ANy help is appreciated.
    Last edited by jwxie; 02-09-2011 at 06:26 AM.

Similar Threads

  1. RequestFocus on DefaultTableModel Particular Cell
    By yernikumar in forum AWT / Swing
    Replies: 1
    Last Post: 03-01-2009, 04:58 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
  •