SEARCH HERE

Tuesday, December 27, 2022

Java JCheckBox

 

Java JCheckBox

The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ".It inherits JToggleButton class.

JCheckBox class declaration

Let's see the declaration for javax.swing.JCheckBox class.

  1. public class JCheckBox extends JToggleButton implements Accessible  

Commonly used Constructors:

ConstructorDescription
JJCheckBox()Creates an initially unselected check box button with no text, no icon.
JChechBox(String s)Creates an initially unselected check box with text.
JCheckBox(String text, boolean selected)Creates a check box with text and specifies whether or not it is initially selected.
JCheckBox(Action a)Creates a check box where properties are taken from the Action supplied.

Commonly used Methods:

MethodsDescription
AccessibleContext getAccessibleContext()It is used to get the AccessibleContext associated with this JCheckBox.
protected String paramString()It returns a string representation of this JCheckBox.

Java JCheckBox Example

  1. import javax.swing.*;  
  2. public class CheckBoxExample  
  3. {  
  4.      CheckBoxExample(){  
  5.         JFrame f= new JFrame("CheckBox Example");  
  6.         JCheckBox checkBox1 = new JCheckBox("C++");  
  7.         checkBox1.setBounds(100,10050,50);  
  8.         JCheckBox checkBox2 = new JCheckBox("Java"true);  
  9.         checkBox2.setBounds(100,15050,50);  
  10.         f.add(checkBox1);  
  11.         f.add(checkBox2);  
  12.         f.setSize(400,400);  
  13.         f.setLayout(null);  
  14.         f.setVisible(true);  
  15.      }  
  16. public static void main(String args[])  
  17.     {  
  18.     new CheckBoxExample();  
  19.     }}  

Output:

JAVA Jcheckbox 1

Java JCheckBox Example with ItemListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;    
  3. public class CheckBoxExample    
  4. {    
  5.      CheckBoxExample(){    
  6.         JFrame f= new JFrame("CheckBox Example");    
  7.         final JLabel label = new JLabel();            
  8.         label.setHorizontalAlignment(JLabel.CENTER);    
  9.         label.setSize(400,100);    
  10.         JCheckBox checkbox1 = new JCheckBox("C++");    
  11.         checkbox1.setBounds(150,10050,50);    
  12.         JCheckBox checkbox2 = new JCheckBox("Java");    
  13.         checkbox2.setBounds(150,15050,50);    
  14.         f.add(checkbox1); f.add(checkbox2); f.add(label);    
  15.         checkbox1.addItemListener(new ItemListener() {    
  16.              public void itemStateChanged(ItemEvent e) {                 
  17.                 label.setText("C++ Checkbox: "     
  18.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  19.              }    
  20.           });    
  21.         checkbox2.addItemListener(new ItemListener() {    
  22.              public void itemStateChanged(ItemEvent e) {                 
  23.                 label.setText("Java Checkbox: "     
  24.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  25.              }    
  26.           });    
  27.         f.setSize(400,400);    
  28.         f.setLayout(null);    
  29.         f.setVisible(true);    
  30.      }    
  31. public static void main(String args[])    
  32. {    
  33.     new CheckBoxExample();    
  34. }    
  35. }    

Output:




JAVA Jcheckbox 2


Java JCheckBox Example: Food Order

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class CheckBoxExample extends JFrame implements ActionListener{  
  4.     JLabel l;  
  5.     JCheckBox cb1,cb2,cb3;  
  6.     JButton b;  
  7.     CheckBoxExample(){  
  8.         l=new JLabel("Food Ordering System");  
  9.         l.setBounds(50,50,300,20);  
  10.         cb1=new JCheckBox("Pizza @ 100");  
  11.         cb1.setBounds(100,100,150,20);  
  12.         cb2=new JCheckBox("Burger @ 30");  
  13.         cb2.setBounds(100,150,150,20);  
  14.         cb3=new JCheckBox("Tea @ 10");  
  15.         cb3.setBounds(100,200,150,20);  
  16.         b=new JButton("Order");  
  17.         b.setBounds(100,250,80,30);  
  18.         b.addActionListener(this);  
  19.         add(l);add(cb1);add(cb2);add(cb3);add(b);  
  20.         setSize(400,400);  
  21.         setLayout(null);  
  22.         setVisible(true);  
  23.         setDefaultCloseOperation(EXIT_ON_CLOSE);  
  24.     }  
  25.     public void actionPerformed(ActionEvent e){  
  26.         float amount=0;  
  27.         String msg="";  
  28.         if(cb1.isSelected()){  
  29.             amount+=100;  
  30.             msg="Pizza: 100\n";  
  31.         }  
  32.         if(cb2.isSelected()){  
  33.             amount+=30;  
  34.             msg+="Burger: 30\n";  
  35.         }  
  36.         if(cb3.isSelected()){  
  37.             amount+=10;  
  38.             msg+="Tea: 10\n";  
  39.         }  
  40.         msg+="-----------------\n";  
  41.         JOptionPane.showMessageDialog(this,msg+"Total: "+amount);  
  42.     }  
  43.     public static void main(String[] args) {  
  44.         new CheckBoxExample();  
  45.     }  
  46. }  

Output:

JAVA Jcheckbox 21 JAVA Jcheckbox 22

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top