In this image taken from eclipse, "Build Automatically" shows a tick开发者_如何转开发 mark without a checkbox. How can I create a similar effect to this with Java JCheckBoxMenuItems, where only the tickmark and not the checkbox are visible?
JCheckBoxMenuItem extends from AbstractButton so you should be able to provide custom Icons for the appropriate set???Icon methods.
The appearance is defined by BasicMenuItemUI
, typically unique to each Look & Feel. You can supply your own variation that overrides paintMenuItem()
. As doing so will violate the user's preferred Look & Feel, you'll have to decide if it's worth the effort.
Addendum: @camickr's Icon
idea is more elegant, but you can always put a ✔ (U+2714) in the menu's text.
you can extend JCheckbox and override paintComponent()
You can do it with JMenuItem and an ActionListener:
JMenuItem jcmi1 = new JMenuItem(" Choix 1");
jcmi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (jcmi1.getText()!="\u2714 Choix 1"){
jcmi1.setText("\u2714 Choix 1");
}else{
jcmi1.setText(" Choix 1");
}
}
});
精彩评论