Name: dc32491 Date: 07/27/2001
BOTH:
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
AND
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
It seems that the very first time an icon is disabled on a JButton, that icon
is cached. If the JButton's icon is later changed, and the button is disabled
again, the first disabled icon is used instead of a disabled version of the
current icon. Bug# 4117779 fixed this for JLabel but not JButton. The
following code demonstrates the problem (substitute .gif files for your own
icons):
==========================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DisabledIconTest {
public static void main(String[] args) {
final ImageIcon icon1 = new ImageIcon
("C:/Java/Classes/VerticalRight.gif");
final ImageIcon icon2 = new ImageIcon
("C:/Java/Classes/VerticalLeft.gif");
final ImageIcon icon3 = new ImageIcon
("C:/Java/Classes/VerticalCenter.gif");
final JLabel iconLabel = new JLabel(icon1);
final JButton iconButton = new JButton(icon1);
final JButton enableButton = new JButton("Enable/Disable");
enableButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
iconButton.setEnabled(!iconButton.isEnabled());
iconLabel.setEnabled(!iconLabel.isEnabled());
}
});
final JButton switchButton = new JButton("Switch Icon");
switchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
if (iconButton.getIcon() == icon1) {
iconButton.setIcon(icon2);
iconLabel.setIcon(icon2);
return;
}
if (iconButton.getIcon() == icon2) {
iconButton.setIcon(icon3);
iconLabel.setIcon(icon3);
return;
}
if (iconButton.getIcon() == icon3) {
iconButton.setIcon(icon1);
iconLabel.setIcon(icon1);
return;
}
}
});
JFrame f = new JFrame("DisabledIconTest");
f.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,
5, 5));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(iconLabel);
f.getContentPane().add(iconButton);
f.getContentPane().add(enableButton);
f.getContentPane().add(switchButton);
f.pack();
f.show();
}
}
==========================================
Steps to reproduce:
1. Run the program, note the icon on both the label and the button.
2. Press the Switch Icon button - the icon changes on both the label and the
button.
3. Press the Enable/Disable button - both label and button are disabled using
the correct icon.
4. Press the Enable/Disable button again to enable the label and button.
5. Press the Switch Icon button.
6. Press the Enable/Disable button. The label is disabled with the current
icon, but the button is disabled with the same icon from step 3.
This bug is present in both 1.3.1 and the Merlin beta.
This could happen on JMenuItems as well I supppose (I haven't tested it)...
(Review ID: 127496)
======================================================================