Docsity

Java GUI Components: Buttons, Labels, TextFields, ComboBoxes, Lists, Checkboxes, RadioButt, Slides of Computer Science

Partial preview of the text

Download Java GUI Components: Buttons, Labels, TextFields, ComboBoxes, Lists, Checkboxes, RadioButt and more Slides Computer Science in PDF only on Docsity!

Chapter 9:

Creating User Interfaces

  • What is JavaBean?
  • JComponent
  • Jbutton
  • ImageIcon
  • JLabel
  • JTextField
  • JTextArea
  • JComboBox
  • JList

 JCheckBox  JRadioButton  Menus  Creating Multiple Windows  JScrollBar  JScrollPane

What is a JavaBean?

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

JComponent Properties

  • toolTipText
  • font
  • background
  • foreground
  • doubleBuffered
  • border
  • preferredSize
  • minimumSize
  • maximumSize

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

Responding to JButton Events

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

JTextField Constructors

  • JTextField(int columns) Creates an empty text field with the specified number of columns.
  • JTextField(String text) Creates a text field initialized with the specified text.
  • JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size.

JTextField Methods

  • getText() Returns the string from the text field.
  • setText(String text) Puts the given string in the text field.
  • setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true.
  • setColumns(int) Sets the number of columns in this text field. The length of the text field is changeable.

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.

JTextArea Properties

  • text
  • editable
  • columns
  • lineWrap ( if a line can be wrapped)
  • wrapStyleWord (word or char boundary)
  • rows
  • lineCount
  • tabSize

Example 9.4 Using Text Areas

  • This example gives a program that displays an image in a label, a title in a label, and a text in a text area.

DescriptionPanel

    • jlblImagejlblTitle
  • jtaTextDescription
  • +setImageIconsetTitle
  • +setTextDescriptiongetMinimumSize

(^1 1) TextAreaDemo

JPanel JFrame

Run

TextAreaDemo

JComboBox Methods

To add an item to a JComboBox jcbo, use

jcbo.addItem(Object item)

To get an item from JComboBox jcbo, use

jcbo.getItem()

Using the

itemStateChanged Handler

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.