tags:

views:

215

answers:

2

hello i have a problem with the focus

mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
gbl.setConstraints(mytext,gc);
jContentPane.add(mytext);

i tried

mytext.requestFocus();

too

and how can i auto-select the text in the textfield so the text is marked?

thanks

+3  A: 

As for selecting all the text you should use...

mytext.selectAll();

As for getting focus, maybe you should try the requestFocus function after everything has been added to jContentPane.

Pace
There is also `select(int,int)` for more fine-grained control: http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#select(int,%20int)
McDowell
+4  A: 

From the Swing Tutorial

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:

//...Where initialization occurs...
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new BorderLayout());

//...Create a variety of components here...

//Create the component that will have the initial focus.
JButton button = new JButton("I am first");
panel.add(button);
frame.getContentPane().add(panel);  //Add it to the panel

frame.pack();  //Realize the components.
//This button will have the initial focus.
button.requestFocusInWindow(); 
frame.setVisible(true); //Display the window.
John
very fine, thank you
Tyzak