Skip to content
Advertisement

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:

  • I know that step 2 is relevant, because if I ommit it then the bug does not reproduce.
  • After step 2 (and before 3), the mouse should not be needed any more. Leave it. The focus of the program should be in the text-field as it was when the program launched.
  • In step 3 I usually type something like abc but the error repoduces for any other input I tried.
  • In step 6 you can also use Shift+Tab to navigate back to the text-field.
  • On step 7, after typing the first letter, you will see that the button’s text changes back to its initial/previous value.

My first tested setup:

  • Apache NetBeans IDE 8.2, which is a bit outdated.

  • java -version yields:

    java version "1.8.0_321"
    Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
    Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
    
  • javac -version yields javac 1.8.0_161. There is a missmatch here with the runtime environment.

My second tested setup:

  • Apache NetBeans IDE 11.0.

  • java -version yields:

    java version "12.0.2" 2019-07-16
    Java(TM) SE Runtime Environment (build 12.0.2+10)
    Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)
    
  • javac -version yields javac 12.0.2.

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).

Advertisement

Answer

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?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement