I'm making a custom button that is made up of two other buttons. It has different behavior from a normal button (hence why it's custom) and looks like this
I would like to change the way icons are set in my button so that they apply to the button on the right (the one with the square). Currently, I am overriding AbstractButton.getIcon and AbstractButton.setIcon so that they apply to the right button, rather than the whole button. So my code looks like this
/* (non-Javadoc)
* @see javax.swing.AbstractButton#getIcon()
*/
@Override
public Icon getIcon() {
return popupButton.getIcon(); // popupButton = the right button
}
/* (non-Javadoc)
* @see javax.swing.AbstractButton#setIcon(javax.swing.Icon)
*/
@Override
public void setIcon(Icon icon) {
Icon oldValue = getIcon();
firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, icon);
popupButton.setIcon(icon);
}
When I try to set my button icons, the button ends up looking like this
where the icon is duplicated in the middle of the whole button. I tried removing firePropertyChange, but that didn't work.
Also here is the code I used to produce the two screenshots
public class MyButtonDemo implements Runnable {
public static class DemoPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DemoPanel() {
MyButton myButton = new MyButton("My Button");
Icon icon = new ImageIcon(new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB));
myButton.setIcon(icon);
add(myButton);
add(new JButton("Normal Button"));
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
try {
UIManager.setLookAndFeel(lafs[2].getClassName());
} catch (Exception e) { }
JFrame frame = new JFrame();
frame.setContentPane(new DemoPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MyButtonDemo());
}
}
What is making my button display the icon for the whole button instead of just the right button? And how do I work around it?