← Home

Form Controls


A radio button is a button that you often see on multiple choice quizzes, the ones that are circular and fill in when you click them. To create these you have to use JRadioButton, which work exactly the same as normal buttons. To create one you would have to do this:

1JRadioButton btn1 = new JRadioButton("One");
2//where "One" is the label of the button.

Adding this to the JFrame will create a radio button. To listen to changes use setActionListener() and actionPerformed(). However, what use would a radio button be if you could click more than one at the same time? Anything that requires only one option being selected would be broken. To fix this you have to add the buttons ot a ButtonGroup. This makes sure that only one option works at a time. The following code would be used:

1ButtonGroup buttons = new ButtonGroup();

to create the buttongroup, and then:


would be used in the constructor to add the button to the button group. Therefore the following would be outputted:

Radio buttons

Ex 1

Make 3 radio buttons with different names of subjects (History, Computing, Maths etc.) and have a JLabel ask which is the user's favourite. Also have the JLabel respond to that with an appropriate message.


Check boxes are exactly the same a radio buttons, only look slightly different. These, instead of becoming a filled circle upon being clicked, become a ticked box. To use these you will require a ButtonGroup, as before, and to import JCheckBoxes. These also interact with ActionListeners. The code for these is pretty intuitive, but I'll display it anyway:

1JCheckBox one = new JCheckBox("Noodles");

Using these you can create things like the following:

Some check boxes


A JSlider is exactly what you think it is, and needs to be added to a JFrame:

1JSlider slide = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);

The contents of the parameters are necessary for this component. The HORIZONTAL refers to the positioning of the slider i.e. horizontally or vertically. The first 0 dictates the number the slider starts at and the 255 is the number the slider ends at. The second 0 dicates where the pointer will be when you start the program. And that's pretty much it, adding that to a JFrame will create a slider. You can however further customise the slider. Including the following code in the constructor will change the slider:

2//every big number comes along every 10.
4//every small number is shown every 1.
6//creates the numbers.
8//creates the lines

A Slider at no value

To listen for a change in value for a JSlider, you must use ChangeListener, as opposed to an ActionListener. This listener is used to, rather unimaginably, listen for a change. Shock horror it is named sensibly!!!! To do so the class must implement the ChangeListener e.g.:

1public class Sliders implements ChangeListener {

A class can implement both an ActionListener and a ChangeListener and whatever else you want, just by separating them by commas, e.g.:

1public class ImplementsManyThings implements ChangeListener, ActionListener, MouseListener {

A ChangeListener must have the method stateChanged(ChangeEvent e) which is called when the slider value is changed.

1public void stateChanged(ChangeEvent e) {
2  //code

Ex 2

Create an application with three sliders that correspond to red, green and blue. You see in computing (and science) any colour can be represented by 3 different values: red, green and blue. Each value is an integer between 0 to 255. So for example black is (0,0,0) i.e. all of its values are 0. This is because black is the absence of light. White, on the other hand, is (255,255,255), because white is a mixture of all colours. If you want, look up RGB values for other colours. The sliders change the background of the JFrame. Put the sliders in a panel with a different background colour to see them better. To do this use the Color class, e.g.:

1new Color(redValue, blueValue, greenValue);

You've seen listeners before but let's get them done with →