} } // class PopupPanel in Java Maker PDF417 in Java } } // class PopupPanel

} } // class PopupPanel generate, create none none in none projects Microsoft Windows Official Website The applet s none none interface consists of an instance of a subclass of JPanel called PopupPanel to which two subpanels are added. An instance of a MouseAdapter subclass, using the anonymous inner class technique, is created and added to the MouseListener list of both panels. Mouse clicks therefore go to the adapter, which looks for the popup menu request.

If detected, then an instance of JPopupMenu with the color menu items is displayed. (Systems differ as to whether the popup signal is generated on a mouse button press or release so both are tested here.) When the user selects an item in the popup menu list, the actionPerformed() method is invoked and the background color is set according to which menu item was selected and over which panel.

Note that you don t necessarily need to use a JPopupMenu object in response to an af rmative isPopupTrigger() response. You could always open a dialog window instead. For example, you could create a popup dialog for a JTextField to allow the user to enter a new value for some parameter that you clicked on.

. 12.6 Handling keystrokes Just as you c none none an catch mouse button clicks, you can also identify which key the user struck. A class that implements the KeyListener interface must provide the. More techniques and tips Figure 12.3 F none for none or the KeyTestApplet demonstration, when the top panel has the focus, keystrokes are captured and reported on the text area..

keyTyped (Key Event e) method. An instance of this KeyListener can then be added to the list of such listeners maintained by a Component subclass. When a key press occurs, the component invokes the keyTyped() method of all the listeners in its list.

From the KeyEvent object you can then obtain the. identity of t none for none he key pressed by the user:. public void k eyTyped (KeyEvent e) { saySomething ("got " + e.getKeyChar (), e);. The KeyEvent none none object provides the key character via the getKeyChar() method. The example KeyTestApplet holds an instance of a JPanel subclass called KeyTestPanel, which in turn holds a subpanel and a JTextArea. We add an instance of a KeyAdapter to the subpanel s list of KeyListener objects.

The subpanel is made focusable so that it can receive the key events. (When the applet runs, you should click on it and hit tab to put the focus on the subpanel.) The adapter sends each KeyEvent object to the keyTyped() method whenever the user presses a key (see Figure 12.

3).. . . .

Code in KeyTestApplet to display a KeyTestPanel object . . .

/** A JPanel class that detects key strokes on one subpanel * and displays messages about them in a text area. **/ class KeyTestPanel extends JPanel. JTextArea fTe none none xtArea; /** Create an interface with a text area and a blank panel. * Key strokes while the panel has focus will be detected. 12.6 Handling keystrokes * and a messa none none ge printed in the text area. **/ KeyTestPanel () { setLayout (new GridLayout (2,1)); JPanel canvas = new JPanel (); add (canvas, BorderLayout.NORTH); canvas.

setBackground (Color.YELLOW); fTextArea = new JTextArea (); fTextArea.setEditable (false); // Add to a scroll pane so that a long list of // keyinputs can be seen.

JScrollPane area-scroll-pane = new JScrollPane (fTextArea); add (area-scroll-pane, BorderLayout.CENTER); // Add to the panel an anonymous KeyAdapter that will // respond to key strokes. canvas.

addKeyListener ( new KeyAdapter () { public void keyTyped (KeyEvent e) { saySomething ("got " + e.getKeyChar (), e);. } } // end anonymous class ); // end met none none hod call // Let the canvas panel get the focus. canvas.setFocusable (true); } // ctor /** Display a message in text area about the key event.

**/ void saySomething (String eventDescription, KeyEvent e) { fTextArea.append (eventDescription + " on " + e.getComponent ().

getClass ().getName () + "\n");.
Copyright © . All rights reserved.