Title: Object Oriented Programming in Java Lecture 5 Swing
1Object Oriented Programmingin Java Lecture
5(Swing)
- Szymon Grabowskisgrabow_at_kis.p.lodz.plhttp//szgr
abowski.kis.p.lodz.pl/Java09/ - Thx to Wojciech Bieniecki for sharing stuff and
friendly advices
Lódz, 2009
2Changing the look and feel http//java.sun.com/d
ocs/books/tutorial/uiswing/lookandfeel/plaf.html
3Changing the look and feel, contd
http//java.sun.com/docs/books/tutorial/uiswing/lo
okandfeel/plaf.html
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(
UIManager.getCrossPlatformLookAndFeelClassName())
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 tryUIManager.setLookAndFeel(UIMa
nager.getSystemLookAndFeelClassName())
4Changing the look and feel, contd
http//java.sun.com/docs/books/tutorial/uiswing/lo
okandfeel/plaf.html
// Set Motif LF on any platform
UIManager.setLookAndFeel ("com.sun.java.swing.
plaf.motif.MotifLookAndFeel")
...Or from cmd-line java -Dswing.defaultlafcom.
sun.java.swing.plaf. motif.MotifLookAndFeel
MyApp java -Dswing.defaultlafcom.sun.java.swing.
plaf.windows. WindowsLookAndFeel MyApp
5JLabel http//www.apl.jhu.edu/hall/java/Swing-T
utorial/
Basically works like AWTs Label, but...
- JLabel compared to Label
- can display images, usually by supplying an
ImageIcon - either to the constructor or via a call to
setIcon - can place borders around the labels
- can use HTML text.
6Jlabel, excerpt from the prev. example
http//www.apl.jhu.edu/hall/java/Swing-Tutorial/
Using HTML on a label.
7JButton http//www.apl.jhu.edu/hall/java/Swing-
Tutorial/
- 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.
8JButton, example http//www.apl.jhu.edu/hall/ja
va/Swing-Tutorial/
9More on images at JButtons http//www.apl.jhu.ed
u/hall/java/Swing-Tutorial/
- JButton actually allows seven associated images
- the main image,
- the image to use when the button is pressed
(setPressedIcon), - 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
(setDisabledIcon), - 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 2ImageIcon cup new ImageIcon("cup.gif")
JButton b new JButton("Java",
cup)b.setPressedIcon(new ImageIcon("hotcup.jpg")
)
10JPanel http//www.apl.jhu.edu/hall/java/Swing-T
utorial/
- 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.
11JPanel, 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.
12Can I have my own border to JPanel?
Sure. Just override the method public void
paintComponent(Graphics g) in JPanel.
13Border examples
14Border examples, contd
15JPanel example
16JPanel example, contd
17JWindow
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.
18JWindow, example, contd
19JCheckBox, JRadioButton http//www.apl.jhu.edu/
hall/java/Swing-Tutorial/http//www.beginner-java
-tutorial.com/
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.
20JCheckBox, 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).
21JCheckBox example (1 / 2) http//www.beginner-ja
va-tutorial.com/jcheckbox.html
22JCheckBox example (2 / 2) http//www.beginner-ja
va-tutorial.com/jcheckbox.html
23Simple dialog boxes http//home.cogeco.ca/ve3ll
/jatutora.htmdb
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).
24Basic event listeners http//home.cogeco.ca/ve3
ll/jatutora.htmdb
25More 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...
26More advanced event listeners (2/3)
http//home.cogeco.ca/ve3ll/jatutora.htm
27More advanced event listeners (3/3)
http//home.cogeco.ca/ve3ll/jatutora.htm
Of course, we can also use an anonymous class.
28Checking 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)
getContentPane().add(bdel)
29Checking 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)
30Event 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).
31Semantic 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)
32Semantic events, contd http//java.sun.com/j2se
/1.3/docs/guide/awt/designspec/events.html
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).
33Semantic events, contd http//java.sun.com/docs
/books/tutorial/uiswing/events/generalrules.html
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.
34Some typical component events and listeners
http//www.crg.cs.nott.ac.uk/sdb/uid/Java20Lectu
re4.pdf
35Listening 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)
getToolkit().beep()
t.setText(tback) tback
t.getText() ) . . . . . . . .
. . . . . .
36Listening 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
FlowLayout(FlowLayout.LEFT))
setBackground(NOTFOCUS) tf new
JTextField(cols) tf.addFocusListener(this)
add(tf) // public Insets
getInsets() return new Insets(5,5,5,5)
37Listening 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
38Keyboard 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.
39Arrow 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()
40Arrow keys handling (2/2)
TextPanel(int cols, TextPanel p) // 2nd arg
ref to the prev TextPanel setLayout(new
FlowLayout(FlowLayout.LEFT))
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
41Tool tips (hints) http//www.exampledepot.com/eg
s/javax.swing/tooltip_ToolTipLoc.html?lrel
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() methodof 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)
42Tool tips (hints), contd http//www.exampledepo
t.com/egs/javax.swing/tooltip_DisplayTtNow.html?l
rel
// 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
ance().getInitialDelay()
43Other tricks with tool tips http//www.examplede
pot.com/egs/javax.swing/tooltip_ImageTt.html?lrel
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
DismissDelay()
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
")
44JSlider http//www.apl.jhu.edu/hall/java/Swing-
Tutorial/
- 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.
45JSlider, a little bit of useful code (1/2)
http//www.apl.jhu.edu/hall/java/Swing-Tutorial/
46JSlider, a little bit of useful code (2/2)
http//www.apl.jhu.edu/hall/java/Swing-Tutorial/
Zero-argument constructor creates a horizontal
slider with a range from 0 to 100 and an initial
value of 50.
47JList 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.
48JList, contd Core Java 2, Vol. II, chapter 6
Word order on the JList quick, brown, hungry,
wild, silent, huge
49JList, 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.
wordList.setSelectionMode(ListSelectionModel.SINGL
E_SELECTION) // select one item at a time
wordList.setSelectionMode( ListSelectionModel.
SINGLE_INTERVAL_SELECTION) // select one item
or one range of items
50List 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
listener.
51JList.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)
52JList.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.
53List selection events, contd
http//www.apl.jhu.edu/hall/java/Swing-Tutorial/
Swing-Tutorial-JList.html
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.
54JList example (1 / 2) http//www.apl.jhu.edu/h
all/java/Swing-Tutorial/Swing-Tutorial-JList.html
55JList example (2 / 2)
56Running the example (row-wise)
57Modifying JList
It is very easy (see prev. slides) to initialize
a JListwith 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.
58Modifying JList, contd http//java.sun.com/docs
/books/tutorial/uiswing/components/list.html
59Removing an item from JList http//java.sun.com/
docs/books/tutorial/uiswing/components/list.html
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.
60Rendering JList elements http//www.java2s.com/T
utorial/Java/0240__Swing/RenderingJListElements.h
tm
Every JList has an installed cell renderer that
draws every cell. If you want to draw cells in
your own way, add a classthat implements
ListCellRenderer interface. Drawing a cell the
interfaces only method, getListCellRendererCompo
nent, is called.
61Rendering JList elements, example
62Rendering JList elements, example, contd
63Another example Sundays in red, cellHasFocus
used
64JFormattedTextField (since Java 1.4)
http//www.tutorialized.com/tutorial/Swing-s-new-
JFormattedTextField-component/351
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 letterA A letter or
digit AnythingU 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
65JFormattedTextField, 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
setPlaceholderCharacter(char)
66JFormattedTextField, source code example (1/2)
http//www.tutorialized.com/tutorial/Swing-s-new-
JFormattedTextField-component/351
67JFormattedTextField, source code example (2/2)
http//www.tutorialized.com/tutorial/Swing-s-new-
JFormattedTextField-component/351
68JToolBar http//www.apl.jhu.edu/hall/java/Swing
-Tutorial/
Basic use a container for small
buttons. Compared to JPanel, however, JToolBar is
floatable(you can drag it out of the original
window).
69Using 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 SpotThe hot spot is
used for the point location in mouse events.
//Create the hotspot for the cursor Point
cursorHotSpot new Point(0,0)
70Using custom cursors, contd http//www.tutorial
ized.com/tutorial/Using-Custom-Cursors-in-Java/14
024
Creating the Custom CursorPut together the
cursor image and the hot spot //Create the
custom cursor Cursor customCursor
toolkit.createCustomCursor(cursorImage,
cursorHotSpot, "Pencil") Displaying the Custom
CursorThe final step is to notify the component
to display the cursor. //Use the custom cursor
setCursor(customCursor)
71Tabbed panes http//home.cogeco.ca/ve3ll/jatuto
rc.htmsp
Tabbed panes allow a multilayered pane with tabs
for the user to access the layer he wants. Each
tab contains a single component.
72Layered panes, intro http//java.sun.com/j2se/1
.4.2/docs/api/javax/swing/JLayeredPane.html
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.
73Layered panes, simple example
http//home.cogeco.ca/ve3ll/jatutorb.htm
74Grabbing screenshots http//www.tutorialized.com
/tutorial/Taking-Screenshots-in-Java/8777
Well make use of the Robot class to capture the
screen image and the ImageIO API to save it as a
JPEG or PNG.
// Get the screen size Toolkit toolkit
Toolkit.getDefaultToolkit()Dimension screenSize
toolkit.getScreenSize() Rectangle rectangle
new Rectangle(0, 0, screenSize.width,
screenSize.height)
Robot robot new Robot() BufferedImage image
robot.createScreenCapture(rectangle)
// 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",
file)
75A few words on the Robot class
http//www.developer.com/java/other/article.php/22
12401
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.
76A few words on the Robot class, contd
http//www.developer.com/java/other/article.php/22
12401
- 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
pixels. - mousePress - Presses one of the buttons on the
mouse. - mouseRelease - Releases one of the buttons on
the mouse. - keyPress - Presses a specified key on the
keyboard. - keyRelease - Releases specified key on the
keyboard.
robot.mouseMove(1005,10) // pointer may
disappear! robot.delay(1000) // 1 sec
delay robot.mousePress(InputEvent.BUTTON2_MASK)
// middle mouse button