JCheckBox in Java Swing


Code for JCheckBox in Swing.

File:demo1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class demo1 extends JFrame implements ActionListener
{
JLabel l;
JCheckBox c1,c2;
demo1()
{
super("Check Box Example");
<!--more-->
setLayout(new FlowLayout());
c1=new JCheckBox("Enable");
getContentPane().add(c1);
c2=new JCheckBox("Disable");
getContentPane().add(c2);
l=new JLabel("Enable Label");
getContentPane().add(l);
l.setVisible(false);
myadapter mine=new myadapter();
addWindowListener(mine);
c1.addActionListener(this);
c2.addActionListener(this);</code>

}
public class myadapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==c1)
{
c2.setSelected(false);
l.setVisible(true);
}
else
{
c1.setSelected(false);
l.setVisible(false);
}
}
public static void main(String ar[])
{
demo1 d=new demo1();
d.setSize(400,400);
d.setVisible(true);
}
}

Output look like

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s