Java GUI Classes and Event Handling in CMSC 132: Object-Oriented Programming II - Prof. Ne, Study notes of Computer Science

This document from the university of maryland, college park's department of computer science outlines the java gui classes, container components, and event handling in the context of the cmsc 132 course. It covers awt, swing, and swt, as well as layout managers and event dispatching threads.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-oqx
koofers-user-oqx 🇺🇸

9 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CMSC 132:
Object-Oriented Programming II
Java Support for GUIs
Department of Computer Science
University of Maryland, College Park
2
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
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java GUI Classes and Event Handling in CMSC 132: Object-Oriented Programming II - Prof. Ne and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Java Support for GUIs

Department of Computer Science

University of Maryland, College Park

2

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

3

Java GUI Classes

AWT (Abstract Window Toolkit) (java.awt.*)

Old GUI framework for Java (Java 1.1) Some reliance on native code counterparts Platform independence problems

Swing (javax.swing.*)

New GUI framework first introduced in Java 1. Includes AWT features plus many enhancements Pure Java components (no reliance on native code) Pluggable look and feel architecture

SWT (Standard Widget Toolkit; from Eclipse)

4

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…

7

Java Components

JFrame

With three JButtons

8

JFrame Hierarchy

Several super classes As well as implemented interfaces

Many, many member methods Including inherited methods that allow Resizing Setting properties Adding components, Etc…

Other top level containers JDialog (dialog boxes) JApplet (web applets) JWindow (stripped down JFrame, no title bar or window buttons)

Object

Component

Container

Window

Frame

JFrame

9

JFrame Structure

Most things go into content pane getContentPane() Use glassPane for pop up menus, some animations Methods getRootPane() getLayeredPane() getContentPane() getGlassPane() Can set…Pane explicitly

glassPane

rootPane

JFrame

layeredPane

LayeredPane contains contentPane

LayeredPane manages (optional) JMenuBar

10

GUI Elements – Layout

Definition

Arrangement of GUI components in container

Layout specification

Logical terms (2nd^ row, 1st^ 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 actual coordinates at runtime, depending on conditions

13

Java Components

JPanel

14

Java Components

JTree

15

Java Components

JTable

16

Java Components

JTable

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

19

Event Dispatching Thread

Example code

Allows current thread to execute GUI code in dispatching thread createAndDisplayGUI Method that actually defines the GUI

javax.swing.SwingUtilities.invokeLater(new Runnable( ) { public void run( ) { createAndDisplayGUI( ); } });

20

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