Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.16. Check boxes

5.16. Check boxes
Last updated: 25 January 2013.

This tutorial will show you how to use check boxes in Java.

The class JCheckBox inherits JToggleButton. Hence, a checkbox is a component that can be selected or deselected. In a group of checkboxes, any number of checkboxes can be selected simultaneously. Since the super class JToggleButton inherits AbstractButton, checkboxes have all the features provided by buttons. In particular, you can bind an icon, a tooltip text and a keyboard shortcut to a checkbox. The following picture shows a frame containing 3 checkboxes that allow the user to change the font weight, font style and foreground of a label:


Checkboxes



Here is the code:


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public final class CheckBoxes extends JFrame implements ItemListener {

   
private JCheckBox boldCheckBox;
   
private JCheckBox italicCheckBox;
   
private JCheckBox foregroundCheckBox;

   
private JLabel label;

   
private boolean boldBoolean;
   
private boolean italicBoolean;
   
private boolean foregroundBoolean;

   
public CheckBoxes(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));

         
// The left pane contains the checkboxes --------------------------------------------------------
          // ---------------------------------------------------------------------------------------------------
         
JPanel leftPane = new JPanel(new GridLayout(0, 1));
          leftPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          boldCheckBox =
new JCheckBox("Bold");
          boldCheckBox.setSelected
(false);
          boldCheckBox.addItemListener
(this);

          italicCheckBox =
new JCheckBox("Italic");
          italicCheckBox.setSelected
(false);
          italicCheckBox.addItemListener
(this);

          foregroundCheckBox =
new JCheckBox("Foreground");
          foregroundCheckBox.setSelected
(false);
          foregroundCheckBox.addItemListener
(this);

          leftPane.add
(boldCheckBox);
          leftPane.add
(italicCheckBox);
          leftPane.add
(foregroundCheckBox);

          contentPane.add
(leftPane);
         
// ---------------------------------------------------------------------------------------------------



          // The right pane contains the label -------------------------------------------------------------
          // --------------------------------------------------------------------------------------------------
         
JPanel rightPane = new JPanel();
          rightPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          label =
new JLabel();
          label.setHorizontalAlignment
(SwingConstants.CENTER);
          label.setPreferredSize
(new Dimension(180, 100));
          label.setText
("Checkbox example");

          rightPane.add
(label);

          contentPane.add
(rightPane);
         
// ---------------------------------------------------------------------------------------------------


          // Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init(){
         
setTitle("Checkboxes");
          setSize
(300, 150);
          setLocationRelativeTo
(null);
   
}


   
public void itemStateChanged(ItemEvent e){

         
if(e.getSource().equals(boldCheckBox)){
               
if(e.getStateChange() == ItemEvent.SELECTED)
                     
boldBoolean = true;
               
else
                     
boldBoolean = false;
         
}
         
else if(e.getSource().equals(italicCheckBox)){
               
if(e.getStateChange() == ItemEvent.SELECTED)
                     
italicBoolean = true;
               
else
                     
italicBoolean = false;
         
}
         
else if(e.getSource().equals(foregroundCheckBox)){
               
if(e.getStateChange() == ItemEvent.SELECTED)
                     
foregroundBoolean = true;
               
else
                     
foregroundBoolean = false;
         
}

         
updateLabel();
   
}


   
private void updateLabel(){
         
Font defaultFont = label.getFont();

         
if(boldBoolean){
               
if(italicBoolean)
                     
label.setFont(new Font(defaultFont.getName(), (Font.ITALIC | Font.BOLD), defaultFont.getSize()));
               
else
                     
label.setFont(new Font(defaultFont.getName(), Font.BOLD, defaultFont.getSize()));
         
}
         
else{
               
if(italicBoolean)
                     
label.setFont(new Font(defaultFont.getName(), Font.ITALIC, defaultFont.getSize()));
               
else
                     
label.setFont(new Font(defaultFont.getName(), Font.PLAIN, defaultFont.getSize()));
         
}

         
if(foregroundBoolean)
               
label.setForeground(Color.BLUE);
         
else
               
label.setForeground(Color.BLACK);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new CheckBoxes();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.16. Check boxes
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.17. Radio buttons

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7