JTextField focusing issue

(The problem only occurs in Ubuntu. It works very well on Windows. I do not know in other Linux environments)

I used the ComponentListener approach to call the focus in the JTextField in the dialog box, but for this case it just doesn’t work, I don’t know why. It shows focus in the text box and quick button changes. Run and see:

import java.awt.Component; import java.awt.GridLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class User { private String username = ""; private String password = ""; public User() { // default constructor } public User(String username, String password) { this.username = username; this.password = password; } /** Create a panel containing the componet and tha label. */ public JPanel createLabeledComponent(JLabel label, Component comp) { GridLayout layout = new GridLayout(2, 1); JPanel panel = new JPanel(layout); panel.add(label); panel.add(comp); label.setLabelFor(comp); return panel; } public void showEditDialog() { JLabel usernameLbl = new JLabel(username); final JTextField usernameField = new JTextField(); usernameField.setText(username); JPanel usernamePnl = createLabeledComponent(usernameLbl, usernameField); JLabel passwordLbl = new JLabel(password); JPasswordField passwordField = new JPasswordField(password); JPanel passwordPnl = createLabeledComponent(passwordLbl, passwordField); Object[] fields = { "User:", usernamePnl, "Password:", passwordPnl }; JOptionPane optionPane = new JOptionPane(fields, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null); JDialog dialog = optionPane.createDialog("User Data"); dialog.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent e) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { usernameField.requestFocusInWindow(); } }); } }); dialog.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new User().showEditDialog(); } }); } } 

Any idea how to solve this?

- update

Now everything works on EDT. Unfortunately, with the same behavior.

By the way, using the last argument (Object initialValue) of the JOptionPane constructor does not work.

+1
java ubuntu dialog focus
source share
3 answers

I remember that I had a similar problem, I used the solution found at the bottom of this page:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574

+1
source share

Perhaps this Dialog Focus solution will work on Ubuntu (I can't test it).

It shows focus in the text box and quick button changes.

Or you can try wrapping the call to requestFocusInWindow () method in SwingUtilities.invokeLater () to place your requrest at the end of the EDT.

0
source share

From How to Use Focus Subsytem :

Exactly how the window gains focus depends on the window system. On all platforms, there is no reliable way to make sure that the window receives focus.

Also from Component.requestFocusInWindow :

Every effort will be made to fulfill the request; however, in some cases this cannot be done. Developers should never assume that this component is the focus owner until this component receives the FOCUS_GAINED event.

Your components cannot be implemented before you call requestFocusInWindow . Have you tried putting dialog.pack(); before setVisible(true) ?

0
source share

All Articles