Java: JRadioButton
This lesson is about the radio button (the little circle things, were only one can be selected at a time).
Radio buttons are pretty useful when you want the user to only select one thing out of a list of things.
Obviously create a new java project, and then make a new class called App(.java) and then we can do our imports (for App.java):
import java.awt.*; import java.awt.event.*; import javax.swing.*;
We have used generalised imports this time, as it saves us time while coding (it basically just imports all of the stuff from the category we have specified).
Now we can begin and end our class:
public class App extends JFrame{
}
Next inside our class we are going to create the variables we are going to need for the program:
private JLabel label; private Font plain; private Font bold; private Font italic; private Font bolditalic; private JRadioButton plainbutton; private JRadioButton boldbutton; private JRadioButton italicbutton; private JRadioButton bolditalicbutton; private ButtonGroup group;
Basically we are going to have plain, bold, italic, and bold+italic bits of text which will be shows when different radio buttons (which have been named appropriately); We also have a group so that we can relate the radio buttons so that only one can be selected at a time.
So lets make our constructor:
public App(){
super("A Radiobutton App!");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
label = new JLabel("dev-hq.co.uk");
add(label);
Ok. So we have all the normal stuff, and we have added the label; now we are going to need to put on the buttons...
The first parameter is the text that goes next to the button, and the second is if it starts checked or not (we only want one of them true):
plainbutton = new JRadioButton("Plain Text", true);
boldbutton = new JRadioButton("Bold Text", false);
italicbutton = new JRadioButton("Italic Text", false);
bolditalicbutton = new JRadioButton("Bold+Italic Text", false);
Thats all cool, so we can add them to the form:
add(plainbutton); add(boldbutton); add(italicbutton); add(bolditalicbutton);
Alright so now we have to create our group, all we have to do is the following (its pretty easy):
group = new ButtonGroup(); group.add(plainbutton); group.add(boldbutton); group.add(italicbutton); group.add(bolditalicbutton);
Now the radio buttons are in a group, they no that only one of them can be selected at a time; so when we select one, the other selected one will deselect itself!
So now lets set the fonts for our font variables:
plain = new Font("Serif", Font.PLAIN, 14);
bold = new Font("Serif", Font.BOLD, 14);
italic = new Font("Serif", Font.ITALIC, 14);
bolditalic = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
Ok, our fonts are now set to what they are supposed to be.
Now we need to set our labels font to the plain default font:
label.setFont(plain);
Right. So now we have to add the AddItemListeners for the radio buttons (so they know what to do(and set the form to be visible while we are in the constructor)):
plainbutton.addItemListener(new HandlerClass(plain)); boldbutton.addItemListener(new HandlerClass(bold)); italicbutton.addItemListener(new HandlerClass(italic)); bolditalicbutton.addItemListener(new HandlerClass(bolditalic)); setVisible(true);
Ok, so now we need to create our handler class, so close the bracket to the constructor (if you havent already):
}
And then create our class as we usually do:
private class HandlerClass implements ItemListener{
private Font f;
}
So we have our handler class (and a font variable(f)); now we need to build our constructor, lets do it (which is called earlier in our code):
public HandlerClass(Font font){
f = font;
}
Now our itemstatechanged function:
public void itemStateChanged(ItemEvent event){
label.setFont(f);
}
Now we just set the label to the font that the user has selected (via the radio buttons).
Finally go back to Main.java and simply create a class object, and then you can sucessfully run the program!
App object = new App();
Full source code:
Main.java:
public class Main {
public static void main(String[] args) {
App object = new App();
}
}
App.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class App extends JFrame{
private JLabel label;
private Font plain;
private Font bold;
private Font italic;
private Font bolditalic;
private JRadioButton plainbutton;
private JRadioButton boldbutton;
private JRadioButton italicbutton;
private JRadioButton bolditalicbutton;
private ButtonGroup group;
public App(){
super("A Radiobutton App!");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
label = new JLabel("dev-hq.co.uk");
add(label);
plainbutton = new JRadioButton("Plain Text", true);
boldbutton = new JRadioButton("Bold Text", false);
italicbutton = new JRadioButton("Italic Text", false);
bolditalicbutton = new JRadioButton("Bold+Italic Text", false);
add(plainbutton);
add(boldbutton);
add(italicbutton);
add(bolditalicbutton);
group = new ButtonGroup();
group.add(plainbutton);
group.add(boldbutton);
group.add(italicbutton);
group.add(bolditalicbutton);
plain = new Font("Serif", Font.PLAIN, 14);
bold = new Font("Serif", Font.BOLD, 14);
italic = new Font("Serif", Font.ITALIC, 14);
bolditalic = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
label.setFont(plain);
plainbutton.addItemListener(new HandlerClass(plain));
boldbutton.addItemListener(new HandlerClass(bold));
italicbutton.addItemListener(new HandlerClass(italic));
bolditalicbutton.addItemListener(new HandlerClass(bolditalic));
setVisible(true);
}
private class HandlerClass implements ItemListener{
private Font f;
public HandlerClass(Font font){
f = font;
}
public void itemStateChanged(ItemEvent event){
label.setFont(f);
}
}
}
Back to Java

