Chapter 6.6.2 – JLabel | Introduction to Programming Using Java

Chapter 6.6.2 – JLabel | Introduction to Programming Using Java

 

6.6.2 JLabel

 

JLabel is certainly the simplest type of component. An object of type JLabel exists just to display a line of text. The text cannot be edited by the user, although it can be changed by your program. The constructor for a JLabel specifies the text to be displayed:

 

Chapter 6.6.2 - JLabel | Introduction to Programming Using Java

 

There is another constructor that specifies where in the label the text is located, if there is extra space. The possible alignments are given by the constants JLabel.LEFT, JLabel.CENTER, and JLabel.RIGHT. For example,

JLabel message = new JLabel(“Hello World!”, JLabel.CENTER); creates a label whose text is centered in the available space. You can change the text displayed in a label by calling the label’s setText() method:

 

qq 3 Chapter 6.6.2 - JLabel | Introduction to Programming Using Java

 

Since the JLabel class is a subclass of JComponent, you can use methods such as setForeground() with labels. If you want the background color to have any effect, you should call setOpaque(true) on the label, since otherwise the JLabel might not fill in its background. For example:

 

Chapter 6.6.2 - JLabel | Introduction to Programming Using Java

 

 

6.6.3  JCheckBox

 

A JCheckBox is a component that has two states: selected or unselected. The user can change the state of a check box by clicking on it. The state of a checkbox is represented by a boolean value that is true if the box is selected and false if the box is unselected. A checkbox has a label, which is specified when the box is constructed:

 

Chapter 6.6.2 - JLabel | Introduction to Programming Using Java

 

Usually, it’s the user who sets the state of a JCheckBox, but you can also set the state in your program. The current state of a checkbox is set using its setSelected(boolean) method. For example, if you want the checkbox showTime to be checked, you would say “showTime.setSelected(true)”. To uncheck the box, say “showTime.setSelected(false)”. You can determine the current state of a checkbox by calling its isSelected() method, which returns a boolean value.

In many cases, you don’t need to worry about events from checkboxes. Your program can just check the state whenever it needs to know it by calling the isSelected() method. However, a checkbox does generate an event when its state is changed by the user, and you can detect this event and respond to it if you want something to happen at the moment the state changes. When the state of a checkbox is changed by the user, it generates an event of type ActionEvent.

If you want something to happen when the user changes the state, you must register an ActionListener with the checkbox by calling its addActionListener() method. (Note that if you change the state by calling the setSelected() method, no ActionEvent is generated. However, there is another method in the JCheckBox class, doClick(), which simulates a user click on the checkbox and does generate an ActionEvent.)

When handling an ActionEvent, you can call evt.getSource() in the actionPerformed() method to find out which object generated the event. (Of course, if you are only listening for events from one component, you don’t even have to do this.) The returned value is of type Object, but you can type-cast it to another type if you want.

Once you know the object that generated the event, you can ask the object to tell you its current state. For example, if you know that the event had to come from one of two checkboxes, cb1 or cb2, then your actionPerformed() method might look like this:

 

Chapter 6.6.2 - JLabel | Introduction to Programming Using Java

 

Alternatively, you can use evt.getActionCommand() to retrieve the action command associated with the source. For a JCheckBox, the action command is, by default, the label of the checkbox.

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | 

Chapter 5.6 – This and Super | 

Chapter 5.7 – Interfaces | 

Chapter 5.8 – Nested Classes |

Chapter 6 – Introduction to GUI Programming | 

1 thought on “Chapter 6.6.2 – JLabel | Introduction to Programming Using Java”

Leave a Comment