Graphical User Interface
Introduction
• Swing library is an official Java GUI toolkit
released by Sun Microsystems
• Swing is a part of JFC, Java F...
Introduction (contd)
• Platform independent
• Customizable
• Extensible
• Configurable
• Lightweight
Swing API packages
• Swing API has 18 public packages
• javax.accessibility
• javax.swing
• javax.swing.border
• javax.swi...
Swing API packages (contd)
• javax.swing.plaf.multi
• javax.swing.plaf.synth
• javax.swing.table
• javax.swing.text
• java...
Swing Components Hierarchy
Swing Components
Layout Management
• Java Swing toolkit has two kind of
components
– Container Components
– Children Components
• The conta...
Layout Management (contd)
• BorderLayout
• BoxLayout
• CardLayout
• FlowLayout
• GridBagLayout
• GridLayout
• GroupLayout
...
Absolute Positioning
• If a container holds components whose size is not
affected by the container's size or by font, look...
Absolute Positioning (contd)
• Creating containers with absolutely positioned
containers can cause problems if the window
...
FlowLayout
• The FlowLayout class provides a very simple
layout manager that is used, by default, by the
JPanel objects
• ...
FlowLayout (contd)
• To specify that the row is to aligned either to
the left or right, use a FlowLayout constructor
that ...
Flow Layout (contd)
BorderLayout
• A BorderLayout object has five areas specified
by the BorderLayout constants:
– PAGE_START
– PAGE_END
– LIN...
BorderLayout (contd)
• If the window is enlarged, the center area gets
as much of the available space as possible
• Other ...
BorderLayout (contd)
GridLayout
• A GridLayout object places components in a
grid of cells
• Each component takes all the available space
withi...
GridLayout (contd)
Event Handling
Introduction
• All GUI applications are event-driven
• An application reacts to different event types
which are generated ...
Introduction (contd)
• In the event model, there are three participants:
– event source
– event object
– event listener
• ...
Event Object
• When something happens in the application, an
event object is created
• There are several types of events, ...
Implementation
• There are several ways, how we can
implement event handling in Java Swing
toolkit
– Anonymous inner class...
Anonymous Inner Class
• The button is the event source and it will
generate events
closeButton.addActionListener(new Actio...
Inner Class
• The listener is defined inside an inner class, which
has a name
ButtonCloseListener listener = new ButtonClo...
Derived Class
• We create a MyButton class, that implements the action
listener
MyButton closeButton = new MyButton("Close...
Adapter Classes
• Time consuming to define all interface
methods
• WindowListener has seven methods
• What if we only want...
Adapter Classes (contd)
• You then extend adapter class,
• overriding methods for events you care about,
such as windowClo...
Upcoming SlideShare
Loading in …5
×

Swing and Graphical User Interface in Java

1,766 views

Published on

Graphical User Interface in Java

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,766
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
38
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Swing and Graphical User Interface in Java

  1. 1. Graphical User Interface
  2. 2. 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
  3. 3. Introduction (contd) • Platform independent • Customizable • Extensible • Configurable • Lightweight
  4. 4. 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
  5. 5. 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
  6. 6. Swing Components Hierarchy
  7. 7. Swing Components
  8. 8. 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
  9. 9. Layout Management (contd) • BorderLayout • BoxLayout • CardLayout • FlowLayout • GridBagLayout • GridLayout • GroupLayout • SpringLayout
  10. 10. 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.
  11. 11. Absolute Positioning (contd) • Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized
  12. 12. 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
  13. 13. 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)
  14. 14. Flow Layout (contd)
  15. 15. BorderLayout • A BorderLayout object has five areas specified by the BorderLayout constants: – PAGE_START – PAGE_END – LINE_START – LINE_END – CENTER
  16. 16. 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.
  17. 17. BorderLayout (contd)
  18. 18. 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
  19. 19. GridLayout (contd)
  20. 20. Event Handling
  21. 21. 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
  22. 22. 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.
  23. 23. 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
  24. 24. Implementation • There are several ways, how we can implement event handling in Java Swing toolkit – Anonymous inner class – Inner class – Derived class
  25. 25. 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
  26. 26. Inner Class • The listener is defined inside an inner class, which has a name ButtonCloseListener 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.
  27. 27. 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.
  28. 28. 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
  29. 29. 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

×