Previous lesson: 14.7 JRadioButtonCourse contents JComboBox The JComboBox class allows the user to select an item using a drop-down list: JComboBox colourCombo = new JComboBox();colourCombo.addItem("Red");colourCombo.addItem("Green");colourCombo.addItem("Blue");panel.add(colourCombo); It looks like this: If you click on the small down-pointing arro...
Previous lesson: 14.6 JCheckBoxCourse contents JRadioButton The JRadioButton class allows the selection of one option in a group of mutually exclusive options; that is, where there are two or more possible options but only one option should ever be chosen. To use JRadioButton objects you need to assign them to a ButtonGroup object which manages the...
Previous lesson: 14.5 JButton Course contents JCheckBox The JCheckBox class allows the entry of values which are true or false by enabling the user to click a small rectangular box resulting in a "tick" mark being displayed within: JCheckBox wantFries = new JCheckBox("Do you want fries with that?");panel.add(wantFries); The screen will look l...
Previous lesson: 14.4 JTextAreaCourse contents JButton The JButton class displays a clickable button component with the text you specify: JButton button = new JButton("Please click me");panel.add(button); The above code result in:. Note that although you can click on the button and it gives the visual feedback of being pressed and released, ...
Previous lesson: 14.3 JTextFieldCourse contents JTextArea Whereas JTextField allows the user to enter text on a single line, the JTextArea class allows entry of text in a box consisting of multiple lines, which is useful for entering information such as an address, for example: JTextArea area = new JTextArea(4, 30);panel.add(area); The constr...
Previous lesson: 14.2 JLabelCourse contents JTextField The JTextField class allows the entry by the user of a piece of text in a box consisting of a single line: private JPanel buildUI() { JPanel panel = new JPanel(new BorderLayout()); // Components will go here... JTextField field = new JTextField(25); panel.add(field); // Return the built p...
Previous lesson: 14.2 How to use JFrame to provide a desktop window Course contents JLabel The JLabel class will show a read-only piece of text: private JPanel buildUI() { JPanel panel = new JPanel(new BorderLayout()); // Components will go here... JLabel label = new JLabel("This is a text label"); panel.add(label); // Return the built panel return...
Previous lesson: 14.1 Common GUI componentsCourse contents JFrame In the previous section you saw that the JFrame class can be used to provide a desktop window for your application. For the purposes of this section, you will create a new JFrame that will show various UI components. Define a class called DemoUI in the virtualzoo.ui package: &n...
In the previous lesson we defined separate listeners for each event. Previous lesson: 13.4 Separate listeners for each eventCourse contents Common GUI components The javax.swing package contains several useful graphical components that you can use in your applications. In this series of lessons you will learn: How to use JFrame to provid...
In the previous lesson we saw how to define inner classes. Previous lesson: 13.3 Inner classesCourse contents Separate listeners for each event As programs become more complex it can be helpful to define a separate listener class for each individual event rather than use just one listener to serve all. Step 1 Rename ButtonListener to C...