Java programming course: 15.3 Modify the other core system classes

Previous lesson: 15.2 Making ZooAdministrator a singleton classCourse contents  Modify the other core system classes Using the approach given above, it is the intention that the user interface classes only communicate with the core system through the ZooAdministrator class. At the moment there is nothing to prevent a user interface class ...

Java programming course: 15.2 Making ZooAdministrator a singleton class

Previous lesson: 15.1 Access modifiers and visibilityCourse contents  Making ZooAdministrator a singleton class You have previously learned that to instantiate an object of any class you use the new keyword to invoke a class's constructor. For example, to instantiate some animals you may have done this: Animal leo = new Lion("Leo", Gender.MALE...

Java programming course: 15.1 Access modifiers and visibility

Previous lesson: 14.17 JOptionPaneCourse contents  Access modifiers and visibility Access modifiers enable you to protect and control parts of your application from inappropriate use by other parts or other applications. A singleton class is one where no more than one instance of that class can exist. In this section you will learn: An ov...

Java programming course: 14.17 JOptionPane

Previous lesson: 14.16 JDialogCourse contents  JOptionPane The JOptionPane class provides a simple way of displaying standard dialogs that show informational, warning or error messages through its static convenience methods. Change the ShowDialogButtonListener inner class as follows: private class ShowDialogButtonListener implements ActionList...

Java programming course: 14.16 JDialog

Previous lesson: 14.15 JTabbedPaneCourse contents JDialog The JDialog class displays a separate window which is "owned" by an application's frame. You can either add your own components to a dialog or use a simple pre-built one through the JOptionPane class. There follows an ActionListener inner class that builds and displays a JDialog consisting o...

Java programming course: 14.15 JTabbedPane

Previous lesson: 14.14 JSplitPaneCourse contents  JTabbedPane The JTabbedPane class displays separate titled "tabs", each of which allows the user to switch between by clicking on a tab: JPanel panelOne = new JPanel();panelOne.add(new JLabel("This is the first panel"));JPanel panelTwo = new JPanel();panelTwo.add(new JLabel("This is the second ...

Java programming course: 14.10 JSpinner

Previous lesson: 14.9 JSliderCourse contents  JSpinner The JSpinner class enables the user to "spin" through a range of items using small up and down arrow buttons: JSpinner spinner = new JSpinner();panel.add(spinner); By default, the spinner allows the spinning through an unbounded range of integer values, defaulting to zero. You can use the ...

Java programming course: 14.9 JSlider

Previous lesson: 14.8 JComboBoxCourse contents  JSlider The JSlider class allows the user to select a value, from within a range of values, by moving a sliding knob: JSlider slider = new JSlider();panel.add(slider); By default, the allowable range of values is from 0 to 100, with the knob pre-selected at 50. The default slider will look like t...

Java programming course: 14.8 JComboBox

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...

Java programming course: 14.7 JRadioButton

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...

Java programming course: 14.6 JCheckBox

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...

Java programming course: 14.5 JButton

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, ...

Java programming course: 14.4 JTextArea

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...

Java programming course: 14.3 JTextField

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...

Java programming course: 14.2 JLabel

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...

Java programming course: 14.1 JFrame

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...

Java programming course: 14 Common GUI components

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...

Java programming course: 13.4 Separate listeners for each event

 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...

Java programming course: 13.3 Using inner classes

 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...

Java programming course: 13.2 Containers and JPanel

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,...