Programming & Scripting Tutorials

Java: JCheckBox



In this lesson we are going to do about the JCheckBox.
Checkboxes are great tools, and in our example we are going to do the usual suspect of changing the style of text depending on which textboxes are selected.

Create a new class called App(.java) and then we can do our imports.
You should understand most of these, however the itemlistener is new to you; It's basically like the actionlistener but for events that stay selected or unselected (like the checkbox):

import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Font;

Now we've done that we can create our class:

public class App extends JFrame{

Inside our class we can now create various variables to be our items:

private JLabel label;
private JCheckBox BoxOne;
private JCheckBox BoxTwo;

Now as usual we can actually start coding the thing properly, lets create our constructor and do all of the basic setting of the title etc:

App(){
    super("Title!");
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 100);

Ok..
So now lets create our label which we will change the style of. We will also set the font:

label = new JLabel("The quick brown fox jumps over the lazy dog");
    label.setFont(new Font("Serif", Font.PLAIN, 14));

With the whole changing font business you may have noticed there are three parameters; The first is the font name, the second is the font decoration, and the third is the font size.

We can now add our label to the window:

add(label);

To set our checkbox variables to be checkboxes we simple use the "new" keyword (as we have been doing with other items), we can then add these to the window:

BoxOne = new JCheckBox("Bold");
    add(BoxOne);
    BoxTwo = new JCheckBox("Italic");
    add(BoxTwo);

Alright, so now we have some checkboxes; but they are just sitting there on the screen.
To make them do something we will have to create a handlerclass; However while we are still messing around with the constructor lets just add the lines we need for our ItemListener (and make the form visible):

    HandlerClass handler = new HandlerClass();

    BoxOne.addItemListener(handler);
    BoxTwo.addItemListener(handler);

    setVisible(true);
}

Next lets get to work on our HandlerClass, we can create it like normal:

private class HandlerClass implements ItemListener{

We have created the class a lot like we would have done using ActionListener.
Now because we have implemented ItemListener we have to override its function; Luckily it only has one 'itemStateChanged' and is accepts an ItemEvent as a parameter:

public void itemStateChanged(ItemEvent event){

Right, so now because we will be messing around with the label's font we need to create a new font variable which we can apply to the label afterwards:

Font font;

Now we can simply use the "isSelected" function to find out which boxes are selected, and to set the font variable appropriately:

if(BoxOne.isSelected() && BoxTwo.isSelected()){
font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
} else if(BoxOne.isSelected()){
font = new Font("Serif", Font.BOLD, 14);
} else if(BoxTwo.isSelected()){
font = new Font("Serif", Font.ITALIC, 14);
} else{
    font = new Font("Serif", Font.PLAIN, 14);
}

We haven't covered font stuff before, but its pretty much common logic.
Now all we need to do is set the font to the label:

label.setFont(font);

Now we can close all our brackets (at last!):

}
}
}

Finally we can just go back to our main(.java) file, and create an object for our App class:

App object = new App();

We can now successfully run our program and see the results:





Full source code:

App.java:
import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Font;

public class App extends JFrame{
private JLabel label;
private JCheckBox BoxOne;
private JCheckBox BoxTwo;
App(){
    super("Title!");
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 100);
    label = new JLabel("The quick brown fox jumps over the lazy dog");
    label.setFont(new Font("Serif", Font.PLAIN, 14));
    add(label);

    BoxOne = new JCheckBox("Bold");
    add(BoxOne);
    BoxTwo = new JCheckBox("Italic");
    add(BoxTwo);

    HandlerClass handler = new HandlerClass();

    BoxOne.addItemListener(handler);
    BoxTwo.addItemListener(handler);

    setVisible(true);
}

private class HandlerClass implements ItemListener{
public void itemStateChanged(ItemEvent event){
Font font;

if(BoxOne.isSelected() && BoxTwo.isSelected()){
font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
} else if(BoxOne.isSelected()){
font = new Font("Serif", Font.BOLD, 14);
} else if(BoxTwo.isSelected()){
font = new Font("Serif", Font.ITALIC, 14);
} else{
    font = new Font("Serif", Font.PLAIN, 14);
}
label.setFont(font);

}
}

}

Main.java:

public class Main {


    public static void main(String[] args) {
       App Object = new App();

    }

}


This Java tutorial was written by


Back to Java

Advertisement: