JCheckBox and JRadioButton are two components in Java Swing Library. These two components are very similar to the JButton Component that we have Discussed in the Previous Article. These two components are special types of buttons that maintain states. The states may be on/off or it may be true/false. These two JCheckBox and JRadioButton although look like buttons but they are the subclasses of JToggleButton. 

We will look at how these components work and which methods are present in the JCheckBox and JRadioButton. First of all, let's start with JCheckBox and then we will look into the JRadioButton. 

JCheckBox in Java GUI (swing)

In Graphical user interface Programs or we can say in general Application that we are using. we find some time the need for Checkbox. where we need to check whether the user agrees or not. or we can check what is the choice of the user. A check box is also useful in cases when there is a list of item and we want the user to select from the list by checking them. Java gives us this awesome component with the help of the JCheckBox component. we will look into the example so that we can understand how the checkboxes work in the Graphical user interfaces.

 
What we want to do in our Simple Program?
well, we look into a very simple program to understand how we can use the JCheckBox. we will not be doing anything crazy. Of course in the next some articles we look into that as well. Once we reach the level of event handling I will let you to the deep concepts of event handling. but for now, we are good with the simple example. 
We will create a simple program where there will be a simple check box. We will also use a button. the button will remain disabled unless the user checks the checkbox. We will look into some important methods of JCheckBox class. 

Below is a simple code that does the above job. please go through the code. comments in the code will help you better understand the purpose of each statement. 

package AlixaProDev;

import javax.swing.JCheckBox ;
import javax.swing.JFrame;

import java.awt.*;

public class JCheckBoxDemo {
// Lets first create reference of JCheckBox
JCheckBox checkBox;
// Now create the Constructor of the JCheckBox()
JCheckBoxDemo(){
// now lets create the instance of JCheckBox
// and assign it to the reference checkBox
checkBox = new JCheckBox("I am the Check Box");

// lets create our main Frame to which we
// will attach the components
JFrame frame = new JFrame("AlixaProDev");

// add the Components to our JFrame
frame.add(checkBox);

// lets customize our frame
frame.setLayout(new FlowLayout());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main (String[] args) {
new JCheckBoxDemo();
}
}

The above code is not the full code of what we want to achieve it will simply create a simple Checkbox with the text that I am a checkbox. I recommend running this in your editor and do some changes and I hope you will be able to achieve the result. but if you are not able, do not worry. Look at the below code this is the code that will achieve the result.

package AlixaProDev;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class JCheckBoxDemo {
// Lets creat the refrences of the components
// that we will need to use in our proogram
JFrame frame ;
JCheckBox checkBox;
JButton button ;
JLabel label;
Font myFavFont;
// Now create the Constructor of the JCheckBox()
JCheckBoxDemo(){
// now lets create the instance of JCheckBox
// and assign it to the reference checkBox

checkBox = new JCheckBox("Do you agree to terms?");
frame = new JFrame("AlixaProDev");
label = new JLabel();
button = new JButton("Login");
myFavFont = new Font("Mv Boli",Font.BOLD,30);
label.setText("THese are the Terms and Conditons!");

label.setBounds(10,20,580,30);
checkBox.setBounds(10,50,580,30);
button.setBounds(10,90,580,30);

label.setFont(myFavFont);
checkBox.setFont(myFavFont);
button.setFont(myFavFont);

label.setForeground(Color.red);
checkBox.setForeground(Color.BLUE);
button.setForeground(Color.black);
button.setBackground(Color.white);

button.setEnabled(false);
checkBox.addChangeListener(new ChangeListener() {
@Override
public void stateChanged (ChangeEvent e) {
if (e.getSource()==checkBox){
button.setEnabled(checkBox.isSelected());

}
}
});

frame.add(label);
frame.add(checkBox);
frame.add(button);
frame.setLayout(null);
frame.setSize(new Dimension(600,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main (String[] args) {
new JCheckBoxDemo();
}
}

The output of the code is in the following picture. the login button will be enabled once the user checks on the checkbox. 

JcheckBox in Java Swing

JRadioButton in Java GUI (swing)

JRadioButton does a very similar job to the JCheckBox but the difference is that JradioButton allows the user to select only one choice from a list of choices while in the JCheckBox we can achieve this result but it is a little bit hard. The JRadioButton restrict the user to select only one choice form the list of the choices. JRadioButton component is helpful in many cases like when we are working with a multiple choice question program. We will look into the different methods that are available to us in the JRadioButton class. JRadioButton class in the Java Swing Library do have many Methods. The way that JRadioButton work is. first, you have to create a bunch of buttons and then you have to group them together so that the user can select only one button. if you did not join these buttons in the group then It will work but the user will be able to select multiple buttons as well. which is not a good practice.

Okay, enough talk. now we will create a simple program to demonstrate the use of JRadioButton. we will create a program in which the user will be able to choose his choice and we will store that choice in a variable. Go through the below code and look into the comments that will help you to understand the purpose of each statement.

A Test APPlication
package AlixaProDev;

import javax.swing.*;
import java.awt.*;

public class JRadioButtonDemo {
JFrame frame ;
JRadioButton radBtn1;
JRadioButton radBtn2;
JRadioButton radBtn3;
JRadioButton radBtn4;
JLabel label;
JLabel resultLabel;
Font myFavFont;
ButtonGroup btnGroup;

JRadioButtonDemo(){
myFavFont = new Font("Helvetica Neue",Font.BOLD,24);
frame = new JFrame("AlixaProDev");
radBtn1 = new JRadioButton("Python");
radBtn2 = new JRadioButton("Java");
radBtn3 = new JRadioButton("PHP");
radBtn4 = new JRadioButton("HTML");
label = new JLabel("Which Language Do you like?");
resultLabel = new JLabel();


frame.add(label);
frame.add(radBtn1);
frame.add(radBtn2);
frame.add(radBtn3);
frame.add(radBtn4);
frame.add(resultLabel);

label.setFont(myFavFont);
radBtn1.setFont(myFavFont);
radBtn2.setFont(myFavFont);
radBtn3.setFont(myFavFont);
radBtn4.setFont(myFavFont);
resultLabel.setFont(myFavFont);

btnGroup = new ButtonGroup();
btnGroup.add(radBtn1);
btnGroup.add(radBtn2);
btnGroup.add(radBtn3);
btnGroup.add(radBtn4);


label.setBounds(20,20,350,30);
radBtn1.setBounds(20,50,300,30);
radBtn2.setBounds(20,80,300,30);
radBtn3.setBounds(20,110,300,30);
radBtn4.setBounds(20,140,300,30);
resultLabel.setBounds(20,170,300,30);

frame.setSize(new Dimension(500,500));
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}

public static void main (String[] args) {
new JRadioButtonDemo();
}

}

here is a lot to discuss in the above code. I will try to make it simple. First of look into the Result then I will try to demonstrate the code to you. 

radion Button in java

Check the code in your IDE. Try to make changes in the code and then look into the result. There are a few things that I want to discuss to you. 

Look in the code, first of all, we have created our Radio Buttons. Now the purpose of the radio buttons is they should be selected one at a time. To achieve this result we have created a Radiobutton group and then added our buttons. all the rest code is not that much complex. I hope you will understand it. If there is something you want me to ask. please do not hesitate to write your question in the comment section and I will answer all of them. 

I Recommend you also read these Articles.

Post a Comment

Previous Post Next Post