'setDefaultButton not working as expected

public JoinChatClient(String serverAddress, String chatName)
    {
        chatWindow.getContentPane().add(sendButton, "South");
        chatWindow.getContentPane().add(splitPane, "Center");
        chatWindow.setSize(800,500);
        sendButton.addActionListener(this);
        chatWindow.setTitle("Chat Room");
        chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        splitPane.setDividerLocation(350);
        sendButton.setBackground(Color.gray);
        sendButton.setForeground(Color.red);
        outChatTextArea.setEditable(false);
        inChatTextArea.setFont (new Font("default",Font.ITALIC,20));
        outChatTextArea.setFont(new Font("default",Font.BOLD,20));
        inChatTextArea.setLineWrap(true);
        outChatTextArea.setLineWrap(true);
        inChatTextArea.setWrapStyleWord(true);
        outChatTextArea.setWrapStyleWord(true);
        inChatTextArea.setText("Enter text to be sent here.");
        outChatTextArea.setText("You can move the separator bar!");
        inChatTextArea.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                if(inChatTextArea.getText().equals("Enter text to be sent here."))
                {
                    inChatTextArea.setText("");
                    inChatTextArea.setFont(new Font("default",Font.BOLD,20));
                }
            }
            public void focusLost(FocusEvent e) {
                if(inChatTextArea.getText().isEmpty())
                {
                    inChatTextArea.setFont (new Font("default",Font.ITALIC,20));
                    inChatTextArea.setText("Enter text to be sent here.");
                }
            }
        });
        chatWindow.getRootPane().setDefaultButton(sendButton);
        chatWindow.setVisible(true);
    }

I've looked over all the threads I could find concerning this, and I cannot figure out why hitting ENTER doesn't activate the actionPerformed method attached to sendButton. Is it because the text field has a FocusListener?

Things I've tried:

  • changing the statement to target the specific text field (inChatTextArea)
  • moved the setVisible statement to the end
  • targeted different parts of the GUI when hitting enter

Bear in mind I've only included the code that builds the GUI in an attempt to waste less of your time.

What I want: Ideally, I want to keep my FocusListener (or something like it) so that I can display the "text field hint." I would like to be able to hit ENTER to send the user's text while the inChatTextArea field is focused.



Solution 1:[1]

If a component on the JFrame has focus, and can accept an enter key press, such as one of the JTextAreas, then the enter presses will go to that component and not to the default button. For the default button to work, then the JFrame or the button or some other component that does not accept enter key presses, needs to have focus. I'm guessing that one of your JTextAreas has stolen the focus, and that this is messing you up.

Solution 2:[2]

This question is old, but I found it when having the same issue. So I hope others might find it useful.

I figured out that getRootPane() will return null if the component is trying to access the root pane too early, e.g. under construction of the component.

Hence, I propose to use SwingUtilities.invoke(Runnable) to postpone setting the default button on the root pane, and also to request the focus to the button.

So this method could be a helper method on a class to extend from:

    protected void setDefaultButton(JButton button) {
        // Uses invoke later, as getRootPane() might return null if the method is called under construction
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JRootPane rootPane = getRootPane();
                if (rootPane != null) {
                    rootPane.setDefaultButton(button);
                }
                button.requestFocus(); // set the focus on the button
            }
        });
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hovercraft Full Of Eels
Solution 2 FNL