Title: Components
1Components
- A Java GUI screen is built up using
components.Frames, Panels, Buttons, Labels,
TextAreas, TestAreas, ComboBoxes, Lists,
CheckBoxes, RadioButtons, Borders,
MessageDialogBoxes, Menus, RadioButtons,
ScrollBars, ScrollPanes and TabbedPanes. - Java Beans are just Java Components that meet
certain standards. Java Beans can be used in
other programming languages and in Visual IDEs.
All of the components listed above are also Java
Beans. - The components listed are all subclasses of the
Component class and as such they inherit a number
of properties and methods.
2Java Beans
- Components that have been set up as Java Beans
can be used in Drag and Drop form builders such
as Borlands JBuilder. They can also be used in
other languages such as Visual Basic and Delphi. - Java Components become Java Beans if they meet
three minimum requirements. - They must be public classes
- They must have a default constructor.
- They must implement the Serializable or
Externalizable interfaces.To be useful they
should also. . . - Provide public access to their properties through
getter and setter methods. - Provide methods for registration to Listeners.
3Component properties
All GUI objects are subclasses of the awt
Component classand as such inherit these
properties.
Note that Layout managers do not always respect
the last three.
4JComponent properties
- Swing components are subclasses of JComponent
which is itself a subclass of Component. Swing
components have these additional properties.
5Components(2)
- JButton
- JLabel
- JTextArea
- JTextArea
- JComboBox
- JList
- JCheckBox
- JRadioButton
- Border
- JMenuBar
- JScrollBar
- JScrollPane
- JTabbedPane
6JButton Event Handler ActionListener
7JLabel Event Handler None
8JTextField Events TextListener
ActionListener
9JTextArea Events TextListener
ActionListener
10JComboBox Events TextListener
ActionListener
11JList Events TextListener
ActionListener
12JCheckBox Events ItemListener
ActionListener
13JRadioButton Events ItemListener
ActionListener
14Radio Button Groups
Radio buttons are frequently displayed in groups,
only one of which can be selected at a time.
DVD
CD
Tape
Selecting a different one will automatically
de-select the other.Use a ButtonGroup component
as a container. Add the Radio Buttons to it then
add the group to the panelButtonGroup bg new
ButtonGroup() bg.add(radioBut1) bg.add(radioBut2
) bg.add(radioBut3)myPanel.add(bg)
15Borders
- Borders with a range of features can be applied
to any of the JComponents. - Border is the superclass for TitledBorder,
BevelBorder,EtchedBorder, LineBorder, MatteBorder
and EmptyBorder. - Constructor for TitledBorderBorder b new
TitledBorder(Title) - Use the setBorder method of the component to add
it to the Component
16BorderFactory
- A class with static methods to create borders.
- Some methods are.
- createTitledBorder(String title)
createLoweredBevelBorder() createRaisedBevelBord
er() createLineBorderBorder(Color c) etc. .
.
17Message Dialogs
- Used to Display a simple message and await user
input before closing. - Class JOptionPane has static methods that can be
called.JOptionPane.showMessageDialog(current
object, Message
Line, Box
title,
Message_Type) - Message type is a static constant in JOPtionBox
- ERROR_MESSAGEINFORMATION_MESSAGE
PLAIN_MESSAGE - Eg JOptionPane.showMessageDialog(this, You must
enter a number, Entry Error,JOptionPane.ERROR_
MESSAGE)
18Menus
- There are Three main levels
- JMenuBar. Create a JMenuBar object and add it to
the frame. - Jmenu. This is the menu object. Create it and add
it to the MenuBar. - JMenuItem. Each element in the menu is a menu
item. Create each item and add to the menu. - Sub menus can be added to a menu like items.