Unit 6 A Review of GUI Programming in Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Unit 6 A Review of GUI Programming in Java

Description:

Elliott Evans Slides on GUIs in Java. 1. Unit 6 A Review of GUI Programming in Java ... For example, you can deactivate mouse events for a multi-component region by ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 26
Provided by: Elli157
Category:

less

Transcript and Presenter's Notes

Title: Unit 6 A Review of GUI Programming in Java


1
Unit 6 A Review of GUI Programming in Java
  • AWT, Swing, Layout Managers, and more.

2
java.lang.Object
AWT Inheritance Hierarchy for Containers and
Components
GUI Control Components are concrete
subclasses of this class (these include
Button, Canvas, CheckBox, Label, List,
TextComponent (its children are TextField,
TextArea), and more!
Container add(Component) setLayout(LayoutManager)

?
Panel (FlowLayout)
Window
Frame (a typical top level Window for
an Application)
java.applet.Applet
Dialog
3
Some Non-container Children of Component
4
About Menus in AWT
Frame
java.lang.Object
MenuBar
MenuComponent
Menu
Menu
MenuItem
MenuBar
MenuItem
MenuItem
Menu
Inheritance Hierarchy
Containment Hierarchy
  • You create a MenuItems and gather them together
    in Menus. Then you create a MenuBar and place
    the Menus in it.
  • MenuItems generate ActionEvents when they are
    selected.

5
Layout Managers in AWT
  • Layout Managers can be applied to any Container
  • using setLayoutManager(LayoutManager mgr)
  • Layout manager tells how components added in a
    container are placed, positioned, sized, etc. as
    the visible container is being laid out.
  • FlowLayout
  • No-argument constructor and one that can specify
    horizontal and vertical gaps (int)
  • Components added are placed in rows that flow
    from left to right
  • Rows flow from top to bottom
  • Resizing can really affect the layout and
    appearance.
  • GridLayout
  • GridLayout(rows, cols) or GridLayout(rows, cols,
    hgap, vgap)
  • Divides region into rectangular grid
  • One component per cell
  • All cells have exactly same size components
    resized to fit the cell (doesnt respect set
    sizes)
  • Either rows or cols can be zero, but not both!
  • If one is zero, non-zero value dominates
  • GridBagLayout fully customizable control of GUI
    layout very complex!

6
Layout Managers in AWT (continued)
  • CardLayout
  • Each added component is a new layer (3D) and
    you can control the layer that is visible
  • BorderLayout
  • Divides region into NORTH, SOUTH, EAST, WEST,
    CENTER sub-regions
  • Can be referred to with Strings North, South,
    Center, etc.
  • add( ) method will need a second argument
    specifying position like add(component,
    BorderLayout.NORTH)
  • Without second argument, add( ) always puts in
    center

7
Example of Layouts Containment Hierarchy
Another Meaning of the Word Child
Consider the example code on our web page
at http//clem.mscd.edu/evansell/LECTURES/Layout
App.java.txt Try resizing the window and see what
the layout managers do.
8
Example of Menus and CardLayout Containment
Hierarchy
Consider the example code on our web page
at http//clem.mscd.edu/evansell/LECTURES/MenuTe
st.java.txt
Frame F
MenuBar
MenuTest (Panel)
Menu
?
MenuItem
MenuItem
?
LayoutPic (Panel) - 1
LayoutPic (Panel) - 2
LayoutPic (Panel) - 5
CardLayout Mgr
9
Swing New, Improved GUI Tools
  • Started with Java 1.2 can be used instead of
    much of AWT but still uses and depends upon some
    of AWT
  • Pretty much every AWT element has a Swing
    corresponding element, named with J preceding
    traditional name (Button ? JButton, etc.)
  • Swing stuff in java packages javax.swing,
    javax.swing.plaf, javax.swing.border, and more.
  • Swing still uses all the basic principles of
    building GUIs with containers, components,
    layouts, and the same event listening and event
    handling that AWT uses.
  • Swing distinguishes between top level containers,
    other containers, and components in the GUI
    containment hierarchy
  • Also, Swing has a lot of improved and useful
    extra capabilities that AWT doesnt have

10
Swing Specialties
  • More dynamic components through use of
    delegation objects (objects which do separable
    work from main GUI component)
  • Delegation model GUI components uses a distinct
    object to carry out special work for it (like
    setting its Look and Fell LF)
  • New containment model for root containers
    JFrame, JApplet, JDialog, etc. have delegates
    that aggregate useful objects
  • Pluggable Look and Feel can masquerade in
    native appearance for a lot of platforms
  • New type of component, JComponent, with improved
    extensible painting and with implicit
    double-buffering.
  • Support for Accessibility to support hardware
    and software for persons with special needs.
  • New Layout Manager BoxLayout Manager
  • New complex components AWT doesnt have JTree,
    JTable, JFileChooser, etc.)
  • Only JFrame, JDialog, JWindow are top level
    components. Only these and JApplet and
    JInternalFrame are treated as root containers.

11
The Root Pane Container Model
JFrame
JRootPane ------------------------------ contentP
ane glassPane menuBar
JLayeredPane
Content pane
JWindow
Container
JDialog
JMenuBar
JApplet
Component
JInternalFrame
Glass pane
12
More on Panes
  • A root pane has four parts
  • The glass pane
  • Hidden, by default. If you make the
    glass pane visible, then it's like a sheet of
    glass over all the other parts of the root pane.
    It's completely transparent unless you implement
    the glass pane's paint method so that it does
    something, and it intercepts input events for the
    root pane.
  • The layered pane
  • Serves to position its contents, which
    consist of the content pane and the optional menu
    bar. Can also hold other components in a
    specified Z order, giving 3D effects of overlay.
  • The content pane
  • The container of the root pane's visible
    components, excluding the menu bar.
  • -- The optional menu bar
  • The home for the root pane's container's
    menus. If the container has a menu bar, you
    generally use the container's setJMenuBar method
    to put the menu bar in the appropriate place.
  • More on the Glass Pane
  • The glass pane is useful when you want to be able
    to catch events or paint over an area that
    already contains one or more components. For
    example, you can deactivate mouse events for a
    multi-component region by having the glass pane
    intercept the events. Or you can display an image
    over multiple components using the glass pane.

13
Using Top Level Containers
  • To appear onscreen, every GUI component must be
    part of a containment hierarchy. Each containment
    hierarchy has a top-level container as its root.
  • Each top-level container has a content pane that,
    generally speaking, contains the visible
    components in that top-level container's GUI.
  • You can optionally add a menu bar to a top-level
    container. The menu bar is positioned within the
    top-level container, but outside the content
    pane.
  • Each program that uses Swing components has at
    least one top-level container. This top-level
    container is the root of a containment hierarchy
    the hierarchy that contains all of the Swing
    components that appear inside the top-level
    container. As a rule, a standalone application
    with a Swing-based GUI has at least one
    containment hierarchy with a JFrame as its root.
    For example, if an application has one main
    window and two dialogs, then the application has
    three containment hierarchies, and thus three
    top-level containers. One containment hierarchy
    has a JFrame as its root, and each of the other
    two has a JDialog object as its root.
  • A Swing-based applet has at least one containment
    hierarchy, exactly one of which is rooted by a
    JApplet object. For example, an applet that
    brings up a dialog has two containment
    hierarchies. The components in the browser window
    are in a containment hierarchy rooted by a
    JApplet object. The dialog has a containment
    hierarchy rooted by a JDialog object.

14
Using Top Level Containers - continued
  • Adding Components to the Existing Content Pane
  • frame.getContentPane().add(yellowLabel,
    BorderLayout.CENTER)
  • Making your own Panel and setting it as content
    pane
  • JPanel contentPane new JPanel()
    contentPane.setLayout(new BorderLayout())
    contentPane.setBorder(someBorder)
    contentPane.add(someComponent, BorderLayout.CENTER
    ) contentPane.add(anotherComponent,
    BorderLayout.SOUTH) topLevelContainer.setContentP
    ane(contentPane)

15
Very Simple Example
16
Root Container Hierarchy
java.awt
Component
java.applet
Frame
Window
Container
Applet
Dialog
javax.swing
JFrame
JWindow
JApplet
JComponent
JDialog
ltltInterfacegtgt RootPaneContainer
JInternalFrame
17
JComponent
  • The base class for both standard and custom
    components that use the Swing architecture all
    Swing components that are not top level inherit
    from it.
  • Endows children components with Container
    properties - different from AWT can add
  • A "pluggable look and feel" (LF) that can be
    specified by the programmer or (optionally)
    selected by the user at runtime. The look and
    feel for each component is provided by a UI
    delegate -- an object that descends from
    ComponentUI.
  • Comprehensive keystroke handling.
  • Support for tool tips -- short descriptions that
    pop up when the cursor lingers over a component.
  • Support for accessibility. JComponent contains
    all of the methods in the Accessible interface,
    but it doesn't actually implement the interface.
    That is the responsibility of the individual
    classes that extend JComponent.
  • Support for component-specific properties.
  • An infrastructure for painting that includes
    double buffering and support for borders.
    Includes improved painting method
    paintComponent(Graphics). Best to call parent
    version (first) if you override.
  • An abstract class!

18
JPanel, Labels, Buttons, etc.
  • JPanel generic lightweight rectangular
    container, just like Panel in AWT. Built-in
    double buffering and flow layout.
  • JLabel generic text labeling used for pieces of
    a GUI. Can have text string and image
  • JButton generic button generating action event.
    Can have text string and image
  • JMenuBar a menu bar that can contain JMenu
    components
  • JTextComponents (JTextField, JTextArea)
    editable text fields in GUI where user can enter
    and edit text. Methods setText( ), getText( ),
    getDocument( ). Generate action events.
  • JList list of items a user can select from
  • JScrollBar a scroll bar used with scroll panes
  • JSeparator a simple component to provide visual
    separator line between components
  • Many more!!

19
Label and Button Component Ancestry
JComponent
?
JLabel
AbstractButton
JPanel
JToggleButton
JButton
JMenuItem
JCheckBox
JRadioButton
JRadioButtonMenuItem
JCheckBoxMenuItem
JMenu
20
A Swing Example with JMenus, JButtons, Glass
Panes, etc.
  • Consider the example code on our web page at
  • http//clem.mscd.edu/evansell/LECTURES/RootPaneCo
    ntain.java.txt
  • Builds Multiple Swing Menus in one menu bar
    gets their input see menu hierarchy below
  • Uses Swing buttons gets their input
  • Shows the glass pane of the root pane!
  • Uses paintComponent()
  • Uses anonymous class for glass pane

21
Example List to Choose Look and Feel
  • Consider the example code on our web page at
  • http//clem.mscd.edu/evansell/LECTURES/ListnLF.ja
    va.txt
  • Lists all possible Java LFs, reveals
    cross-platform LF and that for this system
  • Presents a JList of all of these and allows user
    to select one
  • Then changes the programs LF based on the
    choice
  • Shows the list data model separate from the list
    shows how to build the list from the model
  • Shows how list selection events can be received
    and handled how selection is determined and how
    list model is used.
  • Shows how to change LF in an already running
    program

22
Boxes and Box Layout
  • Swing adds new layout manager BoxLayout this
    creates a grid type layout with one row or one
    column
  • The constants BoxLayout.X_AXIS and
    BoxLayout.Y_AXIS tell, respectively, whether it
    is one row or one column
  • Picture of outer panel with horizontal box
    layout, containing two panels each with vertical
    box layout
  • Constructor BoxLayout(Container target, int
    axis)
  • Respects preferred and maximum sizes simply add
    components to container and box layout will put
    then in the next position of the row or column
  • Can insert rigid or glue space between components
  • Demo code http//clem.mscd.edu/evansell/LECTURES/
    BoxLTest.java.txt
  • Swing has lightweight container (Jcomponent)
    called Box which has default Box layout
    (Box(int axis)). Box has useful static methods
  • static Component createVerticalGlue( )
  • static Component createHorizontalGlue( )
  • static Component createRigidArea(Dimension d)
  • Execute example code and see the glue effects
    (Example code also sets Borders)

23
Putting Swing Things Together to Build an
Application
  • Demo code at http//clem.mscd.edu/evansell/LECTUR
    ES/TextBox.java.txt
  • This is a simple text editor program that makes
    many points. Program is a class TextBox that is
    a type of JPanel (txbx) that is put into a JFrame
    window.
  • Uses a visible JTextArea (bigTex) to do editing
    in.
  • This is supported by a separate Document object,
    which actually holds the text content (is the
    real buffer)
  • To do some things (like to see what text is
    stored in the buffer) we need to access the
    document behind the JTextArea
    (bigTex.getDocument() returns a document object
  • Document can set DocumentListener to allow
    program to hear about insertions and deletions
  • JTextArea is a subclass of JTextComponent, which
    has many useful methods we can call
  • getSelectedText( ), cut( ), paste( ), getText( ),
    selectAll( )
  • addCaretListener( CaretListener) to hear about
    all select actions
  • Uses inner classes for Listeners
    (DocumentListener, CaretListener, ActionListener)
  • These have access to the variable and methods of
    the nesting class TextBox
  • TextBox uses Box Layout horizontal, with rigid
    spaces inserted on either side of text area
  • JTextArea (bigTex) is actually embedded in a
    scroll pane
  • JScrollPane scrollPane
  • new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AL
    WAYS,
  • JScrollPane.HORIZONTAL_
    SCROLLBAR_AS_NEEDED)
  • scrollPane.getViewport().setView(bigTex)

24
Putting Swing Things Together to Build an
Application - continued
  • Program uses temporary modal dialog boxes to
    report problems to user
  • JOptionPane.showMessageDialog(JFrame, String)
  • Program supports File IO
  • Uses File Dialogs to find out which file and
    directory are needed modal (synchronous)
  • Uses File Stream IO with buffered readers and
    writers
  • Heavily Uses Menus
  • Uses a separate class MenuFactory to make full
    menu bar, menus and menu items (see how the
    program passes info to the factory class
  • MenuFactory code at http//clem.mscd.edu/evansel
    l/LECTURES/MenuFactory.java.txt
  • Action Listener is for Menu events

25
Putting Swing Things Together to Build an
Application GUI Containment Picture
JFrame - myF
Container content pane
JPanel - txbx
BoxLayout Manager (invisible)
Component Rigid Area
ScrollPane (scrollPane)
Component Rigid Area
JTextArea bigTex
Document (has text content)
Write a Comment
User Comments (0)
About PowerShow.com