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:
Notes:
abc
but the error repoduces for any other input I tried.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?
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).
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?