Title: Java Programming
1Java Programming
Introduction to Swing Class 7
2Swing
- Graphical User Interface
- Provides
- Windows
- Menus
- Buttons
- Labels
- Textboxes
- In the beginning, there was AWT .
- Abstract Windowing Toolkit
- Relied on peers
- Heavyweight Native Code interfaces
- Written for each target
- Enabled original AWT to be written in 6 weeks!
- Portability problems
3A Swing Program
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
4A Swing Program
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
5A Swing Program
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
6Constants
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
7Arrays
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- JWindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
8Construct a window
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- JWindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
9Where can I draw things?
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- JWindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
10Panes?
- There are 3 of them
- contentPane
- JPanel
- Resides in layeredPane
- Contains components
- glassPane
- JPanel
- Floats above others
- Traps mouse events first
- layeredPane
- JLayeredPane
- Contains contentPane and menuBar
- menuBar
- JMenuBar
11Visibility and Layout
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- JWindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
12Adding things to a window
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
13Some Java features
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
14Almost there ...
- import java.awt.
- import javax.swing.
- public class JWTest
- static final int n_label 10
- public static void main( String args )
- JLabel lab1
- Jwindow w new JWindow()
- Container content_pane w.getContentPane()
- w.setVisible( true )
- content_pane.setLayout( new BorderLayout( ) )
- lab1 new JLabeln_label
- for(int k0kltn_labelk)
- lab1k new JLabel("Label " k)
- content_pane.add( lab1k )
-
- w.pack()
- w.show()
- System.exit(0)
-
15Those labels are boring ...
- Changing them
- Swing API
- Documentation cached at ..
- http//www.javasoft.com
- Observe the hierarchy
Object
Component
Container
JComponent
JLabel
16Those labels are boring ...
- Changing them
- Note the specification
public class JLabel extends
JComponent implements
SwingConstants, Accessible
17Those labels are boring ...
- Look for methods that operate on JLabel
- Note that most of them dont have arguments
- They operate on a JLabel
Icon getIcon() int getHorizontalAlignment() int
getHorizontalTextPosition() String
getText() void setText( String text ) etc
JLabel label new JLabel(Test label) Icon img
label.getIcon()
18But I want to change the text font!
- Look for methods on the superclasses
- On JComponent
- Since a JLabel is also a Jcomponent ..
- works fine!
void setFont( Font font )
Jlabel label new Jlabel(Test
label) label.setFont( newfont )
19and I want to change the location ..
- Look for methods on the superclasses
- On JComponent
- ?? Nothing likely
- Try the next ancestor ..
- On Container
- ?? Nothing likely
- On Component
- looks promising ...
- But theres two???
- works fine!
setLocation
20Polymorphism in practice ...
- But theres two .. !!
- Choose the most convenient
- the compiler determines which one to use by
matching the signature ..
void setLocation( int x, int y )void
setLocation( Point p )
label.setLocation( X_POS, Y_POS )
void setLocation( int, int )
21Polymorphism in practice ...
- Multiple Constructors
- Common situation
- Again choose the most convenient
- Other attribute values will assume default
values, eg - No icon, centre alignment
JLabel() JLabel( Icon image ) JLabel( Icon
image, int horAlign )JLabel( String text
)JLabel( String text, Icon image,
int horAlogin JLabel( String text, int horAlign )
22Other common components
- Buttons
- JButton
- Standard way for initiating some action from a
screen - Used with an ActionListener (next lecture!)
- JToggleButton
- Two states selected / de-selected
- Query with
- boolean isSelected()
- ButtonGroup
- Found in the parent AbstractButton!!
- Allows groups of buttons
- Select one
- Automatic de-select of remainder
23Other common components
- Buttons
- JCheckBox
- Checked box ..
- Selected / Not selected
- Its a specialization of JToggleButton!
- JRadioButton
- Similar to JCheckBox
24Frames Applets
- JFrame
- Specialization of Frame
- Adds
- Border
- Menubar
- Close box
- JApplet
- Similar to JFrame
- Both have
- ContentPane
- GlassPane
25Panels
- JPanel
- Useful as a container for other elements
26Panels
- JPanel
- Useful as a container for other elements
JWindow w new JWindow()JPanel panel1 new
JPanel() JPanel panel2 new JPanel() JButton
b1 new JButton() panel1.add( b1 ) w.add(
panel1 ) panel2.add( b2 ) w.add( panel2 )
27Panels
- JPanel
- Make the panels classes!
28Text - Edit for user input
- JTextField
- Single line of editable text
- public class TextExample extends JPanel
JTextField tf - public TextExample( String s )
- tf new JTextField( s ) add( tf )
-
- public String getText() return
tf.getText()
29Text - Edit for user input
- JTextArea
- Multiple lines of editable text
- public class TextExample extends JPanel
JTextArea ta - static final int rows 10
- static final int cols 20
- public TextExample( String s )
- ta new JTextArea( s, rows, cols ) add(
ta ) -
- public String getText( int row )
- int offset ta.getLineStartOffset( row )
return tf.getText( offset, cols )
30Menus
- Menus are attached to a JMenuBar
- A Menu(JMenu)contains items (JMenuItem)
- Recipe
- Create a frame
- Create a menubar
- Create a menu
- Create some menu items
- Add an ActionListener (next lecture!)
- Add them to the menu
- Add the menu to the menubar
- Set the menubar
-
Repeat as needed
31Menus
- Example
- Create a frame
- Create a menubar
- Create a menu
- Create some menu items
- Trap events
- Add them to the menu
- Add the menuto the menubar
- Set the menubar
-
32Swing Example
- Jwindow
- Simple usage example
- import javax.swing.public class windowexample
public static void main(String args)
JWindow w JLabel label int n w
new JWindow() label new JLabel(Example
label) w.add( label ) n
w.getComponentCount() System.out.println(Wi
ndow has n
components) w.show()
33Java ProgrammingSwing (continued)
10 mins break (not more)
34Menus
- Menus are attached to a JMenuBar
- A Menu(JMenu)contains items (JMenuItem)
- Recipe
- Create a frame
- Create a menubar
- Create a menu
- Create some menu items
- Add an ActionListener (next lecture!)
- Add them to the menu
- Add the menu to the menubar
- Set the menubar
-
Repeat as needed
35Menus
- Example
- Create a frame
- Create a menubar
- Create a menu
- Create some menu items
- Trap events
- Add them to the menu
- Add the menuto the menubar
- Set the menubar
36Thats all very pretty but ...
- Our GUI needs some action!
- Add Listeners to Components
- Component fire events,
- Listeners trap them
- Various types of listeners ..
- ActionListener
- MouseListener
- MenuListener
- and some more ...
- Listeners are interfaces
- No code!
- Remember just a specification for methods you
must supply
37Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
38Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
39Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
40Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
41Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
42Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
43Making a Listener - Method 1
- Define a class implementing ActionListener
-
// Event Handler import java.awt. import
java.awt.event. import javax.swing. public
class EventHandler implements ActionListener
JPanel parent JComponent src public
EventHandler( JPanel m ) // Establish a link
to my parent parent m //
ActionListener interface public void
actionPerformed( ActionEvent e )
System.out.println("aP event " e ) src
(JComponent)(e.getSource())
44 wed better attach the listener to something!
- An Action button
- import javax.swing.public class windowexample
public static void main(String args)
JWindow w new JWindow() JButton b new
JButton(Action!) EventHandler h new
EventHandler( this ) Container cp
getContentPane() cp.add( b )
b.addActionListener( h ) w.pack()
w.show()
45How does the listener talk to us??
- Various possibilities
- Call a method on the parent
46How does the listener talk to us??
- Various possibilities
- Call a method in another class
47A case for an interface!!
- Define a class of objects which can be
incremented - Must provide a void incX( int ) method
48A case for an interface!!
- Define a class of objects which can be
incremented - Must provide a void incX( int ) method
- The interface provides the signature for a method
- (or methods - it can require many)
- The class implements the method(s) in the
interface - and any others that it needs!
49A case for an interface!!
- A class may implement several interface
50Comparable ..
- To make many common structures ..
- Look up tables, search trees
- and perform common operations
- Sort, Match
- We need to compare items ..
- A Comparable interface is part of java.lang
- It has one method
- Class implementor decides how objects of a class
are ordered - May depend on some very complex rules!!
- So compareTo can only be implemented in specific
classes - but there are many places where ordering is
needed!!
51Talking to the handler ...
- The handler class can update one of its
attributes and provide a method to access it
52Making a Listener - Method 2
- Define a separate class is a pain!
- Too many files, overhead
- No problem - make it an inner class
-
public class windowexample int x class
EventHandler implements ActionListener
// ActionListener interface public void
actionPerformed( ActionEvent e ) x
public static void main(String args)
JWindow w new JWindow() JButton b new
JButton(Action!) EventHandler h new
EventHandler( this ) Container cp
getContentPane() cp.add( b )
b.addActionListener( h )
53Making a Listener - Method 2
- Define a separate class is a pain!
- Too many files, too much overhead
- No problem - make it an inner class
-
public class windowexample int x class
EventHandler implements ActionListener
// ActionListener interface public void
actionPerformed( ActionEvent e ) x
public static void main(String args)
JWindow w new JWindow() JButton b new
JButton(Action!) EventHandler h new
EventHandler( this ) Container cp
getContentPane() cp.add( b )
b.addActionListener( h )
54Making a Listener - Method 3
- But thats a whole class with only one method!
- Im lazy!
- No problem - make it an anonymous class
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x ) Container cp
getContentPane() cp.add( b )
55Making a Listener - Method 3
- But thats a whole class with only one method!
- Im lazy!
- No problem - make it an anonymous class
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x ) Container cp
getContentPane() cp.add( b )
56Making a Listener - Method 3
- But thats a whole class with only one method!
- Im lazy!
- No problem - make it an anonymous class
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
57Anonymous classes
- The syntax can be confusing!
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
58Anonymous classes
- The syntax can be confusing!
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
59Anonymous classes
- The syntax can be confusing!
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
60Anonymous classes
- The syntax can be confusing!
-
public class windowexample int x public
static void main(String args) JWindow w
new JWindow() JButton b new
JButton(Action!) b.addActionListener(
new ActionListener() public
void actionPerformed( ActionEvent e )
x )
Container cp getContentPane() cp.add( b
)
61MouseListener interface
- Fine control of your GUI, but
- 5 methods!
- An interface must implement each one
- Fine if you want special effects,but painful if
you only want a click! -
JButton b new JButton(Action!)b.addMouseList
ener( new MouseListener() public
void mouseClicked(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
)
62MouseListener interface
- 5 methods!
- A classic case for an adapter!
- MouseAdapter is a class implementing
MouseListener - Provides dummy implementations for all 5 required
methods - You override only the one you want to use
- Fine if you want special events,but painful if
you only want a click! -
JButton b new JButton(Action!)b.addMouseList
ener( new MouseAdapter() // Only
interested in clicks .. public void
mouseClicked(MouseEvent e) x x 1
)
63What about the event parameter?
- Look in the ..Event classes
- Properties of events
- eg Location
- For those fancy drag-and-drop applications!
- Now you can set the location of some other object
..And repaint making the object follow the
mouse, ... -
JPanel p new JPanel()p.addMouseListener(
new MouseAdapter() // Want to know where
the mouse was pressed public void
mousePressed(MouseEvent e) x
e.getX() y e.getY() // Code to
move something here repaint()
)