Menu item that can be selected or deselected
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.border.EtchedBorder;
public class CheckMenuItem {
public static void main(String[] args) {
final JLabel statusbar = new JLabel(" Statusbar");
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenu view = new JMenu("View");
view.setMnemonic(KeyEvent.VK_V);
JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar");
sbar.setState(true);
sbar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (statusbar.isVisible()) {
statusbar.setVisible(false);
} else {
statusbar.setVisible(true);
}
}
});
view.add(sbar);
menubar.add(file);
menubar.add(view);
JFrame f = new JFrame();
f.setJMenuBar(menubar);
statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
f.add(statusbar, BorderLayout.SOUTH);
f.setSize(360, 250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Related examples in the same category