Monday, August 26, 2013

JCheckBoxMenuItem

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.

-JCheckBoxMenuItem()
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){
setTitle(title);
setSize(400,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JMenuBar mbar=new JMenuBar();
JMenu format=new JMenu("Format");
JCheckBoxMenuItem itemlinewrap=new JCheckBoxMenuItem("Line wrap");
itemlinewrap.setSelected(true);
format.add(itemlinewrap);
mbar.add(format);
setJMenuBar(mbar);
txta=new JTextArea(10,30);
JScrollPane scroll=new JScrollPane(txta);
contpane.add(scroll,BorderLayout.CENTER);
setVisible(true);

}

public void stateChanged(ChangeEvent e){
JCheckBoxMenuItem chm=(JCheckBoxMenuItem)e.getSource();
if(chm.getState()==true)
txta.setLineWrap(true);
else
txta.setLineWrap(false);
}

}

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

JCheckBoxMenuItem Swing

6 comments:

  1. Das Kontrollieren des Kästchens kann ein Problem sein. Wenn du willst, kannst du das machen, ich betway wetten und sie werden dich nicht um eine einzige Frage bitten. Ich habe es ausprobiert und es hat bei mir so oft mit Sicherheit funktioniert.

    ReplyDelete
  2. Thank you, I have recently been looking for info about this subject for ages and yours is the best I have discovered till now. But, what about the bottom line? Are you sure about the source? hearing aids

    ReplyDelete
  3. Hearing Aids Singapore - Visit us for hearing aids in Singapore! Our team can test your hearing and recommend the most suitable hearing aid for your condition.

    ReplyDelete
  4. Receive immediate & long-term suggestions on how to optimize your website. Try it free! Strategy, backlinks, tech SEO, UX, SERP Features, semantics & content ideas. User Experience Ideas. Best Group Buy SEO Tools 2020 Winner. Be in Top 10 of the SERP.

    ReplyDelete
  5. Purchase Top Quality Home gym equipment Malaysia at the Best Price for your home. Visit our outlets now! Built your Home Gym now! Get the Best Price & After-Sales Services. Visit our outlets now! 24/7 Online Store. 45 Years Experience. World Leading Brand. After-Sales Service.

    ReplyDelete
  6. group buy seo tools provides 250 cheap seo tools with high quality, number one reputation worldwide. 100% customer satisfaction guarantee

    ReplyDelete