Object Oriented Programming in Java Lecture 5 Swing - PowerPoint PPT Presentation


PPT – Object Oriented Programming in Java Lecture 5 Swing PowerPoint presentation | free to view - id: 296b97-ZjgwN


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Object Oriented Programming in Java Lecture 5 Swing


or using one of the following methods in BorderFactory: createTitledBorder, ... screen-based component classes, but may apply across a set of components ... – PowerPoint PPT presentation

Number of Views:680
Avg rating:3.0/5.0
Slides: 77
Provided by: SzymonGr2


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Object Oriented Programming in Java Lecture 5 Swing

Object Oriented Programming in Java Lecture
5 (Swing)
  • Szymon Grabowski sgrabow_at_kis.p.lodz.pl http//szgr
  • Thx to Wojciech Bieniecki for sharing stuff and
    friendly advices

Lódz, 2009
Changing the look and feel http//java.sun.com/d
Changing the look and feel, contd
Note the appearance of many look and feels
changes (rather slightly) from release to
release. Previous slide refers to Java 1.4 SDK.
Code example public static void main(String
args) try // Set cross-platform Java LF
(also called "Metal") UIManager.setLookAndFeel(
catch (UnsupportedLookAndFeelException
e) / handle exception / catch
(ClassNotFoundException e) / handle exception
/ catch (InstantiationException e) /
handle exception / catch (IllegalAccessExcep
tion e) / handle exception / new
SwingApplication() / Create and show the GUI.
/ ...
...you can also try UIManager.setLookAndFeel(UIMa
Changing the look and feel, contd
// Set Motif LF on any platform
UIManager.setLookAndFeel ("com.sun.java.swing.
...Or from cmd-line java -Dswing.defaultlafcom.
sun.java.swing.plaf. motif.MotifLookAndFeel
MyApp java -Dswing.defaultlafcom.sun.java.swing.
plaf.windows. WindowsLookAndFeel MyApp
JLabel http//www.apl.jhu.edu/hall/java/Swing-T
Basically works like AWTs Label, but...
  • JLabel compared to Label
  • can display images, usually by supplying an
  • either to the constructor or via a call to
  • can place borders around the labels
  • can use HTML text.

Jlabel, excerpt from the prev. example

Using HTML on a label.
JButton http//www.apl.jhu.edu/hall/java/Swing-
  • JButton compared to AWTs Button
  • ability to associate images (incl. animated
    GIFs) with buttons,
  • can easily set keyboard mnemonics via
    setMnemonic. Use ALT-char activates the button,
  • can also change the alignment of the text or
    icon in the button (setHorizontalAlignment and
    setVerticalAlignment only valid if button is
    larger than preferred size), and change where the
    text is relative to the icon (setHorizontalTextPos
    ition, setVerticalTextPosition),
  • can use HTML text.

JButton, example http//www.apl.jhu.edu/hall/ja
More on images at JButtons http//www.apl.jhu.ed
  • JButton actually allows seven associated images
  • the main image,
  • the image to use when the button is pressed
  • the image to use when the mouse is over it
    (setRolloverIcon, but you need to call
    setRolloverEnabled(true) first),
  • the image to use when the button is selected and
    enabled (setSelectedIcon),
  • the image to use when the button is disabled
  • the image to use when it is selected but
    disabled (setDisabledSelectedIcon),
  • the image to use when the mouse is over it while
    it is selected (setRolloverSelectedIcon).

// ad 2 ImageIcon cup new ImageIcon("cup.gif")
JButton b new JButton("Java",
cup) b.setPressedIcon(new ImageIcon("hotcup.jpg")
JPanel http//www.apl.jhu.edu/hall/java/Swing-T
  • JPanel compared to AWTs Panel
  • JPanel also acts as a replacement for Canvas
    (there is no JCanvas),
  • can assign borders,
  • syntactic sugar lets you supply the
    LayoutManager to the constructor in addition to
    specifying it later via setLayout as with Panel.

JPanel, contd
You can assign borders to JPanels. Swing offers
seven basic border types titled, etched,
beveled (regular plus a softer version), line,
matte, compound, and empty.
Setting a border via the setBorder
method. Creating a border calling a constructor
directly, or using one of the following methods
in BorderFactory createTitledBorder,
createEtchedBorder, createBevelBorder,
createRaisedBevelBorder, createLoweredBevelBorder,
createLineBorder, createMatteBorder,
createCompoundBorder, and createEmptyBorder.
E.g., BevelBorder and SoftBevelBorder can be
applied to buttons to give them a 3D look.
Can I have my own border to JPanel?
Sure. Just override the method public void
paintComponent(Graphics g) in JPanel.
Border examples
Border examples, contd
JPanel example
JPanel example, contd
JWindow a container that can be displayed
anywhere on the users desktop. It does not
have the title bar or window-management, in
comparison to a JFrame. Mostly superseded by
JDialog. But its useful for a splash screen.
JWindow, example, contd
JCheckBox, JRadioButton http//www.apl.jhu.edu/
hall/java/Swing-Tutorial/ http//www.beginner-java
Events can be monitored via ActionListener or
ItemListener. JCheckBox If ActionListener use
isSelected to distinguish the state. If
ItemListener the ItemEvent itself has this
information call getStateChange and compare the
result to ItemEvent.SELECTED or
ItemEvent.DESELECTED. JRadioButton Similarly
handled however, only the radio button that is
clicked will get an ActionEvent, while both the
one clicked and the one that becomes deselected
as a result get an ItemEvent.
JCheckBox, JRadioButton, contd
Call setContentAreaFilled(false), unless you are
sure that the background color of the JCheckBox /
JRadioButton matches the background color of the
Container. Again for both JCheckBox,
JRadioButton you can supply an icon to replace
the normal square with a check in it (via
setIcon), but if you do, be sure to also supply
an icon to be displayed when the checkbox is
selected (setSelectedIcon).
JCheckBox example (1 / 2) http//www.beginner-ja
JCheckBox example (2 / 2) http//www.beginner-ja
Simple dialog boxes http//home.cogeco.ca/ve3ll
Dialogs are popup windows that include short
messages or information screens, confirmation
boxes, and input prompts for string
information. In Swing class JOptionPane. Each
method has a first parameter that points to a
parent (ie. window that it appears in) or null
(the current window).
Basic event listeners http//home.cogeco.ca/ve3
More advanced event listeners (1/3)
If several e.g. buttons require handling the same
event (but in a different way, of course), then
youll have many ifs (or switch
instruction) which is not very readable. If
(source pressme1) ... else if (source
pressme2) ... // yuck!
There are better ways. One possibility is using
inner classes, i.e. nested classes. They allow
routines to be written separately for each
specific object. Details see the source at the
next slide...
More advanced event listeners (2/3)
More advanced event listeners (3/3)
Of course, we can also use an anonymous class.
Checking an event source
All event classes inherit the method Object
getSource() from EventObject, which returns the
reference to the event source.
Example. We have a text editor and want to add
a button bdel to delete the whole text.
class Test extends JFrame implements
ActionListener JButton bdel . . . . . .
. Test() . . . . . . . bdel new
JButton("Delete") bdel.addActionListener(this)
Checking an event source, contd
public void actionPerformed(ActionEvent aev)
if (aev.getSource() bdel) // has bdel
been clicked? editor.setText("")
// delete text return // or
smth else has been done... String key
aev.getActionCommand() editor.append(key)

Event class hierarchy
  • java.util.EventObject
  • java.awt.AWTEvent
  • ActionEvent
  • AdjustmentEvent
  • ItemEvent
  • TextEvent
  • ComponentEvent
  • ContainerEvent
  • FocusEvent
  • InputEvent
  • KeyEvent
  • MouseEvent
  • WindowEvent

ComponentEvent descendant classes handle
low-level (physical) events (e.g. a mouse click
or resizing a component). They identify the
source via getComponent() method (from the
ComponentEvent class).
Semantic events
Four classes, which are NOT ComponentEvent
descendants, deal with semantic events. Semantic
events are defined at a higher-level to
encapsulate the semantics of a user interface
components model. The semantic event classes
defined by the AWT are as follows
java.awt.event.ActionEvent (do a command)
java.awt.event.AdjustmentEvent (value was
adjusted) java.awt.event.ItemEvent (item
state has changed) java.awt.event.TextEvent
(the value of the text object changed)
Semantic events, contd http//java.sun.com/j2se
Semantic events are not tied to specific
screen-based component classes, but may apply
across a set of components which implement a
similar semantic model. For example, a Button
object will fire an action event when it is
pressed, a List object will fire an action
event when an item is double-clicked, a MenuItem
will fire an action event when it was selected
from a menu, and a non-visual Timer object might
fire an action when its timer goes off (the
latter is a hypothetical case).
Semantic events, contd http//java.sun.com/docs
Whenever possible, you should listen for semantic
events rather than low-level events. That way,
you can make your code as robust and portable as
possible. For example, listening for action
events on buttons, rather than mouse events,
means that the button will react appropriately
when the user tries to activate the button using
a keyboard alternative or a look-and-feel-specifi
c gesture.
Some typical component events and listeners
Listening to a text field
TEXT_VALUE_CHANGED event occurs when the text in
a text component changes. Possible use validate
a field (for being an integer, for example).
public class Events05 extends JFrame String
tback "" Events05() JTextField tf
new JTextField(40) tf.addTextListener(new
TextListener() public void
textValueChanged(TextEvent e)
TextComponent t (TextComponent) e.getSource()
try Integer.parseInt(t.getText())
catch(NumberFormatException exc)
t.setText(tback) tback
t.getText() ) . . . . . . . .
. . . . . .
Listening to a focus change (1/2)
class TextPanel extends JPanel implements
FocusListener TextField tf final Color
NOTFOCUS Color.gray, FOCUS Color.blue,
ERROR Color.red Color currColor FOCUS
TextPanel(int cols) setLayout(new
setBackground(NOTFOCUS) tf new
JTextField(cols) tf.addFocusListener(this)
add(tf) // public Insets
getInsets() return new Insets(5,5,5,5)
Listening to a focus change (2/2)
public void focusGained(FocusEvent e)
setBackground(currColor) public void
focusLost(FocusEvent e) String s
tf.getText() if (!s.equals("")) // empty
field skip try int i
Integer.parseInt(s) catch(NumberFormatExc
eption exc) getToolkit().beep()
currColor ERROR tf.requestFocus() //
!! return // end of catch block
currColor FOCUS setBackground(NOTFOCUS)
// end of class
Keyboard handling
KeyEvent keypress, key release, typing a char.
This event can ask for a key code (getKeyCode()).
Result int.
Virtual codes for special keys (F1..F12, Enter
etc.) static int constants, e.g.
KeyEvent.VK_ENTER, KeyEvent.VK_F1.
Another possibility we can ask for a natural
code name, using the static method String
getKeyText(int) (from KeyEvent class) It
returns a string, e.g. Enter or Home.
Arrow keys handling (1/2)
class TextPanel extends JPanel implements
FocusListener TextField tf TextPanel
prev, next // TextPanels make up a list
// each has a ref to the next and the prev
item on the list public static void
main(String args) JFrame f new
JFrame ("Text Field") f.setLayout(new
GridLayout(0,1)) TextPanel tp new
TextPanel3 for( int i 0 i lt 3 i)
tpi new TextPanel(40, (i gt 0?
tpi-1null)) f.add(tpi)
f.pack() f.show()
Arrow keys handling (2/2)
TextPanel(int cols, TextPanel p) // 2nd arg
ref to the prev TextPanel setLayout(new
setBackground(NOTFOCUS) tf new
TextField(cols) tf.addFocusListener(this)
prev p if (prev ! null) prev.next
this // bidirectional list here we fill up
the field next tf.addKeyListener( new
KeyAdapter() // anonymous class
public void keyReleased(KeyEvent e)
int key e.getKeyCode() String ktxt
KeyEvent.getKeyText(key) if
(ktxt.equals("Enter") ktxt.equals("Down"))
// next on the list if (next !
null) next.tf.requestFocus()
else if (ktxt.equals("Up")
prev ! null) prev.tf.requestFocu
s() // previous // end of keyReleased
) add(tf) // end of TextPanel
Tool tips (hints) http//www.exampledepot.com/eg
JButton b1 new JButton("play", new
ImageIcon("icon.gif")) ........... b1.setToolTipT
ext("C'mon, click me!") // the tip's northwest
corner // appears at the same x-coordinate as the
cursor // and 20 pixels lower than the
y-coordinate of the cursor
To change the tip location override
getToolTipLocation() method of the component. //
example below no JButton's descendant class
created... JButton button new
JButton("MyButton") public Point
getToolTipLocation(MouseEvent event) return
new Point(0, 0)
Tool tips (hints), contd http//www.exampledepo
// Set the location of the tool tip such that
its N-W corner // coincides with the bottom
center of the button ...getToolTipLocation(MouseE
vent event) return new Point(getWidth()/2,
getHeight()) ... // Use the default tool tip
location ...getToolTipLocation(MouseEvent event)
return null ...
Tip display startup delay (default
750ms) ToolTipManager.sharedInstance().setInitialD
elay(0) // immediately! Get current startup
delay int initialDelay ToolTipManager.sharedInst
Other tricks with tool tips http//www.examplede

Making a tool tip visible for a specified
interval of time (default 4s) ToolTipManager.shar
edInstance().setDismissDelay(x) // x time in
ms // x could be Integer.MAX_VALUE, i.e.
practically forever Get current delay int
dismissDelay ToolTipManager.sharedInstance().get
Multiple lines in a tip? Use html. Need an image
in a tip? Use html. component.setToolTipText("
lthtmlgt""This is a cool""ltbrgt" "tool
tip""lt/htmlgt") component.setToolTipText("lthtm
lgtltcentergt""This is another""ltbrgt""tool
tip""lt/centergtlt/htmlgt") String imageName
"fileimage.jpg" component.setToolTipText("lthtml
gtHere is an image ltimg src"imageName"gtlt/htmlgt
JSlider http//www.apl.jhu.edu/hall/java/Swing-
  • JSlider vs. AWTs ScrollBar
  • tick marks and labels,
  • borders,
  • sliders that go from high to low instead of low
    to high (setInverted(true)),
  • ability to determine that you are in the middle
    of a drag (when getValueIsAdjusting() returns
    true) so that you can postpone action until the
    drag finishes.

JSlider, a little bit of useful code (1/2)

JSlider, a little bit of useful code (2/2)

Zero-argument constructor creates a horizontal
slider with a range from 0 to 100 and an initial
value of 50.
JList Core Java 2, Vol. II, chapter 6
JList wordList new JList(new String
"stupid", "foolish", "silly", "soft-minded",
"dumb" )
List boxes do not scroll automatically. To make a
list box scroll, you must insert it into a
scroll pane JScrollPane scrollPane new
JScrollPane(wordList) You then add the scroll
pane, not the list, into the surrounding panel.
By default, JList displays 8 items you can
change it wordList.setVisibleRowCount(4) //
display 4 items
  • Three layout orientations
  • JList.VERTICAL (the default),
  • JList.VERTICAL_WRAP start new columns if there
    are more items than the visible row count,
  • JList.HORIZONTAL_WRAP start new columns if
    there are more items than the visible row count,
    but fill them horizontally.

JList, contd Core Java 2, Vol. II, chapter 6
Word order on the JList quick, brown, hungry,
wild, silent, huge…
JList, contd Core Java 2, Vol. II, chapter 6
Multiple item selection on a JList by
default. (Use CTRL when clicking on each item
hold down Shift when choosing an interval.) Can
restrict selection with setSelectionMode method.
E_SELECTION) // select one item at a time
wordList.setSelectionMode( ListSelectionModel.
or one range of items
List selection events Core Java 2, Vol. II,
chapter 6
Rather than listening to action events, you need
to listen to list selection events. Add a list
selection listener to the list component, and
implement the method public void
valueChanged(ListSelectionEvent evt) in the
JList.getSelectedValues() Core Java 2, Vol.
II, chapter 6
The getSelectedValues method returns an array of
objects containing all selected items. Cast each
array element to a String. Object values
list.getSelectedValues() for (Object value
values) do_something_with_(String)_value
You cannot simply cast the return value of
getSelectedValues from an Object array to a
String array. The return value was not created
as an array of strings, but as an array of
objects, each of which happens to be a string.
To process the return value as an array of
strings, use the following code String words
new Stringvalues.length System.arrayCopy(val
ues, 0, words, 0, values.length)
JList.getSelectedValue() (note the difference!)
Core Java 2, Vol. II, chapter 6
Object getSelectedValue() returns the first
selected value or null if the selection is
empty. Useful if multiple selection
disallowed. But again, cast Object to String when
you want to do smth with the returned value.
List selection events, contd
A single click generates three events one for
the deselection of the originally selected entry,
one indicating the selection is moving, and one
for the selection of the new entry. In the
first two cases, the ListEvents
getValueIsAdjusting method returns true, so you
typically check if it is false if you only care
about the selection. Of course, you can also
totally ignore events and later look up which
item (getSelectedValue) or index
(getSelectedIndex) is currently selected.
JList example (1 / 2) http//www.apl.jhu.edu/h

JList example (2 / 2)
Running the example (row-wise)
Modifying JList
It is very easy (see prev. slides) to initialize
a JList with an array of objects (e.g.
strings). But the list is immutable. Its more
cumbersome to make a mutable list, ie. enable to
add/delete individual items later. JList
employs the Model-View-Controller approach.
First create a list model, recommended
(easiest) DefaultListModel. Then use addElement
method. Then set the list model.
Modifying JList, contd http//java.sun.com/docs
Removing an item from JList http//java.sun.com/
actionPerformed() for the action listener
registered on the Fire button. The bold line
removes the selected item in the list. The
remaining lines in the method disable the fire
button if the list is now empty, and make
another selection if it is not.
Rendering JList elements http//www.java2s.com/T
utorial/Java/0240__Swing/ RenderingJListElements.h
Every JList has an installed cell renderer that
draws every cell. If you want to draw cells in
your own way, add a class that implements
ListCellRenderer interface. Drawing a cell the
interfaces only method, getListCellRendererCompo
nent, is called.
Rendering JList elements, example
Rendering JList elements, example, contd
Another example Sundays in red, cellHasFocus
JFormattedTextField (since Java 1.4)
http//www.tutorialized.com/tutorial/ Swing-s-new-
A component to prompt for numbers, dates etc.
formatted input. Acceptable input is either
explicitly specified by the mask or specified by
a value for the component.
Masked input is typically configured by using an
instance of the MaskFormatter class (from
javax.swing.text). It uses a series of
characters to designate acceptable input.
A digit ? A letter A A letter or
digit Anything U A letter, with a..z
mapped to their uppercase equiv. L A letter,
with A..Z mapped to their lowercase equiv. H A
hex. digit (A-F, a-f, 0-9) Used to escape
another mask char
JFormattedTextField, contd
DateFormat format new SimpleDateFormat("yyyy--MM
MM--dd") DateFormatter df new
DateFormatter(format) MaskFormatter mf new
MaskFormatter("() --") // tel format
// lets now make use of the mask JFormattedTextF
ield ftf1 new JFormattedTextField(df)
A useful method of MaskFormatter
JFormattedTextField, source code example (1/2)
http//www.tutorialized.com/tutorial/ Swing-s-new-
JFormattedTextField, source code example (2/2)
http//www.tutorialized.com/tutorial/ Swing-s-new-
JToolBar http//www.apl.jhu.edu/hall/java/Swing
Basic use a container for small
buttons. Compared to JPanel, however, JToolBar is
floatable (you can drag it out of the original
Using custom cursors http//www.tutorialized.com
/tutorial/ Using-Custom-Cursors-in-Java/14024
Goal create your own cursors (GIF or PNG) that
are displayed when the user moves the mouse over
a Java component (AWT or Swing).
A transparent GIF or PNG will be needed.
Loading the Cursor Image //Get the default
toolkit Toolkit toolkit Toolkit.getDefaultToolk
it() //Load an image for the cursor Image
cursorImage toolkit.getImage("pencil.gif")
Defining the Cursor Hot Spot The hot spot is
used for the point location in mouse events.
//Create the hotspot for the cursor Point
cursorHotSpot new Point(0,0)
Using custom cursors, contd http//www.tutorial
ized.com/tutorial/ Using-Custom-Cursors-in-Java/14
Creating the Custom Cursor Put together the
cursor image and the hot spot //Create the
custom cursor Cursor customCursor
cursorHotSpot, "Pencil") Displaying the Custom
Cursor The final step is to notify the component
to display the cursor. //Use the custom cursor
Tabbed panes http//home.cogeco.ca/ve3ll/jatuto
Tabbed panes allow a multilayered pane with tabs
for the user to access the layer he wants. Each
tab contains a single component.
Layered panes, intro http//java.sun.com/j2se/1
javax.swing.JLayeredPane adds depth (Z-order) to
a JFC/Swing container, allowing components to
overlap each other when needed. An Integer
object specifies each component's depth in the
container, where higher-numbered components sit
on top of other components. Example
layeredPane.add(child, new Integer(10)) You
can change the Z-order of the layers after you
have created them.
Layered panes, simple example
Grabbing screenshots http//www.tutorialized.com
Well make use of the Robot class to capture the
screen image and the ImageIO API to save it as a
// Get the screen size Toolkit toolkit
Toolkit.getDefaultToolkit() Dimension screenSize
toolkit.getScreenSize() Rectangle rectangle
new Rectangle(0, 0, screenSize.width,
Robot robot new Robot() BufferedImage image
// Save the screenshot as a png file, then as a
jpeg file file new File("screen.png")
ImageIO.write(image, "png", file) file new
File("screen.jpg") ImageIO.write(image, "jpg",
A few words on the Robot class
Robot object makes it possible for your program
to temporarily take over control of the mouse
and the keyboard.   Sun says Note that some
platforms require special privileges or
extensions to access low-level input control. If
the current platform configuration does not allow
input control, an AWTException will be thrown
when trying to construct Robot objects.
Another warning from Sun If you accidentally
allow your program to take control of the mouse
and the keyboard in an infinite loop, the
program can be very difficult to terminate.
A few words on the Robot class, contd
  • Robot provides several instance methods by which
    a program can produce mouse and keyboard input,
    just as though that input were being provided by
    a human user.
  • mouseMove - Moves the mouse pointer to a set of
    specified absolute screen coordinates given in
  • mousePress - Presses one of the buttons on the
  • mouseRelease - Releases one of the buttons on
    the mouse.
  • keyPress - Presses a specified key on the
  • keyRelease - Releases specified key on the

robot.mouseMove(1005,10) // pointer may
disappear! robot.delay(1000) // 1 sec
delay robot.mousePress(InputEvent.BUTTON2_MASK)
// middle mouse button
About PowerShow.com