I\'m working on my personal Java chat client whose one feature is setting user\'s status (Available, Invisible, Busy). To make it user-friendly, I put those statuses into a
I suggest you use the Icon property of Swing Components. Here is example which sets an icon to the JRadioButtonMenuItem. Whenever there is a change in status use seticon method to change the icon. Instead of colors use icons
Here's an Example http://www.java2s.com/Code/Java/Swing-JFC/Anexampleofradiobuttonmenuitemsinaction.htm
My thought was, the behavior of the JRadioButtonMenuItem is fine, it's just its painting is a little goofy. But I don't want to have to extend it or override paint or any of those shenanigans. So I concluded, just steal its behavior and leave its painting behind.
You'll notice that ButtonGroup
accepts AbstractButton
s, so I like dungeon Hunter's solution: use regular JMenuItem
s. I'd append to that, steal the JToggleButton
's ButtonModel
and send in an ItemListener
that will do the image swapping (as Ramesh John suggested).
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToggleButton;
public class TestRadioCustomIcon {
public static void main(String[] args){
new TestRadioCustomIcon().go();
}
private void go(){
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar jmb = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem i1 = makeFauxRadioMenuItem("Item 1");
JMenuItem i2= makeFauxRadioMenuItem("Item 2");
JMenuItem i3= makeFauxRadioMenuItem("Item 3");
i1.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(i1);
bg.add(i2);
bg.add(i3);
menu.add(i1);
menu.add(i2);
menu.add(i3);
jmb.add(menu);
frame.add(jmb);
frame.pack();
frame.setVisible(true);
}
private ImageIcon selected = loadImage("C:\\path\\to\\image1.bmp");
private ImageIcon deselected = loadImage("C:\\path\\to\\image2.bmp");
private ItemListener il = new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e){
AbstractButton ab = (AbstractButton) e.getSource();
switch(e.getStateChange()){
case ItemEvent.SELECTED:
ab.setIcon(selected);
break;
case ItemEvent.DESELECTED:
ab.setIcon(deselected);
break;
}
}
}
private ImageIcon loadImage(String filePath){
try{
BufferedImage bi = ImageIO.read(new File(filePath));
return new ImageIcon(bi);
} catch (IOException e){
//sad day
return null;
}
}
private JMenuItem makeFauxRadioMenuItem(String label){
JMenuItem item = new JMenuItem(label);
//Make it think it's a RadioButton
item.setModel(new JToggleButton.ToggleButtonModel());
//When selection changes occur, swap icons
item.addItemListener(il);
//Assume deselected
item.setIcon(deselected);
return item;
}
}
And this'll work for any old icon, not just changing the button's color.
Add a new class to customize the RadioButtonMenuItem
by extending JRadioButtonMenuItem
.
Add inner class to update status Icon by implementing Icon inteface. Override all the methods update the paintIcon()
method with current status icon.
Call super class by passing status icon.
super(theMenuText, new StatusIcon(theStudyIcon, getUserStatus(UserId)));
Add listner to get the latest event to update the Icon using setIcon()
method.
The radio button's dot is rendered by the UI delegate for each Look & Feel. You can supply your own BasicRadioButtonUI, but the effort is not trivial. As an alternative, implement the Icon
interface, as shown here in ColorIcon
.