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...
In the previous lesson we looked at containers and JPanel. Previous lesson: 13.2 Containers and JPanelCourse contents Using Inner classes Most classes you write are top-level classes, that is, independent classes in its own right and in their own class file. It is often useful to create inner classes, being classes that exist inside of anothe...
In the previous lesson we introduced graphical user interfaces. Previous lesson: 13.1 Graphical user interfacesCourse contents Containers and JPanel Because the Container object can contain other components, including other containers, it is possible to create some very sophisticated layouts. Rather than place your components directly onto a frame,...
The previous lesson discussed thread waiting and notification. Previous lesson: 12.4 Thread waiting and notificationCourse contents Graphical user interfaces A Graphical User Interface provides the primary means by which users of your application interact with it. In this section you will learn: The structure of graphical programsSome common ...
In the previous lesson you saw how to implement the Runnable interface. Previous lesson: 12.3 Implementing the Runnable interfaceCourse contents Thread waiting and notification For some types of multithreaded application, using the synchronized keyword alone is not enough on its own. Imagine that at the zoo the penguins are fed fish from a ba...
In the previous lesson we showed how to extend the Thread class. Previous lesson: 12.2 Extending the Thread classCourse contents Implementing the Runnable interface You have seen that you can extend the Thread class to define a class which can run concurrently with other threads, but what if the class you want to make multithreaded already extends ...
In the previous lesson we learnt about the concepts of multithreading. Previous lesson: 12.2 MultithreadingCourse contents Extending the Thread class The first way of defining a multithreaded class is to extend the Thread class and override its run() method. Here is the Booker class which does just that: package virtualzoo.core;public class Booker ...
In the previous lesson we saw how to use Map. Previous lesson: 11.4 using MapCourse contents Multithreading Threads allow your application to perform more than one process concurrently. In this section you will learn: The principles of multi-threadingExtending the Thread classImplementing the Runnable interfaceUsing wait() and notifyAll() to ...
In the previous lesson we looked an maps. Previous lesson: 11.3 MapsCourse contents Using a Map for zookeeper's responsibilities At the zoo, each zookeeper is responsible for certain pens, as listed below: Alice looks after the monkeys and penguinsBob looks after the lions, monkeys and penguinsCharles only looks after the penguins This can be...
In the previous lesson we used our Pen class in ZooAdministrator. Previous lesson: 11.2 Using Pen in ZooAdministratorCourse contents Maps You can think of a Map as being like a dictionary – you can look up a word to find its definition. But maps in Java are much more flexible than that since you can use them to look up any object to fin...
In the previous lesson we looked at lists and set. Previous lesson: 11.1 Lists and SetsCourse contents Using the Pen class in ZooAdministrator The zoo administrator will keep a record of each pen in the zoo, being one for each of the animal types. In the ZooAdministrator class define these Pen instance variables: private Pen lionPen, monkeyPe...