Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Zaawansowane programowanie obiektowe Lecture 5 (Swing) Szymon Grabowski [email protected] http://szgrabowski.kis.p.lodz.pl/zpo/ Thx to Wojciech Bieniecki for sharing stuff and friendly advices Łódź, 2009 1 Changing the look and feel [ http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html ] 2 Changing the look and feel, cont’d [ http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/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 L&F (also called "Metal") UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { /* handle exception */ } catch (ClassNotFoundException e) { /* handle exception */ } catch (InstantiationException e) { /* handle exception */ } catch (IllegalAccessException e) { /* handle exception */ } new SwingApplication(); { /* Create and show the GUI. */ } ... ...you can also try: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 3 Changing the look and feel, cont’d [ http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html ] // Set Motif L&F on any platform UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); ...Or from cmd-line: java -Dswing.defaultlaf=com.sun.java.swing.plaf. motif.MotifLookAndFeel MyApp java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows. WindowsLookAndFeel MyApp 4 JLabel [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] Basically works like AWT’s 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. 5 Jlabel, excerpt from the prev. example [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] Using HTML on a label. 6 JButton [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] JButton compared to AWT’s 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 (setHorizontalTextPosition, setVerticalTextPosition), • can use HTML text. 7 JButton, example [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] WindowsUtilities and ExitListener classes – non-standard (the latter shown below) ImageIcon passed to the constructor: either as addition to the text, or instead of it. 8 More on images at JButtons [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] JButton actually allows seven associated images: 1. the main image, 2. the image to use when the button is pressed (setPressedIcon), 3. the image to use when the mouse is over it (setRolloverIcon, but you need to call setRolloverEnabled(true) first), 4. the image to use when the button is selected and enabled (setSelectedIcon), 5. the image to use when the button is disabled (setDisabledIcon), 6. the image to use when it is selected but disabled (setDisabledSelectedIcon), 7. 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")); 9 JPanel [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] JPanel compared to AWT’s 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. 10 JPanel, cont’d 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 11 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. 12 Border examples 13 Border examples, cont’d 14 JPanel example Watch out! 15 JPanel example, cont’d A JPanel object 16 JWindow JWindow – a container that can be displayed anywhere on the user’s desktop. It does not have the title bar or window-management, in comparison to a JFrame. Mostly superseded by JDialog. But it’s useful for a splash screen. 17 JWindow, example, cont’d 18 JCheckBox, 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 19 ItemEvent. JCheckBox, JRadioButton, cont’d 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). 20 JCheckBox example (1 / 2) [ http://www.beginner-java-tutorial.com/jcheckbox.html ] 21 JCheckBox example (2 / 2) [ http://www.beginner-java-tutorial.com/jcheckbox.html ] 22 Simple dialog boxes [ http://home.cogeco.ca/~ve3ll/jatutora.htm#db ] 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). 23 Basic event listeners [ http://home.cogeco.ca/~ve3ll/jatutora.htm#db ] Initialize the focus to answer 24 More advanced event listeners (1/3) If several e.g. buttons require handling the same event (but in a different way, of course), then you’ll have many if’s (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... 25 More advanced event listeners (2/3) [ http://home.cogeco.ca/~ve3ll/jatutora.htm ] implements is now placed in each inner class header, NOT in the main class header! 26 More advanced event listeners (3/3) [ http://home.cogeco.ca/~ve3ll/jatutora.htm ] Of course, we can also use an anonymous class. 27 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); getContentPane().add(bdel); } 28 Checking an event source, cont’d 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); } 29 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). 30 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 component’s 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”) 31 Semantic events, cont’d [ 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). 32 Semantic events, cont’d [ 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-specific gesture. 33 Some typical component events and listeners [ http://www.crg.cs.nott.ac.uk/~sdb/uid/Java%20Lecture4.pdf ] 34 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) { getToolkit().beep(); t.setText(tback); } tback = t.getText(); } }); .............. 35 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 FlowLayout(FlowLayout.LEFT)); setBackground(NOTFOCUS); tf = new JTextField(cols); tf.addFocusListener(this); add(tf); } // public Insets getInsets() { return new Insets(5,5,5,5); } 36 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(NumberFormatException exc) { getToolkit().beep(); currColor = ERROR; tf.requestFocus(); // !! return; } // end of catch block } currColor = FOCUS; setBackground(NOTFOCUS); } } // end of class 37 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”. 38 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 TextPanel[3]; for( int i = 0; i < 3; i++) { tp[i] = new TextPanel(40, (i > 0? tp[i-1]:null)); f.add(tp[i]); } f.pack(); f.show(); } 39 Arrow 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.requestFocus(); // previous } // end of keyReleased } ); add(tf); } // end of TextPanel 40 Tool tips (hints) [ http://www.exampledepot.com/egs/javax.swing/tooltip_ToolTipLoc.html?l=rel ] JButton b1 = new JButton("play", new ImageIcon("icon.gif")); ........... b1.setToolTipText("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); } }; 41 Tool tips (hints), cont’d [ http://www.exampledepot.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(MouseEvent 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().setInitialDelay(0); // immediately! Get current startup delay int initialDelay = ToolTipManager.sharedInstance().getInitialDelay(); 42 Other tricks with tool tips [ http://www.exampledepot.com/egs/javax.swing/tooltip_ImageTt.html?l=rel ] Making a tool tip visible for a specified interval of time (default: 4s) ToolTipManager.sharedInstance().setDismissDelay(x); // x – time in ms // x could be Integer.MAX_VALUE, i.e. practically forever Get current delay int dismissDelay = ToolTipManager.sharedInstance().getDismissDelay(); Multiple lines in a tip? Use html. Need an image in a tip? Use html. component.setToolTipText("<html>"+"This is a cool"+"<br>"+ "tool tip"+"</html>"); component.setToolTipText("<html><center>"+"This is another"+"<br>"+"tool tip"+"</center></html>"); String imageName = "file:image.jpg"; component.setToolTipText("<html>Here is an image <img src="+imageName+"></html>"); 43 JSlider [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] JSlider vs. AWT’s 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. 44 JSlider, a little bit of useful code (1/2) [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ ] 45 JSlider, 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. 46 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. 47 JList, cont’d [ Core Java 2, Vol. II, chapter 6 ] Word order on the JList: quick, brown, hungry, wild, silent, huge… 48 JList, cont’d [ 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.SINGLE_SELECTION); // select one item at a time wordList.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION); // select one item or one range of items 49 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 listener. 50 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 String[values.length]; System.arrayCopy(values, 0, words, 0, values.length); 51 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. 52 List selection events, cont’d [ 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 ListEvent’s 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. 53 JList example (1 / 2) [ http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JList.html ] 54 JList example (2 / 2) 55 Running the example (row-wise) 56 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. It’s 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. 57 Modifying JList, cont’d [ http://java.sun.com/docs/books/tutorial/uiswing/components/list.html ] 58 Removing 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. 59 Rendering JList elements [ http://www.java2s.com/Tutorial/Java/0240__Swing/ RenderingJListElements.htm ] 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 interface’s only method, getListCellRendererComponent, is called. 60 Rendering JList elements, example 61 Rendering JList elements, example, cont’d 62 Another example – Sundays in red, cellHasFocus used 63 JFormattedTextField (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 U L H A digit ? A letter A letter or digit * Anything A letter, with a..z mapped to their uppercase equiv. A letter, with A..Z mapped to their lowercase equiv. A hex. digit (A-F, a-f, 0-9) ‘ Used to escape another mask char 64 JFormattedTextField, cont’d DateFormat format = new SimpleDateFormat("yyyy--MMMM--dd"); DateFormatter df = new DateFormatter(format); MaskFormatter mf = new MaskFormatter("(###) ###-##-##"); // tel format // let’s now make use of the mask: JFormattedTextField ftf1 = new JFormattedTextField(df); A useful method of MaskFormatter: setPlaceholderCharacter(char) 3 formatted text fields; placeholders for the middle one 65 JFormattedTextField, source code example (1/2) [ http://www.tutorialized.com/tutorial/ Swing-s-new-JFormattedTextField-component/351 ] BoxLayout.PAGE_AXIS from top to bottom; BoxLayout.LINE_AXIS from left to right (for horizontal ComponentOrientation) 66 JFormattedTextField, source code example (2/2) [ http://www.tutorialized.com/tutorial/ Swing-s-new-JFormattedTextField-component/351 ] 67 JToolBar [ 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). 68 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.getDefaultToolkit(); //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); 69 Using custom cursors, cont’d [ http://www.tutorialized.com/tutorial/ Using-Custom-Cursors-in-Java/14024 ] Creating the Custom Cursor Put together the cursor image and the hot spot: //Create the custom cursor Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Pencil"); Displaying the Custom Cursor The final step is to notify the component to display the cursor. //Use the custom cursor setCursor(customCursor); 70 Tabbed panes [http://home.cogeco.ca/~ve3ll/jatutorc.htm#sp ] Tabbed panes allow a multilayered pane with tabs for the user to access the layer he wants. Each tab contains a single component. 71 Layered 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. 72 Layered panes, simple example [ http://home.cogeco.ca/~ve3ll/jatutorb.htm ] 73 Grabbing screenshots [ http://www.tutorialized.com/tutorial/Taking-Screenshots-in-Java/8777 ] We’ll 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); 74 A few words on the Robot class [ http://www.developer.com/java/other/article.php/2212401 ] 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. 75 A few words on the Robot class, cont’d [ http://www.developer.com/java/other/article.php/2212401 ] 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 76