Latest News

New Summer Look :)










Creating Swing Applications

 

AWT Architecture

Mapping of Classes to Packages

Design for Containment

Designing Layout Management

Designing the Component Peers

Introducing JFC

Mapping of Classes to Packages

The Swing Components

The JComponent

(1) JComponent Widgets

A Simple Example Using JFC

(2) Layout Managers

(3) Model Classes

Swing Model Classes

Changing Look&Feel

Manager Classes

(5) Miscellaneous

The JTabbedPane Component

The JTable Component

The JTree Component

Internal Windows

Using Internal Windows

 


AWT Architecture

(1) Mapping of classes to packages

(2) Design for Containment

(3) Design for Layout Management

(4) Design for the Peers


Mapping of Classes to Packages

java.awt.applet

java.awt

java.awt.color

java.awt.datatransfer

java.awt.dnd

java.awt.event

java.awt.font

java.awt.geom

java.awt.im

java.awt.image

java.awt.print
Applet & relevant Interfaces

Components, Layouts, Graphics

ColorSpaces

Clipboard, DataFlavor, Transferable

DragSource, DropTarget, Events, Listeners

Events, Listeners, Adapters

Font graphics rendering classes

Shapes, AffineTransform, GeneralPath

InputContext, InputMethodRequests

Image Processing classes

PrinterJob, Printable, PageFormat


Design for Containment

- Arbitrary deep containment hierarchy

- Container IS A Component

- Container HAS Components

- Thus Container can mix containing Containers & Component

- This design is called the "composite design pattern"


Designing Layout Management

Naive Design Solution 1

- Positioning components by specifying absolute coordinates is problematic

- We need to give to Containers algorithms for layout their components relatively


Designing Layout Management

Naive Design Solution 2


Designing Layout Management

Java's Solution
(Strategy Design Pattern)

- LayoutManager knows Container?

- When we need absolute
Coordinates we can set the
layout to null


Designing the Component Peers


- How do we get a multiplatform gui api ?

- Using native implementations for each platform (AWT)

- Write all the gui functionality in java (SWING)

- Peer Interfaces decouple AWT components from a specific platform

- Native Implementations
stay completely hidden

 

Creating the Component Peers

 

- The instantiation of a Peer initiates when a Component is added to a Container

- Which Component Peer should a Component instantiate?

- Object creation is hidden from the Clients and delegated to the Abstract Factory Class named Toolkit


Creating the Component Peers

 

Are the Toolkits Hardcoded?

No, Object Creation by Name is preferred

String myToolkitName = System.getProperty("awt.toolkit","sun.awt.motif.Mtoolkit");

toolkit = (Toolkit) Class.for(myToolkitName).newInstance()


Introducing JFC


Java Foundation classes consists of:

- AWT and its components

- Swing components

- Accessibility API

- Java2D

- Drag&Drop

Swing Components are built on top of AWT

Swing is part of jdk since version 1.2.

In earlier versions you can get it as an external api (with 1.1 and without the 2D api)


Mapping of Classes to Packages

javax.swing :

javax.swing.border :

javax.swing.colorchooser :

javax.swing.event :

javax.swing.filechooser :

javax.swing.plaf :

javax.swing.table :

javax.swing.text :

javax.swing.text.html :

javax.swing.text.rtf :

javax.swing.tree :

javax.swing.undo :
Components, Managers, Models

8 Types of Borders

Customisation classes

Listeners, Events, Adapters

Customisation classes

UI Abstractions for Plug&Play GUI

Customisation classes

Text Editor Classes

HTML Editor classes

RTF Editor kit

Customisation classes

Classes for undo functionality


The Swing Components


- Provide pluggable look-and-feel ability

- Lightweight implementation (no Peers)

- JavaBeans compliance

- source code compatibility with AWT components

- javax.swing.*

(1) JComponent widgets

(2) LayoutManager classes

(3) Model classes and Interfaces

(4) Manager classes

(5) Miscellaneous classes


JComponent

- The Parent of all Swing Components

- Overides and extends Containersd

- Gives the ability to define for every Swing Component: Borders, ToolTipText, and UI look&feel


(1) JComponent Widgets

-JButton, JLabel
-JPanel, Box
-JMenu, JMenuItem, JSeparator,JCheckMenuItem, JRadioButtonMenuItem, JMenuBar, JPopupMenu
-JToggleButton, JRadioButton, JCheckBox

-JFileChooser
-JComboBox, Jlist
-JInternalFrame, JDesktopPane, JDesktopIcon
-JOptionPane

-JProgressBar, JSlider, JScrollBar
-JScrollPane, JViewPort
-JSpiltPane, JTabbedPane

-JTable, JTree
-JTextField, JTextArea, JPasswordField, JEditorPane
-JToolBar, JToolTip

-JApplet
-JWindow, JDialog, Jframe
 


A Simple Example Using JFC

 

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;

public class ExampleUsingJFC extends JFrame {

  public static void main(String[] argv) {
     ExampleUsingJFC myExample = new ExampleUsingJFC("ExampleUsingJFC");
   }	

  public ExampleUsingJFC(String title) {
     super(title);
     setSize(150,150);
     addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) { 
           dispose(); 
           System.exit(0);
         }
     });
     init(); 
     setVisible(true);
  }

  private void init() {
     JPanel my_panel = new JPanel();
     my_panel.setLayout(new GridLayout(3,3));
     for (int i=1;i<10;i++) {
	    ImageIcon icon = new ImageIcon(i+".gif");
	    JButton jb = new JButton(icon);
	    jb.setToolTipText(i+".gif");	
	    my_panel.add(jb);
     }		
     getContentPane().add(my_panel);		
     my_panel.setBorder(BorderFactory.createEtchedBorder());
   }
}

-Borders Available:
Bevel, Compound, Empty, Etched, Line, Matte, Soft, Titled


(2) Layout Managers

BoxLayout
- Horizontally or Vertically

OverlayLayout
- Arranging components over the top of each other.

ScrollPanelLayout
- JScrollPane's layout

ViewportLayout
- JViewport's layout

SizeRequirements
- Helper Class for Layout Managers


(3) Model Classes

MVC architectural pattern

Dates back to late 70's

Well known OO design solution

The parts can be exchanged at runtime. Can change L&F very easily

More than one view for a model always in synchronisation

Notification through events

(+) Clean separation of concerns better maintainability and reusability
(-) Many more classes involved

In java View and Controller are grouped to a UIdelegate


Swing Model Classes

  BoundedRangeModel :

ButtonModel :

ListModel :

ComboBoxModel :

MutableComboBoxModel :

ListSelectionModel :

SingleSelectionModel :

ColorSelectionModel :

TableModel :

TableColumnModel :

TreeModel :

TreeSelectionModel :

Document :
4int -> value, extent, min, max

boolean armed

A collection of objects

A collection of objects and a selected object

A Vector of objects and a selected object

Indices of selected list or table items

The index of the selection in a collection

A Color

A two dimensional array of objects

Many attributes

Objects that can be displayed in a tree

Selected rows

Content in text or styled text and images


Changing Look&Feel

Metal L&F

Motif L&F

Windows L&F

 
public void changeLookTo(String cName) {
 try {  
   UIManager.setLookAndFeel(cName); 
 }
 catch (Exception e) { 
   System.out.println("Could not change l&f"); 
 }
 SwingUtilities.updateComponentTreeUI(this);
 this.pack();
}
  ChooseFrom:

- "javax.swing.plaf.metal.MetalLookAndFeel";

- "javax.swing.plaf.motif.MotifLookAndFeel";

- "javax.swing.plaf.windows.WindowsLookAndFeel";

 

(4) Manager Classes

Managers accept customisations

Managers include :

- DesktopManager, DefaultDesktopManager

- FocusManager, DefaultFocusManager

- MenuSelectionManager

- RepaintManager

- ToolTipManager

- UIManager


(5) Miscellaneous


Classes:

- BorderFactory

- ImageIcon, Icon

- LookAndFeel

- ProgressMonitor, ProgressMonitorInputStream

- SwingUtilities

- GrayFilter

- Timer


The JTabbedPane Component

 

private void init() {
	JTabbedPane jtb = new JTabbedPane();
	for (int i=1;i<10;i++) {
	   ImageIcon icon = new ImageIcon(i+".gif");
	   ImageIcon icon2 = new ImageIcon(i+"a.jpg");
           JScrollPane jsp = new JScrollPane(new JLabel(icon2));	
	   jtb.addTab(i+"-tab",icon,jsp);
	}		
	getContentPane().add(jtb);		
	jtb.setBorder(BorderFactory.createEtchedBorder());
}


The JTable Component


String data[][] = {	{"John","Sutherland","Student"}, 
			{"George","Davies","Student"},
			{"Melissa","Anderson","Associate"},
			{"Stergios","Maglaras","Developer"},
	            };

String fields[] = {"Name","Surname","Status"};

private void init() {
	
	JTable jt = new JTable(data,fields);
	JScrollPane pane = new JScrollPane(jt);
	getContentPane().add(pane);		
 }


The JTree Component

Many classes involved

- JTree

- TreeModel, DefaultTreeModel

- TreeNode, MutableTreeNode, DefaultMutableTreeNode

- TreeSelectionModel, TreeSelectionListener, TreeSelEvent

- TreePath

- TreeCellRenderer

- TreeNodeListener, TreeNodeEvent
JTree (L&F:metal)

JTree (L&F:motif)

JTree (L&F:windows)


JTree Example


private void init() {
   DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
   DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
   root.add(months);
   String monthLabels[] = {"January", "February", "March", "April", "May",
  		 	     "June", "July", "August", "September", "October",
			     "November", "December"};
   for (int i=0; i<monthLabels.length; i++) 
  	months.add( new DefaultMutableTreeNode(monthLabels[i]));
   DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
   root.add(weeks);
   String weekLabels[] = {"Monday", "Tuesday", "Wednesday", "Thursday", 
			    "Friday", "Saturday", "Sunday"};
   for (int i=0; i<weekLabels.length; i++) 
   	weeks.add( new DefaultMutableTreeNode(weekLabels[i]));	
   JScrollPane js = new JScrollPane(new JTree(root));
   getContentPane().add(js);		
}


Internal Windows

- JInternalFrames are placed on a JLayeredPane

- JLayeredPane provides Z ordering, but only for lightweight components (order 0 to N-1)

- JInternalFrames can be: closed, maximized, iconified, resized


Using Internal Windows

 


private void init() {
  JLayeredPane layers = new JDesktopPane();
  setLayeredPane(layers);
  for (int i=0; i<9; i++) {
    ImageIcon icon = new ImageIcon((i+1)+"a.jpg");	
    JScrollPane jsp = new JScrollPane(new JLabel(icon));
    jsp.setPreferredSize(new Dimension(120,140));
    //JInternalFrame(title, resizable, closable,maximizable, iconifiable)
    JInternalFrame jif = new JInternalFrame(i+" frame",true,true,true,true);
    jif.setLocation((i%4)*140,(i/4)*180);	
    jif.getContentPane().add(jsp);
    jif.pack();		
    layers.add(jif);  
  }
}
 



Online Java Training, Creating Swing Applications


 
My other sites

Latest News

New Summer Look :)










Creating Swing Applications

 

AWT Architecture

Mapping of Classes to Packages

Design for Containment

Designing Layout Management

Designing the Component Peers

Introducing JFC

Mapping of Classes to Packages

The Swing Components

The JComponent

(1) JComponent Widgets

A Simple Example Using JFC

(2) Layout Managers

(3) Model Classes

Swing Model Classes

Changing Look&Feel

Manager Classes

(5) Miscellaneous

The JTabbedPane Component

The JTable Component

The JTree Component

Internal Windows

Using Internal Windows

 


AWT Architecture

(1) Mapping of classes to packages

(2) Design for Containment

(3) Design for Layout Management

(4) Design for the Peers


Mapping of Classes to Packages

java.awt.applet

java.awt

java.awt.color

java.awt.datatransfer

java.awt.dnd

java.awt.event

java.awt.font

java.awt.geom

java.awt.im

java.awt.image

java.awt.print
Applet & relevant Interfaces

Components, Layouts, Graphics

ColorSpaces

Clipboard, DataFlavor, Transferable

DragSource, DropTarget, Events, Listeners

Events, Listeners, Adapters

Font graphics rendering classes

Shapes, AffineTransform, GeneralPath

InputContext, InputMethodRequests

Image Processing classes

PrinterJob, Printable, PageFormat


Design for Containment

- Arbitrary deep containment hierarchy

- Container IS A Component

- Container HAS Components

- Thus Container can mix containing Containers & Component

- This design is called the "composite design pattern"


Designing Layout Management

Naive Design Solution 1

- Positioning components by specifying absolute coordinates is problematic

- We need to give to Containers algorithms for layout their components relatively


Designing Layout Management

Naive Design Solution 2


Designing Layout Management

Java's Solution
(Strategy Design Pattern)

- LayoutManager knows Container?

- When we need absolute
Coordinates we can set the
layout to null


Designing the Component Peers


- How do we get a multiplatform gui api ?

- Using native implementations for each platform (AWT)

- Write all the gui functionality in java (SWING)

- Peer Interfaces decouple AWT components from a specific platform

- Native Implementations
stay completely hidden

 

Creating the Component Peers

 

- The instantiation of a Peer initiates when a Component is added to a Container

- Which Component Peer should a Component instantiate?

- Object creation is hidden from the Clients and delegated to the Abstract Factory Class named Toolkit


Creating the Component Peers

 

Are the Toolkits Hardcoded?

No, Object Creation by Name is preferred

String myToolkitName = System.getProperty("awt.toolkit","sun.awt.motif.Mtoolkit");

toolkit = (Toolkit) Class.for(myToolkitName).newInstance()


Introducing JFC


Java Foundation classes consists of:

- AWT and its components

- Swing components

- Accessibility API

- Java2D

- Drag&Drop

Swing Components are built on top of AWT

Swing is part of jdk since version 1.2.

In earlier versions you can get it as an external api (with 1.1 and without the 2D api)


Mapping of Classes to Packages

javax.swing :

javax.swing.border :

javax.swing.colorchooser :

javax.swing.event :

javax.swing.filechooser :

javax.swing.plaf :

javax.swing.table :

javax.swing.text :

javax.swing.text.html :

javax.swing.text.rtf :

javax.swing.tree :

javax.swing.undo :
Components, Managers, Models

8 Types of Borders

Customisation classes

Listeners, Events, Adapters

Customisation classes

UI Abstractions for Plug&Play GUI

Customisation classes

Text Editor Classes

HTML Editor classes

RTF Editor kit

Customisation classes

Classes for undo functionality


The Swing Components


- Provide pluggable look-and-feel ability

- Lightweight implementation (no Peers)

- JavaBeans compliance

- source code compatibility with AWT components

- javax.swing.*

(1) JComponent widgets

(2) LayoutManager classes

(3) Model classes and Interfaces

(4) Manager classes

(5) Miscellaneous classes


JComponent

- The Parent of all Swing Components

- Overides and extends Containersd

- Gives the ability to define for every Swing Component: Borders, ToolTipText, and UI look&feel


(1) JComponent Widgets

-JButton, JLabel
-JPanel, Box
-JMenu, JMenuItem, JSeparator,JCheckMenuItem, JRadioButtonMenuItem, JMenuBar, JPopupMenu
-JToggleButton, JRadioButton, JCheckBox

-JFileChooser
-JComboBox, Jlist
-JInternalFrame, JDesktopPane, JDesktopIcon
-JOptionPane

-JProgressBar, JSlider, JScrollBar
-JScrollPane, JViewPort
-JSpiltPane, JTabbedPane

-JTable, JTree
-JTextField, JTextArea, JPasswordField, JEditorPane
-JToolBar, JToolTip

-JApplet
-JWindow, JDialog, Jframe
 


A Simple Example Using JFC

 

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;

public class ExampleUsingJFC extends JFrame {

  public static void main(String[] argv) {
     ExampleUsingJFC myExample = new ExampleUsingJFC("ExampleUsingJFC");
   }	

  public ExampleUsingJFC(String title) {
     super(title);
     setSize(150,150);
     addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) { 
           dispose(); 
           System.exit(0);
         }
     });
     init(); 
     setVisible(true);
  }

  private void init() {
     JPanel my_panel = new JPanel();
     my_panel.setLayout(new GridLayout(3,3));
     for (int i=1;i<10;i++) {
	    ImageIcon icon = new ImageIcon(i+".gif");
	    JButton jb = new JButton(icon);
	    jb.setToolTipText(i+".gif");	
	    my_panel.add(jb);
     }		
     getContentPane().add(my_panel);		
     my_panel.setBorder(BorderFactory.createEtchedBorder());
   }
}

-Borders Available:
Bevel, Compound, Empty, Etched, Line, Matte, Soft, Titled


(2) Layout Managers

BoxLayout
- Horizontally or Vertically

OverlayLayout
- Arranging components over the top of each other.

ScrollPanelLayout
- JScrollPane's layout

ViewportLayout
- JViewport's layout

SizeRequirements
- Helper Class for Layout Managers


(3) Model Classes

MVC architectural pattern

Dates back to late 70's

Well known OO design solution

The parts can be exchanged at runtime. Can change L&F very easily

More than one view for a model always in synchronisation

Notification through events

(+) Clean separation of concerns better maintainability and reusability
(-) Many more classes involved

In java View and Controller are grouped to a UIdelegate


Swing Model Classes

  BoundedRangeModel :

ButtonModel :

ListModel :

ComboBoxModel :

MutableComboBoxModel :

ListSelectionModel :

SingleSelectionModel :

ColorSelectionModel :

TableModel :

TableColumnModel :

TreeModel :

TreeSelectionModel :

Document :
4int -> value, extent, min, max

boolean armed

A collection of objects

A collection of objects and a selected object

A Vector of objects and a selected object

Indices of selected list or table items

The index of the selection in a collection

A Color

A two dimensional array of objects

Many attributes

Objects that can be displayed in a tree

Selected rows

Content in text or styled text and images


Changing Look&Feel

Metal L&F

Motif L&F

Windows L&F

 
public void changeLookTo(String cName) {
 try {  
   UIManager.setLookAndFeel(cName); 
 }
 catch (Exception e) { 
   System.out.println("Could not change l&f"); 
 }
 SwingUtilities.updateComponentTreeUI(this);
 this.pack();
}
  ChooseFrom:

- "javax.swing.plaf.metal.MetalLookAndFeel";

- "javax.swing.plaf.motif.MotifLookAndFeel";

- "javax.swing.plaf.windows.WindowsLookAndFeel";

 

(4) Manager Classes

Managers accept customisations

Managers include :

- DesktopManager, DefaultDesktopManager

- FocusManager, DefaultFocusManager

- MenuSelectionManager

- RepaintManager

- ToolTipManager

- UIManager


(5) Miscellaneous


Classes:

- BorderFactory

- ImageIcon, Icon

- LookAndFeel

- ProgressMonitor, ProgressMonitorInputStream

- SwingUtilities

- GrayFilter

- Timer


The JTabbedPane Component

 

private void init() {
	JTabbedPane jtb = new JTabbedPane();
	for (int i=1;i<10;i++) {
	   ImageIcon icon = new ImageIcon(i+".gif");
	   ImageIcon icon2 = new ImageIcon(i+"a.jpg");
           JScrollPane jsp = new JScrollPane(new JLabel(icon2));	
	   jtb.addTab(i+"-tab",icon,jsp);
	}		
	getContentPane().add(jtb);		
	jtb.setBorder(BorderFactory.createEtchedBorder());
}


The JTable Component


String data[][] = {	{"John","Sutherland","Student"}, 
			{"George","Davies","Student"},
			{"Melissa","Anderson","Associate"},
			{"Stergios","Maglaras","Developer"},
	            };

String fields[] = {"Name","Surname","Status"};

private void init() {
	
	JTable jt = new JTable(data,fields);
	JScrollPane pane = new JScrollPane(jt);
	getContentPane().add(pane);		
 }


The JTree Component

Many classes involved

- JTree

- TreeModel, DefaultTreeModel

- TreeNode, MutableTreeNode, DefaultMutableTreeNode

- TreeSelectionModel, TreeSelectionListener, TreeSelEvent

- TreePath

- TreeCellRenderer

- TreeNodeListener, TreeNodeEvent
JTree (L&F:metal)

JTree (L&F:motif)

JTree (L&F:windows)


JTree Example


private void init() {
   DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
   DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
   root.add(months);
   String monthLabels[] = {"January", "February", "March", "April", "May",
  		 	     "June", "July", "August", "September", "October",
			     "November", "December"};
   for (int i=0; i<monthLabels.length; i++) 
  	months.add( new DefaultMutableTreeNode(monthLabels[i]));
   DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
   root.add(weeks);
   String weekLabels[] = {"Monday", "Tuesday", "Wednesday", "Thursday", 
			    "Friday", "Saturday", "Sunday"};
   for (int i=0; i<weekLabels.length; i++) 
   	weeks.add( new DefaultMutableTreeNode(weekLabels[i]));	
   JScrollPane js = new JScrollPane(new JTree(root));
   getContentPane().add(js);		
}


Internal Windows

- JInternalFrames are placed on a JLayeredPane

- JLayeredPane provides Z ordering, but only for lightweight components (order 0 to N-1)

- JInternalFrames can be: closed, maximized, iconified, resized


Using Internal Windows

 


private void init() {
  JLayeredPane layers = new JDesktopPane();
  setLayeredPane(layers);
  for (int i=0; i<9; i++) {
    ImageIcon icon = new ImageIcon((i+1)+"a.jpg");	
    JScrollPane jsp = new JScrollPane(new JLabel(icon));
    jsp.setPreferredSize(new Dimension(120,140));
    //JInternalFrame(title, resizable, closable,maximizable, iconifiable)
    JInternalFrame jif = new JInternalFrame(i+" frame",true,true,true,true);
    jif.setLocation((i%4)*140,(i/4)*180);	
    jif.getContentPane().add(jsp);
    jif.pack();		
    layers.add(jif);  
  }
}