Chapter 6.7.3 – Slider And Combo Box Demo | Introduction to Programming Using Java

Chapter 6.7.3 – Slider And Combo Box Demo | Introduction to Programming Using Java

 

6.7.3 Slider And Combo Box Demo

 

Now that we have looked at components and layouts, it’s time to put them together into some complete programs. We start with a simple demo that uses a JLabel, a JComboBox, and a couple of JSliders, all laid out in a GridLayout, as shown in this picture:

 

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

The sliders in this applet control the foreground and background color of the label, and the combo box controls its font style. Writing this program is a matter of creating the components, laying them out, and programming listeners to respond to events from the sliders and combo box. In my program, I define a subclass of JPanel which will be used for the applet’s content pane.

This class implements ChangeListener and ActionListener, so the panel itself can act as the listener for change events from the sliders and action events from the combo box. In the constructor, the four components are created and configured, a GridLayout is installed as the layout manager for the panel, and the components are added to the panel:

 

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

The class also defines the methods required by the ActionListener and ChangeListener interfaces. The actionPerformed() method is called when the user selects an item in the combo box. This method changes the font in the JLable, where the font depends on which item is currently selected in the combo box, fontStyleSelect:

 

33 5 Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

And the stateChanged() method, which is called when the user manipulates one of the sliders, uses the value on the slider to compute a new foreground or background color for the label.

The method checks evt.getSource() to determine which slider was changed:

 

44 5 Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

(The complete source code is in the file SliderAndComboBoxDemo.java.)

 

6.7.4 A Simple Calculator

As our next example, we look briefly at an example that uses nested subpanels to build a more complex user interface. The program has two JTextFields where the user can enter two numbers, four JButtons that the user can click to add, subtract, multiply, or divide the two numbers, and a JLabel that displays the result of the operation:

 

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

Like the previous example, this example uses a main panel with a GridLayout that has four rows and one column. In this case, the layout is created with the statement:

 

66 5 Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

which allows a 3-pixel gap between the rows where the gray background color of the panel is visible. The gray border around the edges of the panel is added with the statement setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );

The first row of the grid layout actually contains two components, a JLabel displaying the text “x =” and a JTextField. A grid layout can only only have one component in each position. In this case, that component is a JPanel, a subpanel that is nested inside the main panel. This subpanel in turn contains the label and text field. This can be programmed as follows:

 

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

The subpanel uses the default FlowLayout layout manager, so the label and text field are simply placed next to each other in the subpanel at their preferred size, and are centered in the subpanel.

Similarly, the third row of the grid layout is a subpanel that contains four buttons. In this case, the subpanel uses a GridLayout with one row and four columns, so that the buttons are all the same size and completely fill the subpanel.

One other point of interest in this example is the actionPerformed() method that responds when the user clicks one of the buttons. This method must retrieve the user’s numbers from the text field, perform the appropriate arithmetic operation on them (depending on which button was clicked), and set the text of the label to represent the result. However, the contents of the text fields can only be retrieved as strings, and these strings must be converted into numbers. If the conversion fails, the label is set to display an error message:

 

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

Chapter 6.7.3 - Slider And Combo Box Demo | Introduction to Programming Using Java

 

(The complete source code for this example can be found in SimpleCalc.java.)

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

 

1 thought on “Chapter 6.7.3 – Slider And Combo Box Demo | Introduction to Programming Using Java”

Leave a Comment