














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















































ļ JCheckBox ļ JRadioButton ļ Menus ļ Creating Multiple Windows ļ JScrollBar ļ JScrollPane
A JavaBean component is just a Java class that meets the following requirements.
Data members Methods Constructors (^) public class default public constructor serializable may have may have registration methods accessor methods
class JavaBean Minimum requirements Optional requirements
JButton
A button is a component that triggers an action event when clicked. The following are JButton non-default constructors:
JButton(String text)
JButton(String text, Icon icon)
JButton(Icon icon)
Example 9.1: Using Buttons
ButtonDemo Run
public void actionPerformed(ActionEvent e) { // Get the button label String actionCommand = e.getActionCommand(); // Make sure the event source is a button if (e.getSource() instanceof JButton) // Make sure it is the right button if ("My Button".equals(actionCommand) System.out.println ("Button pressed!"); }
JLabel
A label is a display area for a short text, an image, or both. The non-default constructors for labels are as follows:
JLabel(String text, int horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment)
Example 9.2: Using Labels
LabelDemo Run
JTextField
A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description).
Example 9.3: Using Text Fields
TextFieldDemo Run
JTextArea
If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea , which enables the user to enter multiple lines of text.
Example 9.4 Using Text Areas
DescriptionPanel
(^1 1) TextAreaDemo
JPanel JFrame
Run
TextAreaDemo
To add an item to a JComboBox jcbo, use
jcbo.addItem(Object item)
To get an item from JComboBox jcbo, use
jcbo.getItem()
public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem(); }
When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.