javaswingjbuttonborder-layout

JButton text changes on its own?


The following simple code:

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class ButtonTextMain {
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            
            final JTextField field = new JTextField(20);
            
            final JButton button = new JButton("Click to change text");
            button.addActionListener(e -> button.setText(field.getText()));
            
            final JPanel panel = new JPanel(new BorderLayout());
            panel.add(field, BorderLayout.CENTER);
            panel.add(button, BorderLayout.PAGE_END);
            
            final JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

can always reproduce the same bug (at least for my setups, which are given below).

The program is supposed to change the button text, according to the text-field's text, when the button is clicked.

The problem is that the text of the button reverts/changes back to its previous value on its own unexpectedly.

The problem arises when I use the Tab to alternate/navigate between the button and the text-field. Here are the specific steps which always reproduce the same bug on my setups:

  1. Run the program.
  2. Resize the frame to be a bit bigger than it was.
  3. Type some short text in the text-field.
  4. Press Tab to navigate the focus to the button.
  5. Press Spacebar to invoke the button.
  6. Press Tab to navigate the focus back to the text-field.
  7. Type any letter you like into the text-field.

Notes:

My first tested setup:

My second tested setup:

The operating system is Windows 10 on both setups.

So the question is: did I do something wrong, and if so, what is it please? First of, can anybody else reproduce the bug I am getting on their setup?

Update:

I can also reproduce this behaviour on the system L&F too, by using:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

inside just before the JTextField creation (and catching exceptions, etc).


Solution

  • Based on the above comments I retested and now I am able to reproduce the problem. The issue occurs when you increase the vertical height of the text field (by some minimal amount).

    I guess I tested before by only increasing the horizontal size of the text field.

    I found a simple work around:

    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class ButtonTextMain {
        public static void main(final String[] args) {
            SwingUtilities.invokeLater(() -> {
    
                final JTextField field = new JTextField(20);
    
                final JButton button = new JButton("Click to change text");
                button.addActionListener(e ->
                {
                    button.setText(field.getText());
                    //button.getParent().revalidate();
                    button.getParent().repaint();
                });
    
                final JPanel panel = new JPanel(new BorderLayout());
                panel.add(field, BorderLayout.CENTER);
                panel.add(button, BorderLayout.PAGE_END);
    
                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        }
    }
    

    This definitely seems like a bug to me.

    Having said that, rarely would you want the vertical height of the text field to increase in size. So maybe you can find a layout manager that only affects the horizontal size and not the vertical height?