Set focus to a text field

publicvoid addEmployees (int id,int sort,int roster)

{

String name =null;

String position =null;

switch (roster)

{

case 1:

name = addName1.getText ();

position = (String) addPosition1.getSelectedItem ();

break;

case 2:

name = addName2.getText ();

position = (String) addPosition2.getSelectedItem ();

break;

}

al.add ("INSERT INTO employees (manager, name, position, sort) VALUES (" + id +", '" + name +"', '" + position +"', '" + sort +"')");

switch (roster)

{

case 1:

employeeAddRemoveLabel1.setText (name +" has been added to the roster.");

addName1.setText("");

addName1.requestFocus ();

employeeAddRemoveLabel1.setForeground (Color.BLUE);

break;

case 2:

employeeAddRemoveLabel2.setText (name +" has been added to the roster.");

addName2.setText("");

addName2.requestFocus ();

employeeAddRemoveLabel2.setForeground (Color.BLUE);

break;

}

System.out.println (al +"\n");

System.out.println ("The size of the array list is " + al.size ());

updateEmployees (roster,name,true);

}

I haveaddName*.requestFocus (); so why isn't that JTextField being focused when the method is called? Basically the user clicks a JButton that calls the above method to add a user that was entered in a JTextField. It's supposed to then clear the JTextField and set focus to it so you can type in another user without having to click on the text field to give it focus again.

[2622 byte] By [tristanlee85a] at [2007-11-27 9:41:50]
# 1

Swing related questions should be posted in the Swing forum.

> why isn't that JTextField being focused when the method is called?

Code seems reasonable, so the problem must be for some other part of your code.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 2
Is this a Swing question or a JDBC question, because they are so mixed together in your code :-P
BigDaddyLoveHandlesa at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 3
> Is this a Swing question or a JDBC question, because> they are so mixed together in your code :-PIt's a question about model view controller :-)
_helloWorld_a at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 4
This would be a Swing question. The SQL stuff you see is me storing different SQL queries into an ArrayList to be executed later.Message was edited by: tristanlee85
tristanlee85a at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 5
Maybe you're doing something after that's requesting the focus.Or, if it's so important, you can use SwingUtilities.invokeLater()to request the focus.
dragzula at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 6
It really isn't that important. Just a convenience thing.
tristanlee85a at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...
# 7
While where talking about mixing levels, right now my officemate is having to deal with some contractor crud including stored database procedures that generate HTML. You can't make this kind of stuff up.
BigDaddyLoveHandlesa at 2007-7-12 23:21:35 > top of Java-index,Java Essentials,Java Programming...