- Forward


An Introduction to Graphical User Interface (GUI) Technology
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

The Building Blocks
Back SMYC Forward
  • Components/Widgets:
    • The parts of a GUI (e.g., buttons, sliders, text fields)
  • Containers:
    • "Screen real estate" that contains components/widgets
Some Top-Level Containers (i.e., Realizations of RootPaneContainer )
Back SMYC Forward
  • JFrame :
    • A main "window" most commonly used by an application
  • JDialog :
    • A "child" window that is usually used for input/output
Creating a JFrame
Back SMYC Forward
javaexamples/gui/JFrameDriver.java
 
Working with Top-Level Containers
Back SMYC Forward
  • The JRootPane :
    • RootPaneContainer objects have a JRootPane that manages layers (if any), menus (if any), and content
    • Generally should not be used
  • The Content Pane:
    • A JRootPane has a Container called the content pane that manages all of the usable "screen real estate"
    • Accessed using the getContentPane() method
Some GUI Components
Back SMYC Forward
  • JButton :
    • button_windows
  • JCheckBox /JRadioButton :
    • radiobutton_windows
  • JTextField /JTextArea :
    • textfield_windows
  • JComboBox :
    • combobox_windows
  • JSlider :
    • slider_windows
GUI Layout
Back SMYC Forward
  • Organizing GUI components
  • Grouping GUI components
  • Positioning GUI components
Absolute Layout
Back SMYC Forward

The "Graph Paper" Approach

layout_absolute1
Absolute Layout in Java
Back SMYC Forward

The null LayoutManager

javaexamples/layout/NullLayoutDriver.java
 
Other Approaches to Layout (Not Considered Here)
Back SMYC Forward
  • Relative Layout:
    • Components are positioned relative to each other, rather than an absolute position
  • Template Layout:
    • Divide the container into sections and place one component in each section
  • Hierarchical Template Layout:
    • Subdivide the sections in a template layout
  • Constrained Template Layout:
    • Add constraints (e.g., doesn't expand) to sections of a template
Responding to GUI Components
Back SMYC Forward
  1. Determine what events the component generates
  2. Identify the interfaces that needs to be implemented
  3. Create a class that implements that interfaces
  4. Add "application logic" to the appropriate methods
Example: Responding to JButton Objects
Back SMYC Forward
  • The Event(s):
    • ActionEvent
  • The Interface(s):
    • ActionListener
  • The Method(s)/Message(s):
    • void actionPerformed(ActionEvent event)
Example: Responding to JButton Objects (cont.)
Back SMYC Forward
javaexamples/usinggui/ButtonHandler.java
 
Menus
Back SMYC Forward
  • JMenuBar:
    • Construct an empty JMenuBar object and then call its void add(JMenuItem item) method
    • JFrame has a void setJMenuBar(JMenuBar bar) method
  • Parts of a Menu:
    • menu-menuitem
Advanced Topics: Scrollable Containers
Back SMYC Forward
  • The Idea:
    • Use a "viewport" to make only a portion of their total area visible at a time
    • Enable the visible portion to change (i.e., appear to scroll)
  • In Java:
    • The Scrollable interface
    • A JScrollPane controls a Scrollable
Advanced Topics: Scrollable Containers (cont.)
Back SMYC Forward
scrollpane
Advanced Topics: Docking Toolbars
Back SMYC Forward
  • The Concept:
    • A container that can be repositioned
  • In Java:
    • JToolBar
Advanced Topics: Borders
Back SMYC Forward
  • Motivation:
    • Visually group components (e.g., a group of RadioButton objects)
  • In Java:
    • A Border can be applied to any JComponent
    • Because a large number are used they should be created using a BorderFactory
Advanced Topics: Layered Layout
Back SMYC Forward
  • Use the same "screen real estate" for multiple purposes
  • Put the different purposes on top of each other and provide a mechanism for moving between them
  • In Java use the JTabbedPane
There's Always More to Learn
Back -