Java Swing Checkbox Easy Example

Java Swing | JCheckBox with examples

View Discussion

Improve Article

Save Article

Like Article

JCheckBox is a part of Java Swing package . JCheckBox can be selected or deselected . It displays it state to the user . JCheckBox is an implementation to checkbox . JCheckBox inherits JToggleButton class.

Constructor of the class are :

  1. JCheckBox() : creates a new checkbox with no text or icon
  2. JCheckBox(Icon i) : creates a new checkbox with the icon specified
  3. JCheckBox(Icon icon, boolean s) : creates a new checkbox with the icon specified and the boolean value specifies whether it is selected or not.
  4. JCheckBox(String t) :creates a new checkbox with the string specified
  5. JCheckBox(String text, boolean selected) :creates a new checkbox with the string specified and the boolean value specifies whether it is selected or not.
  6. JCheckBox(String text, Icon icon) :creates a new checkbox with the string and the icon specified.
  7. JCheckBox(String text, Icon icon, boolean selected): creates a new checkbox with the string and the icon specified and the boolean value specifies whether it is selected or not.

Methods to add Item Listener to checkbox.

  1. addActionListener(ItemListener l): adds item listener to the component
  2. itemStateChanged(ItemEvent e) : abstract function invoked when the state of the item to which listener is applied changes
  3. getItem() : Returns the component-specific object associated with the item whose state changed
  4. getStateChange() : Returns the new state of the item. The ItemEvent class defines two states: SELECTED and DESELECTED.
  5. getSource() : Returns the component that fired the item event.

Commonly used methods:

  1. setIcon(Icon i) : sets the icon of the checkbox to the given icon
  2. setText(String s) :sets the text of the checkbox to the given text
  3. setSelected(boolean b) : sets the checkbox to selected if boolean value passed is true or vice versa
  4. getIcon() : returns the image of the checkbox
  5. getText() : returns the text of the checkbox
  6. updateUI() : resets the UI property with a value from the current look and feel.
  7. getUI() : returns the look and feel object that renders this component.
  8. paramString() : returns a string representation of this JCheckBox.
  9. getUIClassID() : returns the name of the Look and feel class that renders this component.
  10. getAccessibleContext() : gets the AccessibleContext associated with this JCheckBox.
  11. isBorderPaintedFlat() : gets the value of the borderPaintedFlat property.
  12. setBorderPaintedFlat(boolean b) : sets the borderPaintedFlat property,

The following programs will illustrate the use of JCheckBox

1. Program to create a simple checkbox using JCheckBox

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class solve extends JFrame {

static JFrame f;

public static void main(String[] args)

{

f = new JFrame( "frame" );

f.setLayout( new FlowLayout());

JCheckBox c1 = new JCheckBox( "checkbox 1" );

JCheckBox c2 = new JCheckBox( "checkbox 2" );

JPanel p = new JPanel();

p.add(c1);

p.add(c2);

f.add(p);

f.setSize( 300 , 300 );

f.show();

}

}

Output :

2. Program to create a checkbox with a icon .

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class solve extends JFrame {

static JFrame f;

public static void main(String[] args)

{

f = new JFrame( "frame" );

f.setLayout( new FlowLayout());

JCheckBox c1 = new JCheckBox( "checkbox with image" , new ImageIcon( "f:/gfg.jpg" ), true );

JCheckBox c2 = new JCheckBox( "checkbox 2" );

JPanel p = new JPanel();

p.add(c1);

p.add(c2);

f.add(p);

f.setSize( 300 , 300 );

f.show();

}

}

Output :

3. Program to create a checkbox and ItemListener to it.

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class solve extends JFrame implements ItemListener {

static JFrame f;

static JLabel l, l1;

static JCheckBox c1, c2;

public static void main(String[] args)

{

f = new JFrame( "frame" );

solve s = new solve();

f.setLayout( new FlowLayout());

c1 = new JCheckBox( "geeksforgeeks" , new ImageIcon( "f:/gfg.jpg" ), false );

c2 = new JCheckBox( "checkbox 2" , false );

c1.addItemListener(s);

c2.addItemListener(s);

l = new JLabel( "geeksforgeeks not selected" );

l1 = new JLabel( "checkbox2 not selected" );

l.setForeground(Color.red);

l1.setForeground(Color.blue);

JPanel p = new JPanel();

p.add(c1);

p.add(c2);

p.add(l);

p.add(l1);

f.add(p);

f.setSize( 600 , 300 );

f.show();

}

public void itemStateChanged(ItemEvent e)

{

if (e.getSource() == c1) {

if (e.getStateChange() == 1 )

l.setText( "geeksforgeeks  selected" );

else

l.setText( "geeksforgeeks  not selected" );

}

else {

if (e.getStateChange() == 1 )

l1.setText( "checkbox 2  selected" );

else

l1.setText( "checkbox 2 not selected" );

}

}

}

Output :

Note : the above programs might not run in a online compiler please use an offline IDE


robertshalwas.blogspot.com

Source: https://www.geeksforgeeks.org/java-swing-jcheckbox-examples/

0 Response to "Java Swing Checkbox Easy Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel