Title: Understanding Swing Components
1Understanding Swing Components
- Java Chapter 13
- Dr. James Jiang
2In this chapter, you will
- Use the JFrame class methods
- Use Swing event listeners
- Use JPanel class methods
- Use the JCheckBox class
- Use the ButtonGroup classes
- Create a drop-down list and combo box using the
JComboBox class - Create JScrollPanes
- Create JToolBars
3Using the JFrame Class
- GUI Components
- import javax.swing.
- Buttons, check boxes, are defined in the
Component class - Components widgets - windows gadgets
- You usually place components in the Containers
class - Container- type of component that holds other
components. - Usually takes the form of a window
- Defined in the Container class
- Use container to treat components as a single
entity
4Using the JFrame
- The JFrame class is a Component class
- The JFrame is the best container option
- The setSize()
- The setVisible()
- The syntax to use any of these methods is
- JFrame object, a dot, and the method name
- JFrame aFrame new JFrame(This is a frame)
- aFrame.setSize(200, 100)
- aFrame.setVisible(true) (see p.468,
Figure 13.7)
5(No Transcript)
6Using Additional JFrame Class Methods
- A JFrames action in response to a user clicking
-- is set by passing an argument to the
setDefaultCloseOperation() method placed inside
the JFrame constructor method - EXIT_ON_CLOSE exists the program
- DISPOSE_ON_CLOSE closes the frame, disposes of
the JFrame object, and keeps running the
application - DO_NOTHING_ON_CLOSE keeps the JFrame and
continues running - HIDE_ON_CLOSE closes the JFrame and continues
running
7Using Swing Event Listeners
- Classes that respond to user events must
implement an interface that deals with the events - These interfaces are called event listeners
- Each listener can handle a specific event type
- A class can implement as many event listeners as
needed
8(No Transcript)
9(No Transcript)
10Using Swing Event Listeners
- When a user event takes place, the appropriate
method is automatically called. -
- public void actionPerformed (ActionEvent event)
- Object source event.getSource( )
- if (source answerButton)
- //take answerButton action
- else if (source noresponseButton)
- // take nonResponseButton action
- else
- // take some other action
-
11Using JPanel Class Methods
- JPanel is the simplest Swing container add
other components directly. - Steps
- Create a JPanel object
- Add components to the JPanel using the add()
method and the component as the argument - Call the setContentPane() method with the JPanel
object created to set the application's content
pane
12JPanel example
- JPanel pane new JPanel( )
- JButton button JButton(Exit)
- pane.add(button)
- setContentPane(pane)
13Using JPanel Class Methods
- Swing components have a default size for the
objects created - A components size can be changed using the
components setPreferredSize() method - Dimension buttonSize new Dimension(200, 20)
- JButton bigButton new JButton()
- bigButton.setPreferredSize(buttonSize)
14Creat a JFrame with two JButtons within a JPanel
(i)
- Import javax.swing.
- Import java.awt.
- Import java.awt.event.
- Public class JChangeMessage extends JFrame
implement actionListener -
- JButton b1 new JButton(North)
- Button b2 new JButton(South)
- BorderLayout aBorder new Borderlayout()
- public JChangeMessage ( )
- super(Change Message)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E) - JPanel pane new JPanel( )
- pane.setLayout(aBorder)
- setContentPane(pane)
-
15Creat a JFrame with two JButtons within a JPanel
(ii)
- pane.add(North, b1)
- pane.add(South, b2)
- b1.addActionLister(this)
- b2.addActionLister(this)
- public static void main(String args)
- JFrame aFrame new JChangeMessage()
- aFrame.setSize(275, 100)
- aFrame.setVisible(true)
16Creat a JFrame with two JButtons within a JPanel
(iii)
- public void actionPerformed(ActionEvent e)
-
- Object source e.getSource( )
- if (source b1)
- b1.setText(Event Handlers
Incorporated) - else if (source b2)
- b2.setText(Works fine!)
-
17Using the JCheckBox Class
- JCheckBox Class- Consists of a JLabel positioned
beside a square - You can click the square to display or remove a
check mark - JCheckBox cooktailBox new JCheckBox(Cocktails,
false) - JCheckBox dinnerBox new JCheckBox(Dinner,
true)
18(No Transcript)
19ItemsStateChanged Event
- public class JDemoCheckBox extends JFrame
implements ItemListener -
- public void itemStateChanged(ItemEvent e)
- Object source e.getItem( )
- if (source checkBox1)
- int select e.getStateChange( )
- if (select ItemEvent.SELECTED)
- // some statement regarding
selected - else
- // some actions regarding the
dis-selected -
- if (source checkBox2)
-
-
-
20Using the ButtonGroup Classes
- ButtonGroup Classes
- can group several JCheckBoxes so a user can
select only one at a time - all other JCheckBoxes are automatically turned
off when the user selects any one checkbox - Either create a ButtonGroup and then create the
individual JCheckBoxes, or you can create the
JCheckBoxes and then create the Button group
21ButtonGroup examples
- JCheckBox beefBox new JCheckBox(Beef, false)
- JCheckBox fishBox new JCheckBox(Fish, false)
- ButtonGroup dinnerGroup new ButtonGroup()
- dinnerGroup.add(beefBox)
- dinnerGroup.add(fishBox)
- pane.add(beefBox)
- pane.add(fishBox)
- If dinnerGroup.isSelected() // to test if
anyone is selected - dinnerGroup.setSelected(beefBox) // set beefBox
to be selected
22Example (p.475-484)
- public class JDemoButtonGroup extends JFrame
implements itemListener -
- JCheckBox dinnerBox new JCheckBox(Dinner,
true) - JCheckBox beefBox new JCheckBox(Beef,
false) - JCheckBox fishBox new JCheckBox(Fish,
false) - JLabel ePrice new JLabel(Event Price
Estimate) - JLabel totPrice new JLabel( The estimated
total price is ) - public void main(String args)
- JFrame aFrame new JDemoButtonGroup( )
- aFrame.setSize (200, 150)
- aFrame.setVisible(True)
-
-
23Continue
- Public JDemoButtonGroup( )
- super(Check Box)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
- JPanel pane new JPanel()
- pane.setLayout(new FlowLayout() )
- setContentPane(pane)
- pane.add(dinnerBox)
- pane.add(beefBox)
- pane.add(fishBox)
- pane.add(ePrice)
- pane.add(totPrice)
24Continue
- ButtonGroup dinnerGroup new ButtonGroup( )
- dinnerGroup.add(dinnerBox)
- dinnerGroup.add(beffBox)
- dinnerGroup.add(fishBox)
-
- dinnerBox.addItemListener(this)
- beefBox.addItemListener(this)
- fishBox.addItemListener(this)
25Continue
- public void itemStateChanged(ItemEvent check)
- Object source check.getItem( )
- if (source dinnerBox)
- int select check.getStateChange( ) //
retrieve the check status - if (select ItemEvent.SELECTED)
- totPrice.setText(Total price is
500) - else if (select ItemEvent.DESELECTED)
- totPrice.setText(Total price is
) -
- if (source fishBox)
-
- if (source beefBox)
-
26Creating a Drop-Down List and Combo Box Using the
JComboBox Class
- ComboBox - for picking items from a list or
entering text into a field - Drop-down list -- a choice list
27Creating a Drop-Down List and Combo Box Using the
JComboBox Class
- You can build a JComboBox by using a constructor
with no arguments - Then adding items to the list with the addItem()
method - JComboBox majorChoice new JComboBox( )
- majorChoice.addItem(Management Information
Systems) - majorChoice.addItem(Education)
- majorChoice.addItem(Marketing)
- majorChoice.addItemListener(this) // implement
event handler - pane.add(majorChoice) // add the drop-down list
to the JPanel container - majorChoice.setEditable(true) // Combox allow
user to enter text
28(No Transcript)
29More examples
- public void itemStateChanged (ItemEvent list)
- Object source list.getSource( )
- if (source favorBox ) //
favorBox is a ComboBox -
- int favorNum favorBox.getSelectedIn
dex( ) - totalPrice favorPricefavorNum
-
-
-
- Where int favorPrice 0, 725, 325, 125,
135 - int totalPrice 0
30Creating JScrollPanes
- A JScrollPane is a container whose methods can be
used to hold any component that can be scrolled - Add a JTextArea component to use both multiple
rows and columns
31JScrollPane forms
- The JScrollPane constructor takes one of four
forms - JScrollPane() creates an empty JScrollPane where
both horizontal and vertical scrollbars appear
when needed - JScrollPane(Component) creates a JScrollPane that
displays the contents of the specified component - JScrollPane(Component, int, int) creates a
JScrollPane that displays the specified
component, vertical scrollbar, and horizontal
scrollbar. - JScrollPane(int, int) creates a scroll pane with
specified vertical and horizontal scrollbars
32Horizontal and vertical scrollbar constants
- HORIZONTAL_SCROLLBAR_AS_NEEDED
- HORIZONTAL_SCROLLBAR_ALWAYS
- HORIZONTAL_SCROLLBAR_NEVER
- VERTICAL_SCROLLBAR_AS_NEEDED
- VERTICAL_SCROLLBAR_ALWAYS
- VERTICAL_SCROLLBAR_NEVER
- JTextArea area new JTextArea(10, 40)
- JScrollPane scroll new JScrollPane (area,
VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_N
EVER)
33More examples
- JTextArea area new JTextArea(10, 40)
- JScrollPane scroll new JScrollPane(area)
-
- pane.add(scroll)
- setContentPane(pane)
-
- Where JPanel pane new JPanel()
34Creating JToolBars
- A Swing GUI can be designed so that a user can
move a toolbar from one section of a graphical
user interface to another section - This type of toolbar is called dockable toolbar
- The process that allows you to move and then
attach the toolbar is called docking - A toolbar is created in Swing with the JToolBar
class
35Creating JToolBars
- Constructor methods for the JToolBar class
include the following - JToolBar() creates a new toolbar that will line
up components in a horizontal direction - JToolBar(int) creates a new toolbar with a
specified orientation of horizontal or vertical - JToolBar tbar new JToolBar(SwingConstants.VERTIC
AL) - JButton b1 new JButton()
- Tbar.add(b1) // add items to a JToolBar
36Examples
- BorderLayout bord new BorderLayout( )
- JToolBar tbar new JToolBar(SwingConstants.VERTIC
AL) - ImageIcon image1 new ImageICon(dining.gif)
- JButton b1 newJButton(Dinning, image1)
- tbar.add(b1)
- b1.addActionListener(this) // event handling
- (see page 495)
- pane.setLayout(bord)
- pane.add(tbar, BorderLayout.NORTH)
37More examples
- public void actionPerformed(ActionEvent e)
- Object source e.getSource()
- if (source b1)
- edit.append(This is Button 1
Example) - if (source b2)
- edit.append(This is Button 2 Example)
-
- Where JTextArea edit new JTextArea(8, 30)