Public void valuechanged listselectionevent e

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // set up event handler Create a new JScrollPane colorList.addListSelectionListener( object, initialize it with a JList, new ListSelectionListener() { and attach it to the content pane. public void valueChanged( ListSelectionEvent e ) { c.setBackground( colors[ colorList.getSelectedIndex() ] ); } } Change the color according to the item ); selected (use getSelectedIndex). setSize( 350, 150 ); show(); } public static void main( String args ) { ListTest app = new ListTest(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 12.20: MouseDetails.java // Demonstrating mouse clicks and // distinguishing between mouse buttons. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MouseDetails extends JFrame { private String s = ""; private int xPos, yPos; public MouseDetails() { super( "Mouse clicks and buttons" ); addMouseListener( new MouseClickHandler() ); setSize( 350, 150 ); show(); } public void paint( Graphics g ) { g.drawString( "Clicked @ [" + xPos + ", " + yPos + "]", xPos, yPos ); } Another example, illustrating mouse events in AWT and Swing Add a listener for a mouse click. 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 public static void main( String args ) { MouseDetails app = new MouseDetails(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); Use a named inner class as the event handler. Can still } inherit from MouseAdapter (extends MouseAdapter). } ); } // inner class to handle mouse events private class MouseClickHandler extends MouseAdapter { public void mouseClicked( MouseEvent e ) { Use getClickCount, isAltDown, xPos = e.getX(); and isMetaDown to determine the yPos = e.getY(); String to use. String s = "Clicked " + e.getClickCount() + " time(s)"; if ( s else s else s e.isMetaDown() ) // Right mouse button += " with right mouse butt...
View Full Document

This note was uploaded on 08/06/2008 for the course CSE 432 taught by Professor Blank during the Fall '08 term at Lehigh University .

Ask a homework question - tutors are online