Jaeki Song JAVA Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components.
THE SWING UI TOOLKIT
description
Transcript of THE SWING UI TOOLKIT
THE SWING UI TOOLKIT
Mostly from “The Swing Connection”
Lecture 4: Swing 2
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
SWING TOOLKIT• 100% Java implementation of components
• Pluggable Look & Feel– customizable for different environments, or
– use Java L&F in every environment
• Lightweight components– no separate (child)windows for components
– allows more variation on component structure
– makes L&F possible
• Three parts– component set (subclasses of JComponent)
– support classes
– interfaces
Lecture 4: Swing 3
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
OTHER APIs
Lecture 4: Swing 4
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
UI COMPONENTS
Lecture 4: Swing 5
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
BUTTONS
Lecture 4: Swing 6
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
MENUS
JMenuBar
Lecture 4: Swing 7
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
OTHER COMPONENTS
JApplet Border Interface
JColorChooserJComboBox
ImageIcon
JDialogJFileChooser
JInternalFrame
Lecture 4: Swing 8
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
OTHER COMPONENTS
JList
JOptionPane
JScrollBar
JScrollPane JSlider
JSplitPaneJTabbedPane
JLabel
Lecture 4: Swing 9
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
OTHER COMPONENTS
JTable
JTextArea
JTextField
JToolBar
JToolTip
JTree
Lecture 4: Swing 10
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
ARCHITECTURE• Goals:
– entirely on Java
– pluggable L&F
– model-driven programming
– JavaBeans
– compability with AWT
• Use MVC?– Model represents the data
– View as a visual representation of the data
– Controller takes input and translates it to changes in data
Lecture 4: Swing 11
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
THE UI DELEGATE• No reason to separate controller and view
• A separate UI object for defining the visualrepresentation and controller behaviour
the UI delegate
Lecture 4: Swing 12
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
MODELS• Data-centric applications
• Separate model interface for every component– GUI-state models
• up-down state in JButton and subclasses
– application data models• selection state in JToggleButton and subclasses
• Application programmer can implement his/her own data models for existing components
• Shared model definitions
Lecture 4: Swing 13
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
MODEL SEPARATION
• JSlider uses BoundedRangeModel– public JSlider(int orientation, int min, int max, int value) {
checkOrientation(orientation); this.orientation = orientation; this.model = new DefaultBoundedRangeModel(value, 0, min, max); this.model.addChangeListener(changeListener); updateUI();}
• Calling setModel, application can replace the default– JSlider slider = new JSlider();
BoundedRangeModel myModel = new DefaultBoundedRangeModel() { public void setValue(int n) { System.out.println("SetValue: "+ n); super.setValue(n); } });slider.setModel(myModel);
Lecture 4: Swing 14
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
CHANGE NOTIFICATION• Models implement methods for adding and
removing listeners
• Lightweight notification– only notify
– listener queries about the changes
– e.g. scrollabar dragged
• Stateful notification– event described the change
– for complex data models
– e.g. changes in the column of table
Lecture 4: Swing 15
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
LIGHTWEIGHT NOTIFICATION• ChangeListener with one single method
– public void stateChanged(ChangeEvent e);
• Listening to JSlider– JSlider slider = new JSlider();
BoundedRangeModel model = slider.getModel();model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { // need to query the model to get updated value... BoundedRangeModel m = (BoundedRangeModel)e.getSource(); System.out.println("model changed: " + m.getValue()); }});
Lecture 4: Swing 16
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
STATEFUL NOTIFICATION• Tracking JList selection
– String items[] = {"One", "Two", "Three");JList list = new JList(items);ListSelectionModel sModel = list.getSelectionModel();sModel.addListSelectionListener (new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // get change information directly // // from the event instance... if (!e.getValueIsAdjusting()) { System.out.println("selection changed: " + e.getFirstIndex()); } } });
Lecture 4: Swing 17
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
IGNORING MODELS• Most components provide API to the model
directly
• E.g. JSlider’s method– public int getValue() {
return getModel().getValue();}
• Program can simply do the following– JSlider slider = new JSlider();
int value = slider.getValue();
• So, where’s the “model,” anyway!
Lecture 4: Swing 18
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
SETTING LOOK & FEEL• To set a particular L&F (here CDE/Motif), write
– UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel” );
• To set the appropriate L&F, whatever the current environment, write– UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName() );
• Do the above preferably at the end of the program (before instantiating any components)
Lecture 4: Swing 19
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
THE SWING PACKAGES• The Accessibility package (javax.accessibility)
– provides support for supporting the screen access products for people with disabilities
– Swing has full support for accessibility
• javax.swing– contains nearly all of the Swing components
– notable exception is JTextComponent (in javax.swing.text)
• javax.swing.border– in need for customized borders, take a look
• javax.swing.event– includes the additional event classes (not found in java.awt.event)
Lecture 4: Swing 20
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
THE SWING PACKAGES (cont’d)• javax.swing.plaf
– classes for providing the L&F capabilities
– also javax.swing.plaf.basic including the default L&F classes
– the current specialized L&F:s• javax.swing.plaf.metal
• javax.swing.plaf.motif (or com.sun.java.swing.plaf.motif)
• javax.swing.plaf.windows (or com.sun.java.swing.plaf.windows)
– also javax.swing.plaf.multi for mixing multiple L&F:s
• javax.swing.table– including support classes for managing tables
• javax.swing.tree– support classes for managing trees
Lecture 4: Swing 21
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
THE SWING PACKAGES (cont’d)• javax.swing.text
– support classes for text editing
– Document classes
– JTextComponent (superclass for all text components)
– see also separate format packages• javax.swing.text.html• javax.swing.text.rtf
• javax.swing.undo– classes for supporting undo/redo operations
Lecture 4: Swing 22
PR
OG
RA
MM
ING
GR
AP
HIC
AL
US
ER
INT
ER
FA
CE
S
JComponent• An abstract root class of almost all
of Swing components
• Provides– pluggable L&F
– extensibility
– smart trapping of keyboard events (see javax.swing.KeyStroke)
– customizable borders
– easy resizing
– tool tips
– autoscrolling
– support for debugging
– support for accessibility
– support for localization
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent