• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I change the button's icon when it's disenabled?

 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a button is enabled, I can call myButton.setIcon(new ImageIcon(imgfileURL)); to change the button's icon and it works very well. But when the button is disenabled, I can't change the icon. I tryed to call myButton.setEnabled(true), then myButton.setIcon(imgfileURL), then myButton.setEnabled(false). It didn't work. I really appreciate if someone could tell me how to do it.
 
Jeff Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're trying to set the icon that shows while the button is disabled you need to call myButton.setDisabledIcon( disabledIcon ).
Jeff
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff for you reply.
My problem is that myButton.setDisabledIcon(disabled icon) works only when myButton is enabled. If you call myButton.setEnabled(false) first, then you call myButton.setDisabledIcon(disabledIcon) or myButton.setIcon(icon) it doesn't work any more. After a button is set to disabled, I can't change any icon of the button.
I think maybe I can use a cardLayout to solve the problem. Instead of change a disabled button's icon, I will use a lable instead. Here is some test code to help me explain the problem.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxDemo extends JPanel {
JCheckBox box1;
JCheckBox box2;
JCheckBox box3;
public JButton testButton = new JButton();
public static final String IMAGEPATH = "//YourImageDirector/";
public CheckBoxDemo() {
// Create the check boxes
box1 = new JCheckBox("One");
box2 = new JCheckBox("Two");
box3 = new JCheckBox("Hair");

// Register a listener for the check boxes.
CheckBoxListener myListener = new CheckBoxListener();
box1.addItemListener(myListener);
box2.addItemListener(myListener);
box3.addItemListener(myListener);
// Put the check boxes in a column in a panel
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0, 1));
checkPanel.add(box1);
checkPanel.add(box2);
checkPanel.add(box3);
setLayout(new BorderLayout());
add(checkPanel, BorderLayout.WEST);
//test1: set testButton enabled(true), everything works fine.
testButton.setEnabled(true);
//test2: set testButton enabled(false), can't change the imageIcon
//testButton.setEnabled(false);
testButton.setIcon(new ImageIcon(IMAGEPATH + "test1.gif"));
add(testButton, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

/** Listens to the check boxes. */
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
if (source == box1) {
testButton.setIcon(new ImageIcon(IMAGEPATH + "test1.gif"));
} else if (source == box2) {
testButton.setIcon(new ImageIcon(IMAGEPATH + "test2.gif"));
} else if (source == box3) {
testButton.setIcon(new ImageIcon(IMAGEPATH + "test3.gif"));
testButton.setDisabledIcon(new ImageIcon(IMAGEPATH + "test3.gif"));
}
}
}
public static void main(String s[]) {
JFrame frame = new JFrame("CheckBoxDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new CheckBoxDemo());
frame.pack();
frame.setVisible(true);
}
}
Thanks again and you have a nice weekend.

Renee
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!