Docsity

Slides on Java Support for GUIs - Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Partial preview of the text

Download Slides on Java Support for GUIs - Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Java Support for GUIs

Department of Computer Science

University of Maryland, College Park

Overview

Java GUI Classes Creating a GUI Elements of a GUI

Component

items displayed (widgets) Container

region containing components Layout

arrangement of components Event

interactions for GUI

Java GUI Classes

GUI classes can be organized in three groups

Component classes Items (widgets) displayed in GUI JButton, JTextField, JDialog, JScrollBar… Container classes Hold GUI Components JFrame, JPanel, JApplet… Helper classes Describe properties of other GUI components Color, Graphics, Dimension…

How to Create a GUI

Define Frame or Applet to hold components

We’ll just use frames for now

Add GUI components to Frame

Use layout manager to determine position

Add actions to GUI

By adding event listeners to GUI components

Java Components

JFrame

With three JButtons

JFrame Hierarchy Several super classes As well as implemented interfaces Many, many member methods Including inherited methods that al low Resizing Setting properties Adding components, Etc… Other top level containers JDialog (dialog boxes) JApplet (web applets) JWindow (stripped down JFrame, no titlebar or window buttons) Object Component Container Window Frame JFrame

GUI Elements – Layout

Definition

Arrangement of GUI components in container

Layout specification

Logical terms ( nd row, 1 st column, left) Preferred approach Actual coordinates (100 pixels, 5 inches) Can be too rigid, limited to certain window sizes

Layout manager

Entity translating layout specifications into actualcoordinates at runtime, depending on conditions

Java Layout Managers

FlowLayout

Lays out components from left to right

GridLayout

Lays out components in a grid of user specified size

BorderLayout

Designates portions of the container as North,South, East, West, and Center

CardLayout

Adds components one on top of another

GridBagLayout

Customizable manager that can use rows andcolumns of varying lengths

Java Components

JPanel

Java Components

JTree

Java Components

JTable

Each JTable object Gets its data from an object implementingTableModel interface Displays contents of TableModel object DefaultTableModel class implements TableModel Many different ways to use JTable to display data

GUI Elements – Events

Definition

Action or condition occurring outside normal flow ofcontrol of program

Examples

Mouse clicks Keyboard input Menu selections Window actions

In Java

GUI events handled in event dispatching thread

Event Dispatching Thread

Example code

Allows current thread to execute GUI code indispatching thread createAndDisplayGUI Method that actually defines the GUI javax.swing.SwingUtilities.invokeLater(new Runnable( ) { public void run( ) { createAndDisplayGUI( ); } });

Java Support For GUIs

Several GUI code examples Additional Resources

Appendix C of textbook Javadoc for the JDK Swing tutorial Course slides and code handouts Java Ranch