aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JCheckBoxMenuItem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCheckBoxMenuItem" Watch "JCheckBoxMenuItem" New topic
Author

JCheckBoxMenuItem

Rajani
Greenhorn

Joined: Jul 12, 2001
Posts: 7
Hi,
After I select an item in JCheckBoxMenuItem in a Menu,I am not able to clear after it does the action.
Can anybody suggest something??
I tried with setSelected(false)but it does not work
Thanx
Rajani
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi Rajani,
iam not sure ,why u want it and if iam clear with ur question,then take a look at this example.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class menu extends JFrame
{
JMenu menu,submenu;
JMenuBar menubar;
JMenuItem menuitem1,menuitem2,menuitem3,menuitem4;
JButton button1,button2;
JLabel label1,label2;
JCheckBoxMenuItem menuitem5;
Container cont;
menu()
{
cont=getContentPane();
cont.setLayout(new FlowLayout());
label1 = new JLabel("label1");
label2 = new JLabel("label2");
button1 = new JButton("button1");
button2 = new JButton("button2");
menu =new JMenu("Show");
menuitem1=new JMenuItem("button");
menuitem2=new JMenuItem("label");

menu.add(menuitem1);
menu.add(menuitem2);
menubar=new JMenuBar();
submenu = new JMenu("submenu");
menuitem3 = new JMenuItem("label");
menuitem4 = new JMenuItem("button");
menuitem5 = new JCheckBoxMenuItem("Show all ");
submenu.add(menuitem3);
submenu.add(menuitem4);
submenu.add(menuitem5);
menu.add(submenu);
menubar.add(menu);

menuitem1.addActionListener(new actionclass());
menuitem2.addActionListener(new actionclass());
menuitem3.addActionListener(new actionclass());
menuitem4.addActionListener(new actionclass());
menuitem5.addActionListener(new actionclass());
setJMenuBar(menubar);
cont.add(label1);cont.add(label2);
cont.add(button1);cont.add(button2);
label1.setVisible(false);
label2.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
}

class actionclass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(menuitem1))
{
button1.setVisible(true);
label1.setVisible(false);
button2.setVisible(false);
label2.setVisible(false);

}
else if(e.getSource().equals(menuitem2))
{
label1.setVisible(true);
button1.setVisible(false);
button2.setVisible(false);
label2.setVisible(false);

}
else if (e.getSource().equals(menuitem3))
{
label2.setVisible(true);
button1.setVisible(false);
label1.setVisible(false);
button2.setVisible(false);

}
else if (e.getSource().equals(menuitem4))
{
button2.setVisible(true);
button1.setVisible(false);
label1.setVisible(false);
label2.setVisible(false);
}
else if (e.getSource().equals(menuitem5))
{
button2.setVisible(true);
button1.setVisible(true);
label1.setVisible(true);
label2.setVisible(true);
// It defaults the State to unselected.menuitem5.setSelected(false);
}
}
}
public static void main(String a[])
{
JFrame frm = new menu();
frm.setSize(350,300);
frm.setVisible(true);
}
}
i may be wrong in understanding the question.
if ,so can get back to me.
Raj
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Sorry,the statement had gone along with the commented line.please ,drop it into new line while compiling the above program .
cheers,
Raj
Rajani
Greenhorn

Joined: Jul 12, 2001
Posts: 7
Hi Raj,
IN your code after I select ShowAll Button,the check remins after its action is performed.
I want to clear that check...
your help is great!!!
Tahnx
Rajani
Rajani
Greenhorn

Joined: Jul 12, 2001
Posts: 7
Hi Raj,
I jus posted after a min...so -
It work...thank u very much.....
Rajani
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Rajani,

Please re-register with a user name that follows the JavaRanch User Name Guidelines. Your user name is required to be in the format "First Name" + space + "Last Name".

Thanks!
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCheckBoxMenuItem