Lectures 13-14

advertisement
CE203 - Application Programming
Part 5
Autumn 2015
CE203 Part 5
1
Inner Classes 1
In Java a class may be declared within another class; such a
class is called an inner class. The methods of an inner class
may access private instance variables and methods of the
enclosing class.
Inner classes are frequently used for the event-handlers in
applets and GUI programs. On the following slides we
rewrite the square-calculating applet from part 2 of the slides
using this approach. Since the inner class may access the
members of the Arith2 class so it is no longer necessary to
store a reference to the applet in the TextHandler object;
hence no constructor is needed.
Autumn 2015
CE203 Part 5
2
Inner Classes 2
// usual imports needed
public class Arith2 extends JApplet
{ private JTextField input, result;
public void init()
{ JLabel prompt = new JLabel(
"Type a number and press return");
input = new JTextField(5);
result = new JTextField(25);
result.setEditable(false);
}
setLayout(new FlowLayout());
add(prompt); add(input);
add(result);
input.addActionListener(new TextHandler());
Autumn 2015
CE203 Part 5
3
Inner Classes 3
// class Arith2 continued
private class TextHandler
implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ String txt = e.getActionCommand();
input.setText("");
String answer = null;
try
{ int n = Integer.parseInt(txt.trim());
answer = "The square is " + n*n; }
catch(Exception ex)
{ answer = "Invalid input";}
result.setText(answer);
}
} // end of inner class
} // end of Arith class
Autumn 2015
CE203 Part 5
4
Inner Classes 4
Note that, although the inner class was declared as private, its
actionPerformed method had to be declared as public since
the class must correctly implement the ActionListener
interface.
Unless an inner class is declared as static, objects of the class
must belong to objects of an outer class so an inner class
object can be created only in the non-static methods of the
enclosing class.
Autumn 2015
CE203 Part 5
5
Inner Classes 5
If an inner class is public we can refer to objects of that class
outside of the enclosing class.
The following slides present a student class, which contains a
public inner class to hold details of all of the student’s
examination marks. (The bodies for some of the inner class
methods are omitted.)
To refer to the inner class (Marks) outside of the Student
class we need to use Student.Marks. (We have already
encountered this notation – Entry is an inner interface of
Map.)
Autumn 2015
CE203 Part 5
6
Inner Classes 6
public class Student extends Person
{ private int regno;
private Marks marks;
public Student(String name, Date dob,
int regnum)
{ super(name, dob);
regno = regnum;
marks = new Marks();
}
public int regNumber()
{ return regNo;
}
// will inherit methods to get name and dob
Autumn 2015
CE203 Part 5
7
Inner Classes 7
// class Student continued
public void addMark(String module,
float mark)
{ marks.add(module, mark);
}
public Marks getMarks()
{ return marks;
}
public String toString()
{ // body omitted
}
Autumn 2015
CE203 Part 5
8
Inner Classes 8
// class Student continued
public class Marks
{ private HashMap<String, Float> map;
public Marks()
{ map = new HashMap<String, Float>(); }
public void add(String module, float mark)
{ map.add(module, mark); }
public float get(String module)
{ return map.get(module).floatValue(); }
public float getAverage() { …… }
public String toString() { …… }
}
}
Autumn 2015
CE203 Part 5
9
Inner Classes 9
Assuming that the variable lisa refers to a Student object
holding details about a student called Lisa, we could retrieve
and print her marks for CE203 and CE204 using the
following code.
Student.Marks m = lisa.getMarks();
System.out.println("CE203: " +
m.get("CE203") +
"CE204: " +
m.get(CE204"));
Autumn 2015
CE203 Part 5
10
Anonymous Inner Classes 1
When writing applets or GUI components it is often found
that an inner class that implements the ActionListener
interface has just a single object which is simply created and
passed as an argument to the addActionListener method;
this is indeed the case for the Arith applet on slides 65-66.
The object is effectively anonymous – no variable in our
program refers to it. We can take this concept one step
further by using an anonymous inner class, which has no
name and is simply declared when it is needed.
An anonymous inner class must either implement an interface
or extend an existing class.
Autumn 2015
CE203 Part 5
11
Anonymous Inner Classes 2
The following slide shows how we may rewrite the Arith2
applet using an anonymous inner class.
We replace the call to the TextHandler constructor by a call
of the form new ActionListener(){……} with the body of
the text-handling class in the braces; since this call is an
argument to a method it is important not to forget to place a
closing parenthesis and semicolon after the brace.
Note that, despite the syntax, this code does not actually
make a call to an ActionListener constructor (since an
interface does not have a constructor); instead the default
constructor of the anonymous class is called.
Autumn 2015
CE203 Part 5
12
Anonymous Inner Classes 3
// usual imports needed
public class Arith3 extends JApplet
{ private JTextField input, result;
public void init()
{ JLabel prompt = new JLabel(
"Type a number and press return");
input = new JTextField(5);
result = new JTextField(25);
result.setEditable(false);
setLayout(new FlowLayout());
add(prompt);
add(input);
add(result);
Autumn 2015
CE203 Part 5
13
Anonymous Inner Classes 4
// Arith 2 init method continued
input.addActionListener(
new ActionListener()
{ public void actionPerformed(
ActionEvent e)
{ // method body as on slide 4
}
}
);
}
}
Autumn 2015
CE203 Part 5
14
Anonymous Inner Classes 5
It is not possible to provide a constructor for an anonymous
inner class, since a constructor must have the same name as
the class and an anonymous inner class has no name. Hence
the compiler will always generate a default constructor which
will call the no-argument constructors for any object instance
variables. Hence, if an anonymous class has object instance
variables they must be of classes that have no-argument
constructors.
Additionally, if the anonymous class extends another class
the constructor will call the no-argument constructor for the
superclass so we cannot use anonymous inner classes to
extend classes that have no such constructor.
Autumn 2015
CE203 Part 5
15
Anonymous Inner Classes 6
We have seen an example of an anonymous inner class that
implements an interface. The most common situation where
we might use such a class to extend an existing class is when
our class contains methods to replace inherited default
versions. The GreetingPanel class from the program in
part 1 of the slides is a typical example of such a class. Only
one object of this class was created and the variable called
panel that referred to it can easily be eliminated from the
program. On the following slides we rewrite the program
using an anonymous inner class.
Autumn 2015
CE203 Part 5
16
Anonymous Inner Classes 7
import javax.swing.*;
import java.awt.*;
public class Greeting3 extends JApplet
{ public void init()
{ add(new JLabel("Hello"),
BorderLayout.NORTH);
// init method continued on next slide
// previous version had
//
add(new GreetingPanel(),
//
BorderLayout.CENTER);
Autumn 2015
CE203 Part 5
17
Anonymous Inner Classes 8
// Greeting3 init method continued
add(new JPanel()
{ public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.setColor(Color.blue);
g.drawRect(50, 100, 40, 30);
g.fillRect(120, 100, 30, 40);
}
},
BorderLayout.CENTER);
}
}
Autumn 2015
CE203 Part 5
18
Using Frames 1
We have seen various GUI examples written as applets using
classes that extend the JApplet class. We shall now see how
similar examples can be produced in applications using
classes that extend the JFrame class. We start with a framebased version of the simple greeting program from part 1.
(The usual import statements are needed, but have been
omitted due to lack of space on the slide.)
Autumn 2015
CE203 Part 5
19
Using Frames 2
public class Greeting extends JFrame
{ public Greeting()
{ super("Greeting example");
setSize(300, 200);
setVisible(true);
}
public void paint(Graphics g)
{ super.paint(g);
g.drawString("Hello!", 20, 20);
g.setColor(Color.blue);
g.drawRect(50, 100, 40, 30);
g.fillRect(120, 100, 30, 40);
}
}
Autumn 2015
CE203 Part 5
20
Using Frames 3
The JFrame class has a constructor that takes a string
argument to specify the contents of the title bar of the
window containing the frame. We have invoked this in our
constructor using super. We have also used the constructor
to specify the size of the frame.
A frame is not displayed until it is explicitly made visible so
we need to make a call to the setVisible method; this could
be done elsewhere (e.g. in a main method that creates a frame
object) if we did not write a constructor.
Autumn 2015
CE203 Part 5
21
Using Frames 4
We need a main method to create a Greeting object; this
could be placed in a separate class in a separate file:
public class Greet
{ public static void main(String args[])
{ JFrame myFrame = new Greeting();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
Autumn 2015
CE203 Part 5
22
Using Frames 5
The setDefaultCloseOperation method is used to specify
what should happen when the user clicks the close-box of the
window containing the frame; this will usually be the
termination of the program, as in this example. Other options
include DISPOSE_ON_CLOSE, which causes the window to be
closed whilst leaving the program running, and
DO_NOTHING_ON_CLOSE, which indicates that the click
should be ignored.
Autumn 2015
CE203 Part 5
23
Using Frames 6
There may be circumstances in which we want to do
something more specific when the window is closed – for
example if the program has created several frames we may
wish to decrement the frame count and terminate only if it
has reached zero. In such circumstances it is necessary to add
a window listener (which is similar to an action listener) to
the frame.
Autumn 2015
CE203 Part 5
24
Using Frames 7
In most cases classes that extend JFrame are written for a
specific program and not intended for use in other programs.
It is often more convenient to write the main method inside
the frame class instead of creating a separate class:
Autumn 2015
CE203 Part 5
25
Using Frames 8
public class Greeting extends JFrame
{ public Greeting()
{ super("Greeting example");
setSize(300, 200);
setVisible(true);
}
public void paint(Graphics g)
{ // as before
}
public static void main(String args[])
{ JFrame myFrame = new Greeting();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
Autumn 2015
CE203 Part 5
26
Using Frames 9
We can add components to the content pane of a JFrame
object in the same way as we add them to the content pane of
a JApplet object. However, since the JFrame class does not
have an init method, the creation of the components must
be performed in the constructor. The default layout manager
for a frame is, as with applets, BorderLayout.
On the next slides there is an outline of a frame-based version
of the square-calculating class from part 2. The action listener
class, which has been omitted, should be identical to the one
seen previously.
Autumn 2015
CE203 Part 5
27
Using Frames 10
// Arith.java – frame version
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Arith extends JFrame
{ JTextField input, result;
public static void main(String args[])
{ JFrame myFrame = new Arith();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
// continued on next slide
Autumn 2015
CE203 Part 5
28
Using Frames 11
public Arith()
{ super("Squares of numbers");
input = new JTextField(5);
input.addActionListener(……);
result = new JTextField(25);
result.setEditable(false);
setLayout(new FlowLayout());
add(new JLabel(
"Type a number and press return"));
add(input); add(result);
setSize(300, 200); setVisible(true);
}
}
Autumn 2015
CE203 Part 5
29
Text Areas 1
The JTextArea class permits the display and manipulation of
multiple lines of text.
This class has a constructor with two arguments indicating
the number of visible rows and columns – to create a text area
whose visible area comprises 12 rows of 20 characters we
could use
JTextArea myTextArea = new JTextArea(12,20);
When this two-argument constructor is used the area will
initially contain no text.
Autumn 2015
CE203 Part 5
30
Text Areas 2
There is also a constructor that takes as an argument a string
indicating the initial contents. The number of visible rows
and columns will be determined by the number of lines of
text in the string and the length of the longest line.
A third constructor is available – this takes a string and two
integers, allowing the specification of both the initial contents
and the number of visible rows and columns:
JTextArea myTextArea2 = new JTextArea(
"This is the first line\n"+
"and this is the second line",
5,30);
Autumn 2015
CE203 Part 5
31
Text Areas 3
The contents of a JTextArea object may be changed using
the setText method:
myTextArea.setText("line 1");
Additional text may be added to the end of the contents using
the append method:
myTextArea.append("\nline 2");
The class has a setEditable method similar to that of the
JTextField class; if this is not used the area is by default
editable.
Autumn 2015
CE203 Part 5
32
Text Areas 4
It is not possible to add an action listener to a text area; text
input by a user would normally be processed in a buttonhandler which examines the contents of the text area using
the getText method.
A user may use the mouse to select part of the contents of a
text area; there is a method called getSelectedText that
returns the contents of the currently-selected part of the area.
We could use this in a statement such as
myTextArea2.setText(
myTextArea1.getSelectedText());
Autumn 2015
CE203 Part 5
33
Text Areas 5
If a line of text in a text area is too long to be displayed in the
visible area only part of the line will be visible. The user can
however see the rest of the line by using the cursor keys.
However it is sometimes preferable to make the whole width
of the text visible by using line wrapping, i.e. the display of a
long line is continued on the next line of the text area. To
enable this, the setLineWrap method should be used.
myTextArea.setLineWrap(true);
Autumn 2015
CE203 Part 5
34
Text Areas 6
Line wrapping will by default break the line after the last
visible column, resulting in words being split across two
lines. To enforce the break to occur between words it is
necessary to use
myTextArea.setWrapStyleWord(true);
The use of cursor keys to allow the non-visible parts of a text
area to be seen is not always convenient – if the visible area
is small in comparison to the size of the text the user may
need to press a cursor key many times or hold it down and
risk going too far. Scroll bars provide a more user-friendly
way of navigating text areas.
Autumn 2015
CE203 Part 5
35
Scroll Panes 1
The simplest way of providing scroll bars for a text area is to
place the area inside a scroll pane.
To do this we must create a JScrollPane object and add this
to a container instead of adding the text area directly.
A scroll pane may contain any object of a class that
implements the Scrollable interface. The only classes we
have seen that implement this interface are JTextField and
JTextArea.
Autumn 2015
CE203 Part 5
36
Scroll Panes 2
The scrollable component to be placed in a scroll pane is
usually supplied as an argument to the constructor, e.g.
add(new JScrollPane(myTextArea),
BorderLayout.CENTER);
The default behaviour of a scroll pane is to provide scroll
bars only when needed, i.e. when the number of rows or
columns of text exceeds the visible size. This policy may be
overridden by defining specific policies for horizontal and/or
vertical scroll bars.
Autumn 2015
CE203 Part 5
37
Scroll Panes 3
Scroll-bar policies may be specified as arguments to the
JScrollPane constructor – if this is done both policies must
be specified explicitly even if only one differs from the
default. To provide a vertical scroll bar but no horizontal
scroll bar we would use
add(new JScrollPane(myTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
If a scroll bar should be displayed only when needed, we
would
use
VERTICAL_SCROLLBAR_AS_NEEDED
or
HORIZONTAL_SCROLLBAR_AS_NEEDED
Autumn 2015
CE203 Part 5
38
Layout Managers 1
Earlier in this module we encountered two layout manager
classes, BorderLayout and FlowLayout, and saw that we
may specify a layout manager for a container by applying
setLayout. If this method is not called a border layout
manager is used for the container of an applet or frame, but a
flow layout manager is used for a panel.
Autumn 2015
CE203 Part 5
39
Layout Managers 2
When a BorderLayout or FlowLayout object is created it is
possible to specify the size of the horizontal and vertical gaps
that separate the components; if this is not done there will be
no gaps. To obtain horizontal spacing of 30 pixels and
vertical spacing of 20 pixels we could use
c.setLayout(new BorderLayout(30,20));
or
c.setLayout(new FlowLayout(30,20));
Autumn 2015
CE203 Part 5
40
Layout Managers 3
The spacing may be altered after the layout manager has been
created using setVgap and setHgap. If either of these is
called after components have been added to the container, the
layout of these components will not be updated unless we call
the method layoutContainer.
FlowLayout f = new FlowLayout(10,10);
c.setLayout(f);
// add several components to c
f.setHgap(40);
f.layoutContainer(c);
Autumn 2015
CE203 Part 5
41
Layout Managers 4
When a FlowLayout object is being used it is possible to
specify left- or right- alignment instead of the default centrealignment. This may be done using a constructor:
FlowLayout f = new FlowLayout(
FlowLayout.RIGHT);
or later by using the method setAlignment:
f.setAlignment(FlowLayout.CENTER);
If we wish to change the alignment of components that have
already been added we must, as on the previous slide, call the
method layoutContainer.
Autumn 2015
CE203 Part 5
42
Layout Managers 5
We can specify both the alignment and the gaps when
creating a FlowLayout object by using a three-argument
constructor:
c.setLayout(new FlowLayout(
FlowLayout.LEFT,20,10));
Autumn 2015
CE203 Part 5
43
The GridLayout Class 1
The GridLayout manager class may be used to arrange
components in rows and columns. This manager uses a
regular grid; each component will have the same size. The
following code fragment will place twelve buttons in three
rows of four.
c.setLayout(new GridLayout(3,4));
for (int i = 1; i<12; i++)
c.add(new JButton(""+i));
Autumn 2015
CE203 Part 5
44
The GridLayout Class 2
The two arguments passed to the constructor specify the
number of rows and number of columns. A four-argument
version is available which allows gaps between components
to be specified; a call of the form
c.setLayout(new GridLayout(5,6,10,12));
will create a grid comprising five rows and six columns with
a horizontal spacing between components of 10 pixels and a
vertical spacing of 12 pixels.
Autumn 2015
CE203 Part 5
45
The GridLayout Class 3
When components are added to a container using a grid
layout manager the grid is always filled row-by-row (starting
with the top row), each row being filled from left to right. If
gaps in the grid are required it is necessary to add dummy
components (e.g. empty labels).
Autumn 2015
CE203 Part 5
46
The BoxLayout Class
The BoxLayout class can be used to align a group of
components horizontally or vertically. It has a constructor
with two arguments specifying the identity of the container
and whether the layout is horizontal or vertical. The
following code would arrange a group of buttons aligned
vertically:
c.setLayout(new BoxLayout(c,
BoxLayout.Y_AXIS));
for (int i = 1; i<4; i++)
c.add(new JButton("Button "+i));
Autumn 2015
CE203 Part 5
47
The Box Class
Although box layout can be used as the manager for a panel it
is more convenient to create an object of type Box instead of
a JPanel object. This class always uses a box layout manager
so setLayout is not used. To arrange a group of buttons in a
box on the left of a frame or applet we could use
Box b = Box.createVerticalBox();
for (int i = 1; i<4; i++)
b.add(new JButton("Button "+i));
c.add(b, BorderLayout.WEST);
The Box class has methods that allow control of the spacing
between the components but these are quite complex.
Autumn 2015
CE203 Part 5
48
The GridBagLayout Class 1
To obtain more control over the positioning of components
on a grid it is possible to use the GridBagLayout class. This
allows components to be added in any order to specific
locations on the grid and to straddle multiple locations. It also
allows the sizes of the rows and columns to vary.
In order to use this manager an object of type
GridBagConstraints must be created. Details of where on
the grid a component is to be placed should be placed in this
object before the component is added to the grid; the
setConstraints method is used to apply these constraints
to the manager before calling add.
Autumn 2015
CE203 Part 5
49
The GridBagLayout Class 2
gridx and gridy instance variables of the
GridBagConstraints object are used to indicate in which
The
row and column a component is to be placed.
The code fragment on the next slide shows the placing of a
label in column 0 on row 0 of a grid, with a button in column
1 on row 0. A text field is added occupying both columns 1
and 2 on row 1, a gridwidth value being used to indicate
that it should straddle two columns – the gridx value in this
case indicates the first of the columns. (To straddle multiple
rows we would set the gridheight value.)
Autumn 2015
CE203 Part 5
50
The GridBagLayout Class 3
GridBagLayout g = new GridBagLayout();
c.setLayout(g);
GridBagConstraints gbc =
new GridBagConstraints();
JLabel l = new JLabel("Hello");
gbc.gridx = 0; gbc.gridy = 0;
g.setConstraints(l, gbc); c.add(l);
JButton b = new JButton("b1");
gbc.gridx = 1;
// gridy still 0
g.setConstraints(b, gbc); c.add(b);
JTextField t = new JTextField(20);
gbc.gridy = 1;
// gridx still 1
gbc.gridwidth = 2;
g.setConstraints(t, gbc); c.add(t);
Autumn 2015
CE203 Part 5
51
The GridBagLayout Class 4
The number of rows and columns in the grid is determined by
the grid positions of the items added – in the example,
assuming that no more items were added, there would be two
rows of three columns.
The width of each column and height of each row is by
default equal to the width or height of the largest component
in that column or row, but this can be changed by setting
values in the GridBagConstraints object.
For more details of the use of the box layout and grid bag
layout managers see (e.g.) Deitel and Deitel section 22.9.
Autumn 2015
CE203 Part 5
52
Checkboxes 1
A checkbox is a state button that has a boolean value, usually
denoting whether some feature is to be turned on or off.
Objects of type JCheckBox may be added to a frame, applet
or panel in the same way as any other component, but the
handler for the box is different. Instead of adding an action
listener to the box we need to add an item listener which
implements the class ItemListener by providing a void
method called itemStateChanged with an argument of type
ItemEvent. This method will be called whenever the user
clicks on the box to turn it on or off.
Autumn 2015
CE203 Part 5
53
Checkboxes 2
The program on the following slides shows how a checkbox
may be used to control whether a square displayed on a panel
is to be filled or drawn as an outline.
Note that the JCheckBox constructor takes a string argument,
the label for the box.
Within the event-handler we examine the event using the
getStateChange method, which will return either
ItemEvent.SELECTED or ItemEvent.DESELECTED.
Autumn 2015
CE203 Part 5
54
Checkboxes 3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CBoxExample extends JFrame
{ boolean filled = false;
public CBoxExample()
{ super("Check box example");
JCheckBox cb = new JCheckBox("fill");
cb.addItemListener(new CBHandler());
add(cb, BorderLayout.SOUTH);
Autumn 2015
CE203 Part 5
55
Checkboxes 4
// CBoxExample constructor continued
add(new JPanel()
{ public void paintComponent(Graphics g)
{ super.paintComponent(g);
if (filled)
g.fillRect(50,50,100,100);
else
g.drawRect(50,50,100,100);
},
BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
Autumn 2015
CE203 Part 5
56
Checkboxes 5
// class CBoxExample continued
public static void main(String args[])
{ JFrame myFrame = new CBoxExample();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
// handler is an inner class
class CBHandler implements ItemListener
{ public void itemStateChanged(ItemEvent e)
filled =
e.getStateChange()==ItemEvent.SELECTED;
repaint();
}
}
Autumn 2015
CE203 Part 5
57
Radio Buttons 1
Radio buttons are similar to checkboxes, but may be grouped
together and at any time exactly one button from a group will
be selected; clicking on another one will deselect the previous
selection.
Radio buttons are placed on a frame, applet or panel as
individual components; the grouping is performed as a
separate activity by adding the buttons to an object of type
ButtonGroup; this object is not a component so it is not
added to the container.
Autumn 2015
CE203 Part 5
58
Radio Buttons 2
Event handlers for JRadioButton objects should implement
ItemListener.
The example on the following slides shows the use of radio
buttons to control the colour of a square. Note the use of the
boolean argument in the calls to the JRadioButton
constructor to indicate which button is initially selected.
The ItemEvent class has a getSource method similar to that
of ActionEvent; this has been used in the button handler to
determine which radio button the user has selected.
Autumn 2015
CE203 Part 5
59
Radio Buttons 3
// usual imports needed
public class RadioExample extends JFrame
{ JRadioButton redB, blueB, greenB;
Color col = Color.red;
public RadioExample()
{ add(new JPanel()
{ public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.setColor(col);
g.fillRect(50,50,100,100);
}
},
BorderLayout.CENTER);
Autumn 2015
CE203 Part 5
60
Radio Buttons 4
// RadioExample constructor continued
redB = new JRadioButton("red", true);
blueB = new JRadioButton("blue" false);
greenB = new JRadioButton("green", false);
RBHandler rbh = new RBHandler();
redB.addItemListener(rbh);
blueB.addItemListener(rbh);
greenB.addItemListener(rbh);
ButtonGroup g = new ButtonGroup();
g.add(redB);
g.add(blueB);
g.add(greenB);
Autumn 2015
CE203 Part 5
61
Radio Buttons 5
// RadioExample constructor continued
JPanel p = new JPanel();
p.setLayout(new FlowLayout(10,0));
p.add(redB); p.add(blueB); p.add(greenB);
add(p, BorderLayout.SOUTH);
setSize(300, 300);
setVisible(true);
}
public static void main(String args[])
{ JFrame myFrame = new RadioExample();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
Autumn 2015
CE203 Part 5
62
Radio Buttons 6
// class RadioExample continued
// inner class
class RBHandler implements ItemListener
{ public void itemStateChanged(ItemEvent e)
{ if (e.getSource()==redB)
col = Color.red;
else if (e.getSource()==blueB)
col = Color.blue;
else col = Color.green;
repaint();
}
}
}
Autumn 2015
CE203 Part 5
63
Menus 1
In order to use menus in a frame or applet we need to add a
menu bar. This contains one or more menus, which are
made up of menu items (or submenus).
Hence three classes need to be used: JMenuBar, JMenu and
JMenuItem.
This example on the following slides generates a frame in
which the colour of a displayed square is selected from a
menu.
Autumn 2015
CE203 Part 5
64
Menus 2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuExample extends JFrame
{ Color col = Color.red;
public static void main(String args[])
{ JFrame myFrame = new MenuExample();
myFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
Autumn 2015
CE203 Part 5
65
Menus 3
// class MenuExample continued
public MenuExample()
{ JMenu menu = new JMenu("Colour");
// add some items to the menu
JMenuItem b = new JMenuItem("Blue");
b.addActionListener(
new ItemHandler(Color.blue));
menu.add(b);
JMenuItem r = new JMenuItem("Red");
r.addActionListener(
new ItemHandler(Color.red));
menu.add(r);
Autumn 2015
CE203 Part 5
66
Menus 4
// MenuExample constructor continued
// add the menu to a menu bar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(menu);
setSize(200, 200);
setVisible(true);
}
public void paint(Graphics g)
{ super.paint(g);
g.setColor(col);
g.fillRect(50,50,100,100);
}
Autumn 2015
CE203 Part 5
67
Menus 5
// class MenuExample continued
class ItemHandler implements ActionListener
{ private Color theCol;
public ItemHandler(Color colour)
{ theCol = colour;
}
public void actionPerformed(ActionEvent e)
{ col = theCol;
repaint();
}
}
}
Autumn 2015
CE203 Part 5
68
Menus 6
Note that the listeners for menu items should implement
ActionListener, not ItemListener.
The setJMenuBar method is used to associate a menu bar
with an applet or frame; the applet or frame can have only
one menu bar, but the bar may have many menus.
The add method is used to add items or submenus to a menu.
If we were writing a frame with a square and a circle we
might provide a menu for the circle and a menu for the
square, each having submenus to change the colour and size.
An outline of the necessary code is shown on the next slide.
(The addition of action listeners has been omitted.)
Autumn 2015
CE203 Part 5
69
Menus 7
JMenu sqMenu = new JMenu("Square");
JMenu sqSizeMenu = new JMenu("Size");
// need to add some items to sqSizeMenu
JMenu sqColMenu = new JMenu("Colour");
// need to add some items to sqColMenu
sqMenu.add(sqSizeMenu);
sqMenu.add(sqColMenu);
JMenu circMenu = new JMenu();
// need to add submenus and items
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(sqMenu); bar.add(circMenu);
Autumn 2015
CE203 Part 5
70
Menus 8
When using submenus we should provide action listeners
only for the menu items, and not for the submenus – the
display of submenus is performed automatically when their
names are selected from a menu.
A menu can also contain check boxes and radio buttons of
type JCheckBoxMenuItem or JRadioButtonMenuItem; as
with other menu items we may add action listeners to these
(not item listeners).
Pop-up menus (of type JPopUpMenu) associated with
individual components can be written – mouse listeners must
be added to the components to allow the pop-up menus to be
displayed.
Autumn 2015
CE203 Part 5
71
Tabbed Panes 1
A tabbed pane arranges components into layers with only one
layer being visible at a time, the user selecting which is to be
displayed by clicking on a tab at the top of the pane. A
tabbed pane may be added to a panel, frame or applet; it
should normally be the only component added to the
container so if we wish to use other components we should
create a new panel specifically for the tabbed pane.
The JTabbedPane class has a no-argument constructor;
components are added to the pane using the addTab method.
This takes four arguments: a label for the tab, an icon for the
tab (this may be null), the identity of the component and a
tool-tip string. An example is shown on the next slide
Autumn 2015
CE203 Part 5
72
Tabbed Panes 2
public class MyFrame extends JFrame
{ JTabbedPane tp = new JTabbedPane();
JTextArea t = new JTextArea(………);
JPanel p = new JPanel() { ……… };
public MyFrame()
{ add(tp, BorderLayout.CENTER);
tp.addTab("Text", null, t, "edit text");
tp.addTab("Pict", null, p, "view diagram");
setSize(250,200);
setVisible(true);
}
}
Autumn 2015
CE203 Part 5
73
Dialogue Boxes 1
A dialogue box is a window that is temporarily placed on the
screen to display a message for the user or allow him or her
to supply input.
To create and display a message dialogue box the static
method showMessageDialog from the JOptionPane class is
used:
JOptionPane.showMessageDialog(null,
"The answer is " + answer,
"ANSWER",
JOptionPane.INFORMATION_MESSAGE);
Autumn 2015
CE203 Part 5
74
Dialogue Boxes 2
The first argument of the showMessageDialog method, if
non-null, supplies information about the window over which
the dialogue box will appear; by default it appears at the
centre of the screen; in a frame we could use this to indicate
that it should appear in front of the frame. The second
argument is the message to be displayed, the third is a title for
the window and the fourth determines what icon will appear
alongside the message. Options are INFORMATION_MESSAGE,
ERROR_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE
and PLAIN_MESSAGE (which has no icon).
By default a message dialogue box has an “OK” button at the
bottom which is used to dismiss the window.
Autumn 2015
CE203 Part 5
75
Dialogue Boxes 3
The JOptionPane class also provides a method called
showInputDialog. This takes just one argument, a prompt
string and will create and display a dialogue box showing an
editable text field below the prompt. Two buttons will be
provided, labelled “OK” and “Cancel”. The method waits
until the user presses one of these buttons then returns a
string – if the “OK” button has been selected the returned
value will contain the text entered in the text field, otherwise
null will be returned.
String name = JOptionPane.showInputDialog(
"Please type your name");
Autumn 2015
CE203 Part 5
76
Dialogue Boxes 4
There are also methods called showOptionDialog which
allows the programmer to specify the text to be displayed on
a number of buttons and returns a value indicating which
option was selected and showConfirmDialog which can
supply “yes” and “no” buttons or “yes”, “no” and “cancel”
buttons.
If anything more complex is required the programmer must
create a new JOptionPane object, set its properties, and
create and show a JDialog object, using the option pane as
the argument to the JDialog constructor.
Autumn 2015
CE203 Part 5
77
Java Swing
• There are many more classes in the Java Swing API
• Try them out using the Java API
• The underlying principles are the same as discussed
throughout the lectures
Autumn 2015
CE203 Part 5
78
Java Security Manager 1
• Java has been designed to work in a distributed
environment
• Code can be downloaded dynamically from a variety of
(untrusted) sources
• Running such code alongside applications that contain
confidential information poses a potential risk.
• Security is a real concern and built into Java via access
control
Autumn 2015
CE203 Part 5
79
Java Security Manager 2
• All access control decisions are done by a security
manager: java.lang.SecurityManager.
• A security manager must be installed into the Java runtime
in order to activate the access control checks.
• Applets are run with a security manager by default.
• Applications have no security manager by default.
Autumn 2015
CE203 Part 5
80
Java Security Manager 3
Two options to use a security manager:
• Set one via the setSecurityManager method in the
java.lang.System class, i.e..:
System.setSecurityManager(new SecurityManager());
• Call Java using appropriate command line arguments, e.g.:
javac -Djava.security.manager ...
Autumn 2015
CE203 Part 5
81
Java Security Manager 4
• A small set of default permissions are granted to code by
class loaders. Administrators can then flexibly manage
additional code permissions via a security policy.
• This is done via the java.security.Policy class. There
is only one Policy object installed into the Java runtime at
any given time.
• Policy files tell the security manager what is allowed by
whom (and what is not).
• These policies are defined declaratively in text files
(usually created via a policy tool utility)
Autumn 2015
CE203 Part 5
82
Java Security Manager 5
Sample code snippet:
…
Permission perm = new
java.io.FilePermission("/tmp/abc", "read");
SecurityManager sm =
System.getSecurityManager();
if (sm != null) {
sm.checkPermission(perm);
}
...
Autumn 2015
CE203 Part 5
83
Java Security Manager 6
http://docs.oracle.com/javase/8/docs/technotes/guides/security/overview/jsoverview.html
Autumn 2015
CE203 Part 5
84
Java Security Manager (Example)
//file: EvilEmpire.java
import java.net.*;
public class EvilEmpire {
public static void main(String[] args) throws
Exception{
try {
Socket s = new Socket("www.essex.ac.uk", 80);
System.out.println("Connected!");
}
catch (SecurityException e) {
System.out.println("SecurityException:
could not connect.");
}
}
}
(see Niemeyer and Knudsen, 2005)
Autumn 2015
CE203 Part 5
85
Java Security Manager (Example) 2
Run this code without any parameters, e.g. (command line):
C:\> java EvilEmpire
… and with security manager (e.g. command line):
C:\> java –Djava.security.manager EvilEmpire
… and perhaps use your own policy file:
C:\> java –Djava.security.manager
-Djava.security.policy=EvilEmpire.policy
EvilEmpire
(you can of course also use IntelliJ to do this)
Autumn 2015
CE203 Part 5
86
Java Security Manager
• This has just been a quick overview.
• Check the Java homepage for more details, e.g.
http://docs.oracle.com/javase/8/docs/technotes/guides/
security/overview/jsoverview.html
Autumn 2015
CE203 Part 5
87
Download