Docsity

AWT and Swing-Java Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download AWT and Swing-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

AWT and Swing

 Most GUI class libraries in C++ are platform specific

 Different hardware capabilities

 Subtle differences between the "look-and-feel" of various

Windowing operating systems

 Abstract Window Toolkit (AWT) is cross-platform

 Swing can observe various OS look-and-feel conventions

Common functionality/specific implementation approach

Toolkit -------------------------------------------------- AWT

Button List JVM

| | Native GUI

Button Peer List Peer (Windows, Mac, X)

 AWT GUI classes are platform-independent elements

 Each AWT platform-specific toolkit comes with peer class

implementing platform-specific behavior of its AWT class

 Combining platform-independent AWT class with

platform-specific peer class transforms generic, abstract windows

behavior into specific, particular behavior

JDK 1.0 (circa 1996)

 JDK 1.0 went a long way to implementing

platform-independent GUI library

 Bruce Eckel: it "produced a GUI that looks

equally mediocre on all systems."

 Just 4 fonts

 Couldn‟t access GUI of native OS

 Didn‟t separate model and UI code cleanly

JDK 1.1 (circa 1998)

 JDK 1.1 makes AWT more robust and extensible

 Delegation-based event model separates user

interface from problem domain

 Avoids cascaded if statements testing for object type

required by first AWT

 Designates "listeners" of events triggered by problem

domain objects

 Listeners implement the Observer design pattern

 Other enhancements: button tool tips, cut/paste

to the clipboard, popup menus, printing, etc.

 Adds supports for JavaBeans

Graphical Components

button menus title bar menu bar combo box

scroll bars

AWT class hierarchy

Checkbox, Choice, Label, List, Scrollbar,ScrollPane, TextArea, TextField

Window and Frame classes

 A Window is a top-level window with no borders and no menubar

 It can generate a WindowOpened or a WindowClosed event,

to which a WindowListener or WindowAdapter can respond

 A Frame is a top-level window with a title and a border

 Because it has more features, it can generate more events:

WindowOpened, WindowClosing, WindowClosed,

WindowIconified, WindowDeiconified,

WindowActivated, WindowDeactivated

 Respond to these events with a WindowListener

 Once a subclass of Container has been constructed, it can add

(attach) any AWT component within it, such as a Button, Label,

TextField, or another Frame or Panel

A simple example

//Demonstrates construction of a Container and a Button

import java.awt.*;

class Gui extends Frame

{ public Gui(String s) //constructor { super(s); //construct Frame part of Gui setBackground(Color.yellow); setLayout(new FlowLayout()); Button pushButton = new Button("press me"); add(pushButton); }

} //class Gui

class Ex_1 //Creates an instance of class Gui

{ public static void main(String[] args)

{ Gui screen = new Gui("Example 1"); screen.setSize(500,100); screen.setVisible(true); }

} //class Ex_

Superclass does not most of the work
of creating an instance of Gui. Modify properties of Gui.
Create a button and attach it to Gui.
Construct a Gui, set its size
and make it visible.
What does this program not do?docsity.com

Responding to events, continued

 Uses event delegation model of JDK 1.

 When an event occurs, it generates an ActionEvent object

 ActionListener interface listens for a particular ActionEvent  Responds in its actionPerformed method

 WindowListener interface observes events triggered by

Window object, such as closing it, and responds in

corresponding methods

 Program now has a live Button: actionPerformed method

rings a bell

 Also a live close window button, which performs System.exit(0)

 Most Components in the AWT have corresponding Listeners

Adapter Classes

 Time consuming to define all interface methods

 WindowListener has seven methods

  • What if we only want to use one?
  • Required to define all methods in interface

 Adapter class implements an interface

  • Does anyone recognize a design pattern here?
  • Default implementation ({ }, empty body) for all

methods

 You then extend adapter class,

  • overriding methods for events you care about, such as

windowClosing.

 Has "is a" relationship with interface

  • WindowAdapter is a WindowListener
  • MouseAdapter is a MouseListener

Layout managers

 JDK provides a set of genericSketchpad uses hard-coded layout, which depends on a 800x600 screen layout manager classes

  • Arrange Component objects within a Container object in predictable ways

FlowLayout (the default) add components one after another in rows:

setLayout(new FlowLayout(FlowLayout.LEFT,10,10); for (int counter=1; counter <= 6; counter++) add(new Button(String.valueOf(counter)));

GridLayout places components in cells of a grid:

setLayout(new GridLayout(3,2,5,5); //3 rows, 2 columns, 5 pixel gaps for (int counter=1; counter <= 6; counter++) add(new Button(String.valueOf(counter)));

BorderLayout arranges components using along four sides (North, South, East West) and Center positions

Swing overview

 Defined in package javax.swing

 Original GUI components from AWT in java.awt

 Heavyweight components - rely on local platform's

windowing system for look and feel

 Swing components are lightweight

 Not weighed down by GUI capabilities of platform

 More portable than heavyweight components

 Swing components allow programmer to specify look and feel

 Can change depending on platform

 Can be same across all platforms

Jcomponent features

 Pluggable look and feel

 Can look like different platforms, at run-time

 Shortcut keys (mnemonics)

 Direct access to components through keyboard

 Common event handling

 If several components perform same actions

 Tool tips

 Describe component when mouse rolls over it

Menus

Menu Bar

 JMenuBar()

 add( JMenu )

Menu

 JMenu( String )

 add( JMenuItem )

JMenuBar mb = new JMenuBar(); //create a menu bar JMenu fileMenu = new JMenu (“File”); //create a menu mb.add( fileMenu ); //add menu to menu bar setMenuBar( mb ); // add a menu bar to frame fileMenu.setMnemonic( KeyEvent.VK_F ); // add a hotkey to menu

JMenuItem miOpen = new JMenuItem( “Open...”, KeyEvent.VK_O ); JMenuItem miExit = new JMenuItem( “Exit” );

fileMenu.add( miOpen ); // add a menu item fileMenu.addSeparator(); // add a menu separator fileMenu.add( miExit );

JMenuItem( String )

JMenuItem( String,int )