Focus on Java Text Box

Hi, I have a focus problem.

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 automatically select text in the text box so that the text is marked?

+2
source share
2 answers

From Swing Tutorial

If you want to make sure that a particular component receives focus when you first turn on the window, you can call the requestFocusInWindow method on the component after the component is implemented, but before the frame is displayed. The following code example shows how this operation can be performed:

//...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.
+6
source

Regarding the selection of all the text you should use ...

mytext.selectAll();

, , requestFocus , jContentPane.

+5

Source: https://habr.com/ru/post/1742730/


All Articles