I have a JDialog
with some JTextfields
, and I want to remove the focus from the first textfield when I open the Dialog.
I tried .removeFocus()
, but then I can't use focus anymore. I just want to remove it, so when I open the dialog no textfield is selected.
From How to use the focus subsystem we get the following:
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.