Monday, August 26, 2013


JCheckBoxMenuItem component can used to implement check boxes as the menu items. You can select many checkbox menu items. When you select a check box, menu item displays a check mark. Deselecting the item clears the check mark. Like a menu item, checkbox menu item can display text, icon, and both. Below are constructors that can be used to create a checkbox menu item.

creates a checkbox menu item without text or icon.
-JCheckBoxMenuItem(Action action)
creates a checkbox menu item that its properties are taken from the Action object.
-JCheckBoxMenuItem(Icon icon)
creates a checkbox menu item with the specified icon.
-JCheckBoxMenuItem(String text)
creates a checkbox menu item with the specified text.
-JCheckBoxMenuItem(String text,boolean selected)
creates a checkbox menu item with the specified text and selection state.
-JCheckBoxMenuItem(String text,Icon icon)
creates a checkbox menu item with the specified text and icon.
-JCheckBoxMenuItem(String text,Icon icon,boolean selected)
creates check menu item with the specified text, icon, and selection state.

In the example program below, a menu bar is created using the JMenuBar class. On this menu bar, we place a menu called Format. In the menu Format, there is one checkbox menu item. When the user select this checkbox menu item, the text in the JTextArea will be wrapped.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class JCheckBoxMenuItemShow extends JFrame implements ChangeListener{
JTextArea txta;
JCheckBoxMenuItemShow(String title){
setLayout(new FlowLayout());
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JMenuBar mbar=new JMenuBar();
JMenu format=new JMenu("Format");
JCheckBoxMenuItem itemlinewrap=new JCheckBoxMenuItem("Line wrap");
txta=new JTextArea(10,30);
JScrollPane scroll=new JScrollPane(txta);


public void stateChanged(ChangeEvent e){
JCheckBoxMenuItem chm=(JCheckBoxMenuItem)e.getSource();


public class JFrameSwing {
public static void main(String[] args){
new JCheckBoxMenuItemShow("JCheckBoxMenuItem");

JCheckBoxMenuItem Swing

No comments:

Post a Comment