swing and graphical user interface in java

29
Graphical User Interface

Upload: babak

Post on 28-Jan-2015

107 views

Category:

Education


2 download

DESCRIPTION

Graphical User Interface in Java

TRANSCRIPT

Page 1: Swing and Graphical User Interface in Java

Graphical User Interface

Page 2: Swing and Graphical User Interface in Java

Introduction• Swing library is an official Java GUI toolkit

released by Sun Microsystems• Swing is a part of JFC, Java Foundation Classes• Used to create Graphical user interfaces with

Java. • It is a collection of packages for creating full

featured desktop applications. • JFC consists of AWT, Swing, Accessibility, Java 2D,

and Drag and Drop. • Swing was released in 1997 with JDK 1.2

Page 3: Swing and Graphical User Interface in Java

Introduction (contd)

• Platform independent• Customizable• Extensible• Configurable• Lightweight

Page 4: Swing and Graphical User Interface in Java

Swing API packages

• Swing API has 18 public packages• javax.accessibility• javax.swing• javax.swing.border• javax.swing.colorchooser• javax.swing.event• javax.swing.filechooser• javax.swing.plaf• javax.swing.plaf.basic• javax.swing.plaf.metal

Page 5: Swing and Graphical User Interface in Java

Swing API packages (contd)• javax.swing.plaf.multi• javax.swing.plaf.synth• javax.swing.table• javax.swing.text• javax.swing.text.html• javax.swing.text.html.parser• javax.swing.text.rtf• javax.swing.tree• javax.swing.undo

Page 6: Swing and Graphical User Interface in Java

Swing Components Hierarchy

Page 7: Swing and Graphical User Interface in Java

Swing Components

Page 8: Swing and Graphical User Interface in Java

Layout Management

• Java Swing toolkit has two kind of components– Container Components– Children Components• The containers group children into suitable

layouts• To create layouts, we use layout managers

Page 9: Swing and Graphical User Interface in Java

Layout Management (contd)

• BorderLayout• BoxLayout• CardLayout• FlowLayout• GridBagLayout• GridLayout• GroupLayout• SpringLayout

Page 10: Swing and Graphical User Interface in Java

Absolute Positioning• If a container holds components whose size is not

affected by the container's size or by font, look-and-feel, or language changes

• Creating a container without a layout manager involves the following steps.

1. Set the container's layout manager to null by calling setLayout(null).

2. Call the Component class's setbounds method for each of the container's children.

3. Call the Component class's repaint method.

Page 11: Swing and Graphical User Interface in Java

Absolute Positioning (contd)

• Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized

Page 12: Swing and Graphical User Interface in Java

FlowLayout• The FlowLayout class provides a very simple

layout manager that is used, by default, by the JPanel objects

• The FlowLayout class puts components in a row, sized at their preferred size

• If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows

• If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container

Page 13: Swing and Graphical User Interface in Java

FlowLayout (contd)

• To specify that the row is to aligned either to the left or right, use a FlowLayout constructor that takes an alignment argument– public FlowLayout(int align) • Another constructor of the FlowLayout class

specifies how much vertical or horizontal padding is put around the components

– public FlowLayout(int align, int hgap, int vgap)

Page 14: Swing and Graphical User Interface in Java

Flow Layout (contd)

Page 15: Swing and Graphical User Interface in Java

BorderLayout

• A BorderLayout object has five areas specified by the BorderLayout constants:– PAGE_START– PAGE_END– LINE_START– LINE_END– CENTER

Page 16: Swing and Graphical User Interface in Java

BorderLayout (contd)

• If the window is enlarged, the center area gets as much of the available space as possible

• Other areas expand only as much as necessary to fill all available space

• Often a container uses only one or two of the areas of the BorderLayout object — just the center, or the center and the bottom.

Page 17: Swing and Graphical User Interface in Java

BorderLayout (contd)

Page 18: Swing and Graphical User Interface in Java

GridLayout

• A GridLayout object places components in a grid of cells

• Each component takes all the available space within its cell, and each cell is exactly the same size

• If the window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container

Page 19: Swing and Graphical User Interface in Java

GridLayout (contd)

Page 20: Swing and Graphical User Interface in Java

Event Handling

Page 21: Swing and Graphical User Interface in Java

Introduction

• All GUI applications are event-driven• An application reacts to different event types

which are generated during its life• Events are generated mainly by the user of an

application• But they can be generated by other means as

well. e.g. internet connection, window manager, timer

Page 22: Swing and Graphical User Interface in Java

Introduction (contd)

• In the event model, there are three participants: – event source– event object– event listener

• The Event source is the object whose state changes and generates Events

• The Event object (Event) encapsulates the state changes in the event source

• The Event listener is the object that wants to be notified. Event source object delegates the task of handling an event to the event listener.

Page 23: Swing and Graphical User Interface in Java

Event Object

• When something happens in the application, an event object is created

• There are several types of events, e.g ActionEvent, TextEvent, FocusEvent, ComponentEvent etc, created under specific conditions.

• Event object has information about an event, that has happened

Page 24: Swing and Graphical User Interface in Java

Implementation

• There are several ways, how we can implement event handling in Java Swing toolkit– Anonymous inner class– Inner class– Derived class

Page 25: Swing and Graphical User Interface in Java

Anonymous Inner Class

• The button is the event source and it will generate events closeButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) { System.exit(0);

} });• Here we register an action listener with the button• This way, the events are sent to the event target• The event target in our case is ActionListener class

Page 26: Swing and Graphical User Interface in Java

Inner Class

• The listener is defined inside an inner class, which has a nameButtonCloseListener listener = new ButtonCloseListener(); closeButton.addActionListener(listener);

• Here we have a non anonymous inner class class ButtonCloseListener implements ActionListener {

public void actionPerformed(ActionEvent e) { System.exit(0);

} }

• The button listener is defined here.

Page 27: Swing and Graphical User Interface in Java

Derived Class

• We create a MyButton class, that implements the action listener MyButton closeButton = new MyButton("Close");

• Now create the MyButton custom class class MyButton extends JButton implements ActionListener {

• The MyButton class is extended from the JButton class and implements the ActionListener interface

• This way, the event handling is managed within the MyButton class. addActionListener(this);

• Here we add the action listener to the MyButton class.

Page 28: Swing and Graphical User Interface in Java

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

Page 29: Swing and Graphical User Interface in Java

Adapter Classes (contd)

• 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