Archive for the 'J2EE' Category

682 Graphical User Interface Components: Part 1 Chapter (Web design tools)

Sunday, February 10th, 2008

682 Graphical User Interface Components: Part 1 Chapter 12 Fig. 12.17 Fig. 12.17 Demonstrating mouse event handling (part 3 of 3). Each mouse event results in a String displayed in JLabel statusBar at the bottom of the window. Lines 21 22 in the constructor define JLabelstatusBarand attach it to the content pane. Until now, each time we used the content pane, method setLayoutwas called to set the content pane s layout manager to a FlowLayout. This allowed the content pane to display the GUI components we attached to it from left to right. If the GUI components do not fit on one line, the FlowLayoutcreates additional lines to continue displaying the GUI components. Actually, the default layout manager is a BorderLayoutthat divides the content pane s area into five regions north, south, east, west and center. Line 22 uses a new version of Containermethod addto attach statusBarto the region Border- Layout.SOUTH, which extends across the entire bottom of the content pane. We discuss BorderLayoutand several other layout managers in detail later in this chapter. Lines 25 26 in the constructor register the MouseTrackerwindow object as the listener for its own mouse events. Methods addMouseListener and addMouse- MotionListener are Component methods that can be used to register mouse event listeners for an object of any class that extends Component. When the mouse enters or exits the application area, method mouseEntered (lines 56 59) and method mouseExited(lines 62 65) are called, respectively. Method mouse- Exiteddisplays a message in statusBarindicating that the mouse is outside the application (see the first sample output window). Method mouseEntered displays a message dialog box indicating that the mouse entered the application window. [Note: Be sure to press Enter to dismiss the message dialog, rather than using the mouse. If you use the mouse to dismiss the dialog, when you move the mouse over the window again, mouseEnteredredisplays the dialog. This will prevent you from trying the other mouse events.] Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface (Web hosting domain names) Components: Part 1

Saturday, February 9th, 2008

Chapter 12 Graphical User Interface Components: Part 1 681 40 41 // handle event when mouse pressed 42 public void mousePressed( MouseEvent event ) 43 { 44 statusBar.setText( “Pressed at [” + event.getX() + 45 “, ” + event.getY() + “]” ); 46 } 47 48 // handle event when mouse released after dragging 49 public void mouseReleased( MouseEvent event ) 50 { 51 statusBar.setText( “Released at [” + event.getX() + 52 “, ” + event.getY() + “]” ); 53 } 54 55 // handle event when mouse enters area 56 public void mouseEntered( MouseEvent event ) 57 { 58 JOptionPane.showMessageDialog( null, “Mouse in window” ); 59 } 60 61 // handle event when mouse exits area 62 public void mouseExited( MouseEvent event ) 63 { 64 statusBar.setText( “Mouse outside window” ); 65 } 66 67 // MouseMotionListener event handlers 68 69 // handle event when user drags mouse with button pressed 70 public void mouseDragged( MouseEvent event ) 71 { 72 statusBar.setText( “Dragged at [” + event.getX() + 73 “, ” + event.getY() + “]” ); 74 } 75 76 // handle event when user moves mouse 77 public void mouseMoved( MouseEvent event ) 78 { 79 statusBar.setText( “Moved at [” + event.getX() + 80 “, ” + event.getY() + “]” ); 81 } 82 83 // execute application 84 public static void main( String args[] ) 85 { 86 MouseTracker application = new MouseTracker(); 87 88 application.setDefaultCloseOperation( 89 JFrame.EXIT_ON_CLOSE ); 90 } 91 92 } // end class MouseTracker Fig. 12.17 Fig. 12.17 Demonstrating mouse event handling (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Unable to start debugging on the web server - 680 Graphical User Interface Components: Part 1 Chapter

Saturday, February 9th, 2008

680 Graphical User Interface Components: Part 1 Chapter 12 Look-and-Feel Observation 12.8 Method calls to mouseDraggedare sent to the MouseMotionListenerfor the Componenton which the drag operation started. Similarly, the mouseReleasedmethod call is sent to the MouseListenerfor the Componenton which the drag operation started. The MouseTracker application (Fig. 12.17) demonstrates the MouseListener and MouseMotionListener methods. The application class implements both interfaces so it can listen for its own mouse events. Note that all seven methods from these two interfaces must be defined by the programmer when a class implements both interfaces. The message dialog box in the sample output windows appears when the user moves the mouse into the application window. 1 // Fig. 12.17: MouseTracker.java 2 // Demonstrating mouse events. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class MouseTracker extends JFrame 12 implements MouseListener, MouseMotionListener { 13 14 private JLabel statusBar; 15 16 // set up GUI and register mouse event handlers 17 public MouseTracker() 18 { 19 super( “Demonstrating Mouse Events” ); 20 21 statusBar = new JLabel(); 22 getContentPane().add( statusBar, BorderLayout.SOUTH ); 23 24 // application listens to its own mouse events 25 addMouseListener( this ); 26 addMouseMotionListener( this ); 27 28 setSize( 275, 100 ); 29 setVisible( true ); 30 } 31 32 // MouseListener event handlers 33 34 // handle event when mouse released immediately after press 35 public void mouseClicked( MouseEvent event ) 36 { 37 statusBar.setText( “Clicked at [” + event.getX() + 38 “, ” + event.getY() + “]” ); 39 } Fig. 12.17 Fig. 12.17 Demonstrating mouse event handling (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Geocities web hosting - Chapter 12 Graphical User Interface Components: Part 1

Friday, February 8th, 2008

Chapter 12 Graphical User Interface Components: Part 1 679 ponent that derives from java.awt.Component. The methods of interfaces MouseListenerand MouseMotionListenerare summarized in Figure 12.16. Each of the mouse event handling methods takes a MouseEventobject as its argument. A MouseEventobject contains information about the mouse event that occurred, including the x-and y-coordinates of the location where the event occurred. The MouseListener and MouseMotionListenermethods are called automatically when the mouse interacts with a Component if listener objects are registered for a particular Component. Method mousePressed is called when a mouse button is pressed with the mouse cursor over a component. Using methods and constants of class InputEvent (the superclass of MouseEvent), a program can determine which mouse button the user clicked. Method mouse- Clicked is called whenever a mouse button is released without moving the mouse after a mousePressedoperation. Method mouseReleased is called whenever a mouse button is released. Method mouseEntered is called when the mouse cursor enters the physical boundaries of a Component. Method mouseExited is called when the mouse cursor leaves the physical boundaries of a Component. Method mouseDragged is called when the mouse button is pressed and held, and the mouse is moved (a process known as dragging). The mouseDragged event is preceded by a mousePressed event and followed by a mouseReleasedevent. Method mouseMoved is called when the mouse is moved with the mouse cursor over a component (and no mouse buttons pressed). MouseListenerand MouseMotionListenerinterface methods Methods of interface MouseListener public void mousePressed( MouseEvent event ) Called when a mouse button is pressed with the mouse cursor on a component. public void mouseClicked( MouseEvent event ) Called when a mouse button is pressed and released on a component without moving the mouse cursor. public void mouseReleased( MouseEvent event ) Called when a mouse button is released after being pressed. This event is always preceded by a mousePressedevent. public void mouseEntered( MouseEvent event ) Called when the mouse cursor enters the bounds of a component. public void mouseExited( MouseEvent event ) Called when the mouse cursor leaves the bounds of a component. Methods of interface MouseMotionListener public void mouseDragged( MouseEvent event ) Called when the mouse button is pressed with the mouse cursor on a component and the mouse is moved. This event is always preceded by a call to mousePressed. public void mouseMoved( MouseEvent event ) Called when the mouse is moved with the mouse cursor on a component. Fig. 12.16 MouseListenerand MouseMotionListenerinterface methods. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

678 Graphical User Interface Components: Part (Web site translator) 1 Chapter

Thursday, February 7th, 2008

678 Graphical User Interface Components: Part 1 Chapter 12 71 // execute application 72 public static void main( String args[] ) 73 { 74 MultipleSelection application = new MultipleSelection(); 75 76 application.setDefaultCloseOperation( 77 JFrame.EXIT_ON_CLOSE ); 78 } 79 80 } // end class MultipleSelection Fig. 12.15 Using a multiple-selection JList(part 3 of 3). Line 29 creates JListcolorListand initializes it with the Strings in the array colorNames. Line 30 sets the number of visible rows in colorListto 5. Line 31 uses JList method setFixedCellHeight to specify the height in pixels of each item in the JList. We do this to ensure that the rows in both JLists in the example have the same height. Lines 32 33 specify that colorList is a MULTIPLE_INTERVAL_SELECTION list. Line 34 adds a new JScrollPane containing colorList to the content pane. Lines 59 65 perform similar tasks for JList copyList, which is defined as a SINGLE_INTERVAL_SELECTIONlist. Line 61 uses JListmethod set- FixedCellWidthto set copyList s width to 100 pixels. A multiple-selection list does not have a specific event associated with making multiple selections. Normally, an event generated by another GUI component (known as an external event) specifies when the multiple selections in a JListshould be processed. In this example, the user clicks JButtoncopyButton to trigger the event that copies the selected items in colorListto copyList. When the user clicks copyButton, method actionPerformed (line 45 50) is called. Lines 48 49 use JList method setListData to set the items displayed in copyList. Line 49 calls colorList s method getSelectedValues, which returns an array of Objects representing the selected items in colorList. In this example, the returned array is passed as the argument to copyList s setListDatamethod. Many students ask how reference copyListcan be used in line 48, when the program does not create the object to which it refers until Line 59. Remember that method action- Performedat lines 45 50 does not execute until the user presses the copyButton, which cannot occur until after the call to the constructor completes. At that point in the program s execution, line 59 already has initialized copyListwith a new JListobject. 12.11 Mouse Event Handling This section presents the MouseListener and MouseMotionListener event-listener interfaces for handling mouse events. Mouse events can be trapped for any GUI com Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: (Web site hosting) Part 1

Wednesday, February 6th, 2008

Chapter 12 Graphical User Interface Components: Part 1 677 18 19 // set up GUI 20 public MultipleSelection() 21 { 22 super( “Multiple Selection Lists” ); 23 24 // get content pane and set its layout 25 Container container = getContentPane(); 26 container.setLayout( new FlowLayout() ); 27 28 // set up JList colorList 29 colorList = new JList( colorNames ); 30 colorList.setVisibleRowCount( 5 ); 31 colorList.setFixedCellHeight( 15 ); 32 colorList.setSelectionMode( 33 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); 34 container.add( new JScrollPane( colorList ) ); 35 36 // create copy button and register its listener 37 copyButton = new JButton( “Copy >>>” ); 38 39 copyButton.addActionListener( 40 41 // anonymous inner class for button event 42 new ActionListener() { 43 44 // handle button event 45 public void actionPerformed( ActionEvent event ) 46 { 47 // place selected values in copyList 48 copyList.setListData( 49 colorList.getSelectedValues() ); 50 } 51 52 } // end anonymous inner class 53 54 ); // end call to addActionListener 55 56 container.add( copyButton ); 57 58 // set up JList copyList 59 copyList = new JList( ); 60 copyList.setVisibleRowCount( 5 ); 61 copyList.setFixedCellWidth( 100 ); 62 copyList.setFixedCellHeight( 15 ); 63 copyList.setSelectionMode( 64 ListSelectionModel.SINGLE_INTERVAL_SELECTION ); 65 container.add( new JScrollPane( copyList ) ); 66 67 setSize( 300, 120 ); 68 setVisible( true ); 69 } 70 Fig. 12.15 Using a multiple-selection JList(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

676 Graphical User Interface Components: Part 1 Chapter (X web hosting)

Tuesday, February 5th, 2008

676 Graphical User Interface Components: Part 1 Chapter 12 the automatic scrolling capability for the JList. Line 42 adds a new instance of class JScrollPaneto the content pane. The JScrollPaneconstructor receives as its argument the JComponentfor which it will provide automatic scrolling functionality (in this case JList colorList). Notice in the screen captures that a scrollbar created by the JScrollPane appears at the right side of the JList. By default, the scrollbar appears only when the number of items in the JList exceeds the number of visible items. Lines 45 59 use JList method addListSelectionListener to register an instance of an anonymous inner class that implements ListSelectionListener (defined in package javax.swing.event) as the listener for JList colorList. When the user makes a selection from colorList, method valueChanged(line 51 55) executes and sets the background color of the content pane with method setBackground(inherited from class Componentinto class Container). The color is selected from the array colorswith the selected item s index in the list that is returned by JList method getSelectedIndex (as with arrays, JListindexing is zero based). 12.10 Multiple-Selection Lists A multiple-selection list enables the user to select many items from a JList. A SINGLE_INTERVAL_SELECTION list allows selection of a contiguous range of items in the list by clicking the first item, then holding the Shift key while clicking the last item to select in the range. A MULTIPLE_INTERVAL_SELECTION list allows continuous range selection as described for a SINGLE_INTERVAL_SELECTIONlist and allows miscellaneous items to be selected by holding the Ctrl key (sometimes called to Control key)while clicking each item to select. To deselect an item, hold the Ctrl key while clicking the item a second time. The application of Fig. 12.15 uses multiple-selection lists to copy items from one JList to another. One list is a MULTIPLE_INTERVAL_SELECTIONlist and the other is a SINGLE_INTERVAL_SELECTIONlist. When you execute the program, try using the selection techniques described above to select items in both lists. 1 // Fig. 12.15: MultipleSelection.java 2 // Copying items from one List to another. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class MultipleSelection extends JFrame { 12 private JList colorList, copyList; 13 private JButton copyButton; 14 15 private String colorNames[] = { “Black”, “Blue”, “Cyan”, 16 “Dark Gray”, “Gray”, “Green”, “Light Gray”, 17 “Magenta”, “Orange”, “Pink”, “Red”, “White”, “Yellow” }; Fig. 12.15 Using a multiple-selection JList(part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Apache web server for windows - Chapter 12 Graphical User Interface Components: Part 1

Tuesday, February 5th, 2008

Chapter 12 Graphical User Interface Components: Part 1 675 49 50 // handle list selection events 51 public void valueChanged( ListSelectionEvent event ) 52 { 53 container.setBackground( 54 colors[ colorList.getSelectedIndex() ] ); 55 } 56 57 } // end anonymous inner class 58 59 ); // end call to addListSelectionListener 60 61 setSize( 350, 150 ); 62 setVisible( true ); 63 } 64 65 // execute application 66 public static void main( String args[] ) 67 { 68 ListTest application = new ListTest(); 69 70 application.setDefaultCloseOperation( 71 JFrame.EXIT_ON_CLOSE ); 72 } 73 74 } // end class ListTest Fig. 12.14 Selecting colors from a JList(part 2 of 2). A JListobject is instantiated at line 34 and assigned to reference colorListin the constructor. The argument to the JListconstructor is the array of Objects (in this case Strings) to display in the list. Line 35 uses JList method setVisibleRowCount to determine the number of items that are visible in the list. Lines 38 39 use JList method setSelectionMode to specify the list selection mode. Class ListSelectionModel (package javax.swing) defines constants SINGLE_SELECTION, SINGLE_INTERVAL_SELECTIONand MULTIPLE_INTERVAL_SELECTION to specify a JList s selection mode. A SINGLE_SELECTION list allows only one item to be selected at a time. A SINGLE_INTERVAL_SELECTION list is a multiple-selection list that allows several items in a contiguous range in the list to be selected. A MULTIPLE_INTERVAL_SELECTION list is a multiple-selection list that does not restrict the items that can be selected. Unlike JComboBox, JLists do not provide a scrollbar if there are more items in the list than the number of visible rows. In this case, a JScrollPaneobject is used to provide Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

674 Graphical User Interface Components: Part 1 Chapter (Web design seattle)

Monday, February 4th, 2008

674 Graphical User Interface Components: Part 1 Chapter 12 The application of Fig. 12.14 creates a JList of 13 colors. When a color name is clicked in the JList, a ListSelectionEvent occurs and the application window content pane s background color changes. 1 // Fig. 12.14: ListTest.java 2 // Selecting colors from a JList. 3 4 // Java core packages 5 import java.awt.*; 6 7 // Java extension packages 8 import javax.swing.*; 9 import javax.swing.event.*; 10 11 public class ListTest extends JFrame { 12 private JList colorList; 13 private Container container; 14 15 private String colorNames[] = { “Black”, “Blue”, “Cyan”, 16 “Dark Gray”, “Gray”, “Green”, “Light Gray”, “Magenta”, 17 “Orange”, “Pink”, “Red”, “White”, “Yellow” }; 18 19 private Color colors[] = { Color.black, Color.blue, 20 Color.cyan, Color.darkGray, Color.gray, Color.green, 21 Color.lightGray, Color.magenta, Color.orange, Color.pink, 22 Color.red, Color.white, Color.yellow }; 23 24 // set up GUI 25 public ListTest() 26 { 27 super( “List Test” ); 28 29 // get content pane and set its layout 30 container = getContentPane(); 31 container.setLayout( new FlowLayout() ); 32 33 // create a list with items in colorNames array 34 colorList = new JList( colorNames ); 35 colorList.setVisibleRowCount( 5 ); 36 37 // do not allow multiple selections 38 colorList.setSelectionMode( 39 ListSelectionModel.SINGLE_SELECTION ); 40 41 // add a JScrollPane containing JList to content pane 42 container.add( new JScrollPane( colorList ) ); 43 44 // set up event handler 45 colorList.addListSelectionListener( 46 47 // anonymous inner class for list selection events 48 new ListSelectionListener() { Fig. 12.14 Selecting colors from a JList(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: Part 1 (Medical web site)

Sunday, February 3rd, 2008

Chapter 12 Graphical User Interface Components: Part 1 673 67 application.setDefaultCloseOperation( 68 JFrame.EXIT_ON_CLOSE ); 69 } 70 71 } // end class ComboBoxTest A scrollbar to scroll through scroll arrows scroll box the items in the list. Fig. 12.13 Fig. 12.13 Program that uses a JComboBoxto select an icon (part 3 of 3). Look-and-Feel Observation 12.7 Set the maximum row count for a JComboBoxto a number of rows that prevents the list from expanding outside the bounds of the window or applet in which it is used. This will ensure that the list displays correctly when it is expanded by the user. Lines 34 50 register an instance of an anonymous inner class that implements Item- Listener as the listener for JComboBox images. When the user makes a selection from images, method itemStateChanged (line 40 46) sets the Icon for label. The Iconis selected from array iconsby determining the index number of the selected item in the JComboBoxwith method getSelectedIndexin line 45. Note that line 43 changes the icon only for a selected item. The reason for the if structure here is that for each item that is selected from a JComboBox, another item is deselected. Thus, two events occur for each item selected. We wish to display only the icon for the item the user just selected. 12.9 JList A list displays a series of items from which the user may select one or more items. Lists are created with class JList, which inherits from class JComponent. Class JList supports single-selection lists (i.e., lists that allow only one item to be selected at a time) and multiple-selection lists (lists that allow any number of items to be selected). In this section, we discuss single-selection lists. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01