the java swing toolkitwaltermilner.com/java/swing.pdf · the awt started with jdk 1.0 awt used os...

22
The Java Swing Toolkit Introduction to Swing WHAT IS SWING? ............................................................................................................................................. 2 SIMPLE EXAMPLE ............................................................................................................................................. 3 HOW TO START A SWING APPLICATION........................................................................................................... 4 LABELS, TEXT BOXES AND BUTTONS ................................................................................................................ 5 HANDLING BUTTON CLICK EVENTS .................................................................................................................. 6 Which Button? ................................................................................................................................................. 6 EVENT HANDLING IN GENERAL .................................................................................................................................... 7 Using adapter classes ...................................................................................................................................... 8 IMAGES............................................................................................................................................................ 9 Images from the net ...................................................................................................................................... 10 COLOR ........................................................................................................................................................... 11 FONTS ............................................................................................................................................................ 12 CONTAINERS AND STRUCTURE ...................................................................................................................... 13 JPanels and Borders ....................................................................................................................................... 13 ADDING COMPONENTS TO A JPANEL .......................................................................................................................... 13 THE CONTAINMENT HIERARCHY ................................................................................................................................ 14 TOP LEVEL CONTAINERS ........................................................................................................................................... 14 JROOTPANE AND COMPONENTS................................................................................................................................ 14 Using the glassPane....................................................................................................................................... 14 LAYOUT MANAGERS ............................................................................................................................................... 15 BorderLayout ................................................................................................................................................. 15 GridBagLayout ............................................................................................................................................... 16 JTABBEDPANE ....................................................................................................................................................... 17 JSPLITPANE .......................................................................................................................................................... 17 MENUS, POPUPS ANDTOOLBARS ................................................................................................................... 19 MAKING A BASIC MENU........................................................................................................................................... 19 MAKING THE MENU WORK ....................................................................................................................................... 19 ELABORATING MENUS ............................................................................................................................................ 19 POPUP MENUS...................................................................................................................................................... 20 MENU DESIGN ISSUES ............................................................................................................................................. 20 TOOLBARS ............................................................................................................................................................ 20 SUBCLASSING WIDGETS ................................................................................................................................. 22

Upload: others

Post on 11-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

WHAT IS SWING? ............................................................................................................................................. 2

SIMPLE EXAMPLE ............................................................................................................................................. 3

HOW TO START A SWING APPLICATION ........................................................................................................... 4

LABELS, TEXT BOXES AND BUTTONS ................................................................................................................ 5

HANDLING BUTTON CLICK EVENTS .................................................................................................................. 6

Which Button? ................................................................................................................................................. 6 EVENT HANDLING IN GENERAL .................................................................................................................................... 7

Using adapter classes ...................................................................................................................................... 8

IMAGES ............................................................................................................................................................ 9

Images from the net ...................................................................................................................................... 10

COLOR ........................................................................................................................................................... 11

FONTS ............................................................................................................................................................ 12

CONTAINERS AND STRUCTURE ...................................................................................................................... 13

JPanels and Borders ....................................................................................................................................... 13 ADDING COMPONENTS TO A JPANEL .......................................................................................................................... 13 THE CONTAINMENT HIERARCHY ................................................................................................................................ 14 TOP LEVEL CONTAINERS ........................................................................................................................................... 14 JROOTPANE AND COMPONENTS................................................................................................................................ 14

Using the glassPane ....................................................................................................................................... 14 LAYOUT MANAGERS ............................................................................................................................................... 15

BorderLayout ................................................................................................................................................. 15 GridBagLayout ............................................................................................................................................... 16

JTABBEDPANE ....................................................................................................................................................... 17 JSPLITPANE .......................................................................................................................................................... 17

MENUS, POPUPS ANDTOOLBARS ................................................................................................................... 19

MAKING A BASIC MENU ........................................................................................................................................... 19 MAKING THE MENU WORK ....................................................................................................................................... 19 ELABORATING MENUS ............................................................................................................................................ 19 POPUP MENUS ...................................................................................................................................................... 20 MENU DESIGN ISSUES ............................................................................................................................................. 20 TOOLBARS ............................................................................................................................................................ 20

SUBCLASSING WIDGETS ................................................................................................................................. 22

Page 2: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

WHAT IS SWING?

The Swing toolkit is a group of packages which are used for creating GUI interfaces. With Swing you can create windows, labels, text boxes, radio buttons, scroll bars and so on.

An alternative to Swing is the Abstract Windowing Toolkit, or AWT. This is abstract in the sense that you use ideas like buttons, which are an abstract version of the actual buttons present on native GUIs in Windows, Solaris and other OS's. The AWT started with JDK 1.0

AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT is said to be 'heavy-weight' (because it uses a lot of classes) whereas most classes in Swing are lightweight. Swing started in JDK 1.2. In most versions of the JDK, you cannot mix AWT and Swing.

These brief notes are intended to explain what Swing is all about, and to provide brief code snippets to enable you to quickly write GUI interfaces which work in typical ways.They are not a complete reference - for that, look at the online API.

This document only covers the simple widgets. Those which carry significant amounts of data, such as lists and tables, are covered by a separate document which involves MVC.

Most of the code listing does not include import statements. Most IDEs will produce these for you.

Page 3: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

SIMPLE EXAMPLE This displays a window:

class Test { public static void main(String[] args) { JFrame frame=new JFrame("A Window"); frame.setBounds(0,0,400,200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

A JFrame object is a typical window. The setBounds method controls the x and y co-ordinates of the top left of the window, in pixels, relative to the screen. The third and fourth parameters determine the width and height. The setDefaultCloseOperation controls what happens when the window's close button is clicked - EXIT_ON_CLOSE exits the application (the default is nothing happens). A JFrame is initially not shown - setVisible(true) changes that.

Basic JFrame

Page 4: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

HOW TO START A SWING APPLICATION Since Swing controls the user interface, it needs to handle keystrokes, mouse clicks and drags and so on. These are treated as events, and you write pieces of code called event handlers to control what happens when the user clicks a button and so on. This is described in details later on.

When an application which uses Swing starts, an additional thread is started which receives and dispatches events to the appropriate handler. This event dispatching thread is additional to the initial application thread which runs main.

However Swing code is not threadsafe. This means the thread of main and the event-dispatching thread are not synchronised, and deadlock between them is possible. This problem is solved if all code to create and run the UI is done in the event dispatching thread. A class calledSwingUtilities has a static method for doing this, which can be used like this, in code you would probably have in main:

SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } });

This defines an anonymous inner class which implements the Runnable interface, and passes an instance of that to the invokeLater method. The createAndShowGUI is a method that you write, possibly in the class that has main. For example, the following does what the first piece of code did, but without the thread problem. It also sub-classes JFrame:

class Test { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } static void createAndShowGUI() { MyFrame frame = new MyFrame("A Window"); } } class MyFrame extends JFrame { MyFrame(String title) { super(title); setBounds(0, 0, 400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }

In the listings which follow, we are mostly concerned with the frame constructor, and sometimes extra methods in the MyFrame class for event-handling.

Page 5: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

LABELS, TEXT BOXES AND

BUTTONS A JLabel displays some text which the user cannot change. A JTextField is a text entry field which the user can type into. A JButton is a clickable button.

This is produced by the following (code in the class containing main is as the last listing):

class MyFrame extends JFrame { MyFrame(String title) { super(title); setBounds(0, 0, 250, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLayout(null); JLabel label = new JLabel("Enter a number"); add(label); label.setBounds(5, 5, 100, 20); JTextField textField = new JTextField("1234"); add(textField); textField.setBounds(110, 5, 100, 20); JButton button = new JButton("OK"); add(button); button.setBounds(5, 30, 80, 20); } }

setLayout(null); concerns the use of a layout manager, which is a software agent which controls how the components are laid out. The null means we do not want to use a layout manager, and we will position components ourselves. This is not a good idea, but it’s a simple start.

Then we instantiate a new JLabel, add it to the window, and use setBounds to size and position it.

Similarly we make a text field, add it and position it, and the same for a JButton.

Simple Swing widgets

Page 6: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

HANDLING BUTTON CLICK EVENTS When the button is clicked, it generates an ActionEvent object. This is dealt with as follows:

1. When we create the button, we tell it where it should send its ActionEvent objects when it is clicked. This is done using the addActionListener method.

2. The object which receives the ActionEvents must implement the ActionListener interface.

3. That interface has just one method, called actionPerformed. The actionPerformed method will be executed when the button is clicked, so you write there what you want to happen when the button is clicked.

This plan can be carried out in different ways - in particular, different choices of object for step 2. This is often done using anonymous inner classes, which is rather obscure syntax. A simpler approach is to have the container object - the JFrame - receive the ActionEvent clicks. As follows:

class MyFrame extends JFrame implements ActionListener { MyFrame(String title) { .. as before.. JButton button = new JButton("OK"); add(button); button.setBounds(5, 30, 80, 20); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println("You clicked"); } }

Now our frame implements the ActionListener interface, which is the actionPerformed method. We have told the button to addActionListener this - so its ActionEvents will be sent to the frame object, and its actionPerformed method is called.

WHICH BUTTON? Suppose we have more than one button - which is usually the case. They will often call the same actionPerformed method. How do we tell which button was clicked? The ActionEvent class has a getSource() method to tell us who produced the event. We need to make the buttons data members, not local:

class MyFrame extends JFrame implements ActionListener { JButton okButton, cancelButton; MyFrame(String title) { .. as before .. okButton = new JButton("OK"); add(okButton); okButton.setBounds(5, 30, 80, 20); okButton.addActionListener(this); cancelButton = new JButton("Cancel"); add(cancelButton); cancelButton.setBounds(90, 30, 80, 20); cancelButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == okButton) { System.out.println("OK"); } if (source == cancelButton) { System.out.println("Cancel");

Page 7: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

} } }

EVENT HANDLING IN GENERAL

ActionEvent is a sub-class of AWTEvent. This has a superclass with classes of types of events of different kinds - for example, a MouseEvent, a MouseWheelEvent, a KeyEvent and a WindowEvent, the latter happening when a window is closed, resized, restored and so on. These are all handled in a similar way. Their events are passed to an object which implements the appropriate Listener interface. A getSource() can distinguish events coming from different widgets.

Widgets actually maintain a list of objects to notify. So when a button is clicked, it can notify a set of objects. This is why the method is addActionListener rather than setActionListener.

Two examples follow. Firstly this outputs the x and y pixel positions of the mouse as it moves over the frame:

class MyFrame extends JFrame implements MouseMotionListener { MyFrame(String title) { .. as before.. addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { System.out.println(e.getX()+ " "+e.getY()); } }

Second example - when the window close button is clicked, the user must enter q to quit. This is a useful approach, checking if the user really wants to quit, save work and so on:

class MyFrame extends JFrame implements WindowListener { MyFrame(String title) { ..as before.. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); .. as before.. addWindowListener(this); } public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { System.out.println("Enter q to quit"); Scanner scanner=new Scanner(System.in); String s = scanner.nextLine(); if (s.equals("q")) System.exit(0); } public void windowClosed(WindowEvent e) { }

Page 8: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } }

USING ADAPTER CLASSES

In the last example, the WindowListener interface has seven methods, and we are only doing something in one of them. To make for less typing, we can use an instance of a sub-class of the abstract WindowAdapter, just implementing the methods we want to. This is often done by making the object to be an instance of an anonymous inner class - like this:

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Enter q to quit"); Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); if (s.equals("q")) { System.exit(0); } } });

This is in place of addWindowListener(this); and the frame no longer implements WindowListener. Instead ..new WindowAdapter().. defines an anonymous sub-class of WindowAdapter, and creates an instance of it to deal with the WindowEvents.

Page 9: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

IMAGES Swing has a class called ImageIcon which can be used to place images on labels and other components. For example:

MyFrame(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); ImageIcon icon = new ImageIcon("c:/temp/baby.jpg"); JLabel label = new JLabel(icon); add(label); pack(); }

This is very simple, but there is a problem in that an absolute pathname to the image file is given. The code above works if the file is in the folder called temp on drive c - but in a typical deployment of an application, you cannot place a file in a fixed folder on the user's computer. We need to be able to somehow place the file in the application and refer to it there.

There are three steps to this:

1. Place the image file in a suitable folder within the folder holding the source code .java files for the relevant package. For example, this is in a package called test, so we could place the image in a sub-folder of test called images.

2. Use a classloader to get the location relative to where the class files will be loaded from:

package test; import .. class Test { public static void main(String[] args) { .. } .. } class MyFrame extends JFrame { JPanel top, bottom; MyFrame(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); ClassLoader cldr = this.getClass().getClassLoader(); java.net.URL imageURL = cldr.getResource("test/images/baby.jpg"); ImageIcon icon = new ImageIcon(imageURL); JLabel label = new JLabel(icon); add(label); pack(); } }

Here, cldr is the class loader which will load the MyFrame class file. Its getResource method gets a location URL for the file named baby.jpg, in the folder called images in the package called test.

3. When this is compiled (or Run in NetBeans), in the classes folder of the build folder, there will be a folder named test to hold the package, and it will contain the class file, and a sub-folder called image with the image file in it.

If this is jar'd (in NetBeans, Run Clean and Build) this produces a .jar file (in NetBeans, in the dist folder) which contains the image file.

Image on label

Source code image placement in NetBeans

Folders after compiling

Page 10: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

IMAGES FROM THE NET Alternatively, image files can be downloaded from the net, very simply:

class MyFrame extends JFrame { MyFrame(String title) { .. try { URL imageURL = new URL("http://p.ebaystatic.com/aw/pics/logos/logoEbay_x45.gif"); ImageIcon icon = new ImageIcon(imageURL); JLabel label = new JLabel(icon); add(label); } catch (MalformedURLException ex) { } pack(); } }

Image from the net

Page 11: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

COLOR Color is a class, with objects representing colours. We can construct a new colour by supplying red, green and blue components as floats in the range 0 to 1, like

Color c = new Color(1.0f, 0.5f, 0.1f);

1.0f is a float constant, while 1.0 is type double.

Or there are a set of constant named colours, like

Color c = Color.GREEN;

Swing components have methods setBackground and setForeground to set the colours (not all components have backgrounds and foregrounds). For example:

class MyFrame extends JFrame { MyFrame(String title) { .. as before .. JLabel label = new JLabel("Enter a number"); add(label); Color c1 = new Color(1.0f, 0.5f, 0.1f); label.setForeground(c1); label.setBounds(5, 5, 100, 20); JTextField textField = new JTextField("1234"); Color c2 = Color.GREEN; textField.setBackground(c2); add(textField); textField.setBounds(110, 5, 100, 20); } }

Using Color

Page 12: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

FONTS An instance of the Font class represents a font. The main issue is that fonts are installed locally, so there is no guarantee that a specific font will be available. This is addressed by having two kinds - 'logical' and 'physical'. The logical fonts are a basic set of generic fonts - Dialog, DialogInput, Monospaced, Serif and SansSerif. You can instantiate one of these fonts as for example

Font font = new Font("Dialog", Font.PLAIN, 12);

and then use the setFont method to apply it to a component, such as

label.setFont(font);

The same is true of the physical fonts, but you have to ensure that the font you want is actually available. A reasonable approach is to have a set of fonts which you would prefer to use, in order, and use a logical font as the fallback if none are present. For example:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontName = "SansSerif"; String f[] = ge.getAvailableFontFamilyNames(); String[] wantedFonts = {"Code2000","Linux Libertine","DejaVu Sans" }; boolean gotFont = false; for (int j = 0; j < wantedFonts.length; j++) { for (int i = 0; i < f.length; i++) { if (f[i].equals(wantedFonts[j])) { fontName = wantedFonts[j]; gotFont = true; break; } } if (gotFont) break; } Font font = new Font(fontName, Font.PLAIN, 16); JLabel label = new JLabel("αβγδεζگڳڳڳڳڱڱ in " + fontName); label.setFont(font); add(label);

This example shows two other points. Firstly, since Java uses Unicode for characters and strings, it can display a very large set of characters.

Secondly, such characters can be used in source code for identifiers and string constants. Whether you can see them depends on which font your editor uses in your IDE - which will be an option you can change. Otherwise you can represent Unicode characters as constants like \u0061 for lower case 'a'.

Unicode font

Page 13: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

CONTAINERS AND STRUCTURE Swing provides various features to enable you to organise components in a frame as you want. We look at JPanels as a basic building block, borders, the idea of a containment hierarchy, the top-level containers, layout managers, tabbed panes and split panes.

JPANELS AND BORDERS

A JPanel is a rectangular area which can be used to divide a frame by adding the panels to the frame. We can also add panels to panels, making the layout structure as intricate as is needed. We need to take the layout manager into account. The default for a JFrame is BorderLayout, and for a JPanel, FlowLayout, which adds components in rows. But this can be changed by setLayout().

Borders can also be added to JPanels - and most components. To get a border, use a method from the static BorderFactory class. The idea is that if you use three thin blue borders in your application, in fact only one will be constructed by the BorderFactory, and you will share the instances.

The example shows the use of a compound border, with the outer border being empty. This can be used to establish 'padding' around components:

MyFrame(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // make panels top = new JPanel(); bottom = new JPanel(); // set sizes top.setPreferredSize(new Dimension(100, 100)); bottom.setPreferredSize(new Dimension(100, 100)); // add to frame add(top, BorderLayout.NORTH); add(bottom, BorderLayout.SOUTH); // put labels in panels top.add(new JLabel("Top")); bottom.add(new JLabel("Bottom")); // make and set borders Border redBorder=BorderFactory.createLineBorder(Color.red, 1); Border blankBorder=BorderFactory.createEmptyBorder(5,5,5,5); Border joint = BorderFactory.createCompoundBorder(blankBorder, redBorder); top.setBorder(joint); pack(); }

ADDING COMPONENTS TO A JPANEL We can create a JPanel and add it to the window. Then we can create components and add them to the panel (not the window):

MyFrame(String title) { .. as before.. // set up a panel on the left JPanel yellow = new JPanel(); add(yellow); yellow.setBounds(0,0,125,100); yellow.setBackground(Color.YELLOW); // add a label to it JLabel label = new JLabel("Enter a number"); label.setBounds(5, 5, 100, 20); yellow.add(label);

Page 14: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

// set up a panel on the right JPanel red = new JPanel(); add(red); red.setBounds(125,0,125,100); red.setBackground(Color.RED); // add a textfield to it JTextField textField = new JTextField("1234"); red.add(textField); textField.setBounds(110, 5, 100, 20); }

THE CONTAINMENT HIERARCHY

In the above example, the JFrame contains two JPanels, because we have added them. In turn, the left JPanel contains a JLabel, and the right contains a JTextField.

Swing uses this concept in general, with all components being contained in other components. Only at the top of each containment tree do we find a top level container, which in this case is a JFrame.

TOP LEVEL CONTAINERS There are three useful top level containers JFrame, JDialog and JApplet. The first we have already seen. A JDialog is suitable for use as a dialog box and is descibed later . A JApplet will contain an applet, normally inside a browser window.

There is a fourth, but it is usually excluded from the useful list. This is a JWindow. This is a plain rectangle, with no title bar, re-sizing handles, nor close minimize restore buttons. So not useful.

On JDK 7, on some platforms, you can have translucent and non-rectangular windows. See later.

JROOTPANE AND COMPONENTS JFrame and the other top-level containers is actually slightly more complicated than it appears. In detail,

A Jframe has a JRootPane

A JFrame has glassPane and a Layered Pane

A LayeredPane has a contentPane, and maybe a MenuBar

Usually we are only interested in the contentPane -the area inside the window where things will be shown. When we place a JLabel in a JFrame, it is added to the contentPane:

frame.getContentPane().add(label);

Since this is so common, the add method of JFrame does this for you, so you can just say

frame.add(label);

with the same effect.

Menu bars are easy to use - they are described elsewhere.

The glassPane usually does nothing. It can be used to intercept mouse events before they reach the frame components, and you can draw on the glassPane (and therefore over frame components), provided you make it visible.

USING THE GLASSPANE For example, the following gets the glassPane, and make sit intercept mouse movements. In response it draws on the glassPane tracking the mouse. The mouse events interception only works if the glassPane is setVisible(); which might not be expected:

class MyFrame extends JFrame implements MouseMotionListener {

Containment hierarchy

Drawing on the glassPane

JRootPane and elements

Page 15: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

Component glassPane; MyFrame(String title) { ..as before.. glassPane = getGlassPane(); glassPane.setVisible(true); glassPane.addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { } int lastMouseX = -1; int lastMouseY = -1; public void mouseMoved(MouseEvent e) { int mouseX = e.getX(); // where's the mouse int mouseY = e.getY(); if (lastMouseX == -1) { // if first time, last mouse is here as well lastMouseX = mouseX; } if (lastMouseY == -1) { lastMouseY = mouseY; } Graphics g = glassPane.getGraphics(); // get a graphics context g.drawLine(lastMouseX, lastMouseY, mouseX, mouseY); lastMouseX = mouseX; //ready for next time lastMouseY = mouseY; } }

This consumes the mouse event, and the underlying button does not receive it. If you want to pass it on, you have to work out if the mouse is over the button, and if so, construct a suitable event and send it on. See

http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

LAYOUT MANAGERS Previously we have used setBounds() to position components with pixel co-ordinates and sizes. This is not recommended. The size of a pixel depends on the resolution of the display, and so your components will be smaller on a higher resolution screen. Insead it is better to use a LayoutManager. There is a choice of these, and they place components in different patterns - or you can write your own. We will just look at two.

BORDERLAYOUT This is the default for a JFrame. Components can be placed in one of five positions, named north south east west and center. For example:

class MyFrame extends JFrame { MyFrame(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); JButton okButton = new JButton("OK"); add(okButton, BorderLayout.WEST); JButton cancelButton = new JButton("Cancel"); add(cancelButton, BorderLayout.EAST); pack(); } }

Using BorderLayout

Page 16: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

We have not set a layout manager, so we get the default BorderLayout. We have not sized the frame or the components, but we have called pack(). This calculates the "preferred sizes" of the components, sizes them to that, positions them, and calculates the minimum size of the frame.

The five locations may seem to be limiting. But you can add panels to the frame, then components to the panels, and so on.

GRIDBAGLAYOUT Some say GridBagLayout is one of the more complicated layout managers, but it is very flexible and effective. Components are placed in cells in rows and columns. You use an instance of GridBagConstraints when you add a component. This has fields gridx and gridy which fix which column and row it goes in - top left cell is (0,0). You can fix the alignment with .anchor EAST or WEST and so on. For a large component, you can make it use more than one row or column using gridWidth or gridHeight. And you can space out components using insets. For example:

setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.insets = new Insets(5, 5, 5, 5);// everything has a 5 pixel border JLabel label1 = new JLabel("Name"); gc.gridx = 0; // top left gc.gridy = 0; gc.anchor = GridBagConstraints.EAST; // aligned right add(label1, gc); // add it

The whole thing is:

MyFrame(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.insets = new Insets(5, 5, 5, 5); // top row JLabel label1 = new JLabel("Name"); gc.gridx = 0; gc.gridy = 0; gc.anchor = GridBagConstraints.EAST; add(label1, gc); JTextField name = new JTextField(""); name.setPreferredSize(new Dimension(100, 20)); gc.gridx = 1; gc.gridy = 0; gc.anchor = GridBagConstraints.WEST; add(name, gc); // row 2 JLabel label2 = new JLabel("Date of birth"); gc.gridx = 0; gc.gridy = 1; gc.anchor = GridBagConstraints.EAST; add(label2, gc); JTextField dob = new JTextField(""); dob.setPreferredSize(new Dimension(100, 20)); gc.gridx = 1; gc.gridy = 1; gc.anchor = GridBagConstraints.WEST; add(dob, gc); JButton okButton = new JButton("OK"); gc.gridx = 0; gc.gridy = 2; // row 3 gc.anchor = GridBagConstraints.CENTER; add(okButton, gc); JButton cancelButton = new JButton("Cancel");

GridBagLayout

Page 17: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

gc.gridx = 1; gc.gridy = 2; gc.anchor = GridBagConstraints.CENTER; add(cancelButton, gc); pack(); }

JTABBEDPANE

A tabbed pane produces a set of panes accessible via a 'tab'. This means that a lot of interface can be there in a small area - though you can't see more than one at a time.

The sequence is simply:

1. Construct a JTabbedPane

2. Construct components, and add them as tabs to the tabbed pane. These might be JPanels

3. Add the Tabbed pane to the outer container.

For example:

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); JPanel panel1 = new JPanel(); tabbedPane.addTab("One", panel1); JPanel panel2 = new JPanel(); tabbedPane.addTab("Two", panel2); .. add(tabbedPane);

and of course you would add components to the panels.

As well as text labels for tabs, you can have icons as well. So if 'one' is an ImageIcon, you can say

tabbedPane.addTab("",one, panel1);

and so on.

JSPLITPANE A JSplitPane object does what the name suggests - it splits a container into two parts, and has the functionality to allow the user to control the split. You would typically:

1. Make the things which would be the left and right components - typically, JPanels. You would probably add components to those two panels.

2. Construct the JSplitPane, with the two panels as arguments.

3. Add the JSplitPane to the enclosing container - the one you are splitting.

For example - the left component will be a scroll pane enclosing a JList

JScrollPane listScroller = new JScrollPane(list);

and the right is just a JPanel:

JPanel rightPane = new JPanel(); rightPane.setBackground(Color.yellow); JLabel lab1= new JLabel("Right pane"); rightPane.add(lab1);

Then make the split pane:

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScroller, rightPane);

and add it - this is in the constructor of a subclassed JFrame:

JTabbedPane

Icons for tab labels

JSplitPane

Page 18: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

add(splitPane);

Some code configures this, with obvious meaning:

splitPane.setOneTouchExpandable(true); splitPane.setDividerLocation(150); Dimension minimumSize = new Dimension(100, 50); listScroller.setMinimumSize(minimumSize); rightPane.setMinimumSize(minimumSize);

For a three-way split, nest them. For example, you might split a frame vertically, then split the top pane horizontally.

Page 19: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

MENUS, POPUPS ANDTOOLBARS

MAKING A BASIC MENU Follow these steps

1. Make a menubar, and add it to the frame (or whatever)

2. Make a menu, and add it to the menubar

3. Make menu items, and add them to the menu

Repeat steps two and three for as many menus as you want.

For example:

JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu1 = new JMenu("Menu One"); menuBar.add(menu1); JMenuItem item1 = new JMenuItem("Option One"); menu1.add(item1); JMenuItem item2 = new JMenuItem("Option Two"); menu1.add(item2); JMenu menu2 = new JMenu("Menu Two"); menuBar.add(menu2); JMenuItem item3 = new JMenuItem("Option Three"); menu2.add(item3); JMenuItem item4 = new JMenuItem("Option Four"); menu2.add(item4);

MAKING THE MENU WORK JMenuItems are in fact sub-classed JButtons, and so they use the ActionListener interface and you can treat them like ordinary buttons. This means your JMenuItems would need to be data fields not local to the constructor, so the actionPerformed can refer to them. So we modify the above:

.. item1 = new JMenuItem("Option One"); menu1.add(item1); item1.addActionListener(this); .. public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source==item1) .. whatever }

ELABORATING MENUS You can add menuItems to menu bars. Or you can add menus to menus, giving submenus.

You can have icons as well as text labels in menuItems.

You can have a menu separator.

You can have checkboxes and radio buttons in menus.

For example, where 'one' is an ImageIcon:

JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu1 = new JMenu("Menu One");

Basic menu

Elaborated menu

Page 20: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

menuBar.add(menu1); item1 = new JMenuItem("Option One"); menu1.add(item1); item1.addActionListener(this); JMenuItem item2 = new JMenuItem("Option Two", two); menu1.addSeparator(); menu1.add(item2); JCheckBox cb = new JCheckBox("Yes or no"); menu1.add(cb);

POPUP MENUS Popup menus, which appear floating somewhere in a frame (uually where the mouse was clicked) are excellent. They involve less mouse movement than having to go up to the menu bar, and they can be context-dependent - that is, if the user right-clicks on a particular thing, they get a menu just relevant to that thing.

You make a popup menu and add items to it very simply:

popup = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("A popup menu item"); popup.add(menuItem); JMenuItem menuItem2 = new JMenuItem("Another popup menu item"); popup.add(menuItem2);

To make it work, you have to do two things - get the menu to pop up, and get responses to menu selections. The first is done using a mouse listener. For example:

addMouseListener(this); .. public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { popup.show(e.getComponent(), e.getX(), e.getY()); } }

which means the popup appears if they right-click. The e.getX() and getY mean the menu will appear where they clicked.

To make the menu work - the JMenuItems are subclassed JButtons, so use actionListener just as for menu bars.

MENU DESIGN ISSUES Users often complain they cannot 'find' things. This happens when there is a large and complex menu, with many levels, and items are not logically placed. For example, in Word 2003 and before you inserted a header through View in the menu, which is hardly logical.

Some suggestions are:

1. Order top level items as expected - File Edit …..Help. In File have New, Open, Save, Save as, Close and Exit. This obviously depends on the application, but keep it as close to this - which users will expect - as possible.

2. Use separators to group items logically.

3. If your menus are four deep - find another way.

TOOLBARS

Toolbars are rows of buttons, which can 'float' in their own window, or will 'dock' at the edge of a container. Instantiate a JToolBar, make and add JButtons to it, then add the toolbar to a container using BorderLayout. The JButtons would normally show icons, but text is possible. The JButtons use actionPerformed. For example:

Page 21: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

ClassLoader cldr = this.getClass().getClassLoader(); java.net.URL imageURL = cldr.getResource("test/images/Edit24.gif"); ImageIcon one = new ImageIcon(imageURL); .. JToolBar toolBar = new JToolBar("Tools"); JButton butt1 = new JButton(one); .. toolBar.add(butt1); .. add(toolBar, BorderLayout.NORTH);

Initial toolbar Docked on the left Dragged to new

window

Page 22: The Java Swing Toolkitwaltermilner.com/java/swing.pdf · The AWT started with JDK 1.0 AWT used OS native code to draw its widgets. Swing uses Java to do a lot of the drawing. AWT

The Java Swing Toolkit

Introduction to Swing

SUBCLASSING WIDGETS You can set attribute such as fonts and colours for components. For an application you will be likely to want these to be consistent. You can do this by setting the attributes of each widget, but ideally you would have in effect a style sheet. One way to do this is to subclass a standard widget, giving the class the attributes that you want, and instantiating the subclass.

For example, a JButton can be made to show text and an icon, and to display only that, and not a button 'body'. We can also set an alternative icon to be displayed when the mouse is rolled over the JButton. To use this consistently we can subclass JButton. All instances can share the same two icons, so these can be static fields. We can load these in a static initialisation block:

class MyButton extends JButton { static ImageIcon red; static ImageIcon yellow; static { ClassLoader cldr = MyButton.class.getClassLoader(); java.net.URL imageURL = cldr.getResource("test/images/red.gif"); red = new ImageIcon(imageURL); imageURL = cldr.getResource("test/images/yellow.gif"); yellow = new ImageIcon(imageURL); } MyButton(String text) { super(text); setIcon(red); setRolloverIcon(yellow); setContentAreaFilled(false); setBorderPainted(false); setFocusPainted(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); } }

Sub-classed JButtons