Create Check Box in Java

Posted By: Java Examples - 4:37 PM

Share

& Comment

This section illustrates you how to create a Check Box component in Java Swing.In this section, you can learn simply creating the Check Box in Java Swing. Check Boxes are created in swing by creating the instance of the JCheckBox class using it's constructor which contains the string which has to be shown beside the check box on the frame or window


Java version of Check Box is given bellow :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author AHCHUTHAN
 */
public class Checkboxtest extends JFrame {

    private JTextField field;
    private JCheckBox bold, italic;

    Checkboxtest() {
        super("Testing Checkbox");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        field = new JTextField("Font Stayle Changes", 20);
        field.setFont(new Font("counter", Font.PLAIN, 14));
        bold = new JCheckBox("BOLD");
        italic = new JCheckBox("ITALIC");

        CheckBoxHandler handler = new CheckBoxHandler();
        bold.addItemListener(handler);
        italic.addItemListener(handler);

        c.add(bold);
        c.add(italic);
        c.add(field);
        setSize(400, 400);
        setVisible(true);

    }

    public static void main(String args[]) {
        Checkboxtest b = new Checkboxtest();
        b.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public class CheckBoxHandler implements ItemListener {

        private int valBold = Font.PLAIN;
        private int valItalic = Font.PLAIN;

        public void itemStateChanged(ItemEvent e) {
            if (e.getSource() == bold) {
                valBold = bold.isSelected()
                        ? Font.BOLD : Font.PLAIN;
            }

            if (e.getSource() == italic) {
                valItalic = italic.isSelected()
                        ? Font.ITALIC : Font.PLAIN;
            }
            field.setFont(new Font("counter",
                    valBold + valItalic, 14));


        }
    }
}
Download

Output of this program :

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .