Tuesday, 22 August 2017

Java Swing Checkbox Example | Java Swing Program with Checkbox | JCheckBox in Swing Java

                      In this post, we will see Java Swing Checkbox Example | Java Swing Program with Checkbox | JCheckBox in Swing Java.

Watch following video:

                    To know what is Swing, how Swing is different from Applet, how to create Swing Application; check this post http://www.comrevo.com/2017/08/swing-in-java-with-example.html                    

                    To know how to generate event in Java Swing along with Java Swing program for addition of two numbers; check this post http://www.comrevo.com/2017/08/java-swing-program-for-addition-of-two-numbers.html 

                    Here, we will see Java Swing program for adding checkbox on frame.  

                    In Swing, we can add checkbox on frame by creating object of JCheckBox and by calling add() method.

                    Go through the following program:


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class SwingCheckBox

   public static void main(String args[])
       Abc obj=new Abc();

class Abc extends JFrame implements ActionListener

  JLabel l1,l2,l3,l4;
  JTextField t1,t2;
  JButton b;
  JCheckBox c1,c2,c3;

  public Abc()

     setLayout(new FlowLayout());

     l1=new JLabel("Name:");

     t1=new JTextField(25); 
     l2=new JLabel("College:");
     t2=new JTextField(25);
     l3=new JLabel("Facilities:");

     c1=new JCheckBox("Hostel");

     c2=new JCheckBox("Mess");
     c3=new JCheckBox("Gym");

     b=new JButton("Submit");

     l4=new JLabel();






 public void actionPerformed(ActionEvent ae)

    String name=t1.getText();
    String college=t2.getText();
    String message="Welcome "+name+" @"+college+"; You opted for ";
       message=message+c1.getText()+" ";
       message=message+c2.getText()+" ";


How To Run:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac SwingCheckBox.java 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java SwingCheckBox


                        Check other posts on Java Programming in this link http://www.comrevo.com/2017/08/java-programming.html