Most Sample Code from "Java How to Program, Fifth Edition" from Deitel and Deitel
Discussion on solutions from last week's exercise.
Answers are now posted at:
http://www2.cs.uregina.ca/~nova/Java_Study/Examples6/Exercise_s6.html
You have three selection modes to chose from when using JLists:
Check out this example which allows only one item (a color) to be selected at a time.
Note that the JList generates a ListSelectionEvent which is handled by a ListSelectionListener.
The code for setting the mode of the JList so that only one item can be selected is:
colorList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
The code for setting the background color is:
container.setBackground ( colors[ colorList.getSelectedIndex() ]);
*Note: here we are using the Colors class with some predefined colors such as Color.BLACK. To find more about Colors, you can look up the "Color" class in the sun documents (under java.awt) http://java.sun.com/j2se/1.5.0/docs/api/index.html
You might be curious how to create lists where you can select multiple items (by using the CRTL-click and Shift-click keys). The sample code is below:
Notice that there are two JLists. Try using the Shift-click and CTRL-click to see how both lists respond.
"Mouse events can be trapped for any GUI component that derives from java.awt.Component" (page 642, Deitel)
A component can generate a MouseEvent which is handled by either a MouseListener or a MouseMotionListener. Both of these are interfaces; therefore, any class that implements them, must define specific funtions.
To implement a MouseListener, you must define:
To implement a MouseMotionListener, you must define:
The following code implements MouseListener and MouseMotionListener:
Try moving, clicking, dragging, and pressing your mouse over and on this application.
You will notice that the MouseEvent stores the x and y coordinates of the mouse and that the coordinates are accessible through the getX() and getY() functions.
Consider all the functions that you have to define in order to create a MouseMotionListener and MouseListener (7 functions all together). What if, for instance, you only want define a specific action when the mouse is clicked. If you are implementing the MouseListener interface, the "contract" that you make with the compiler indicates that you have to define five functions: mousePressed, mouseClicked, mouseReleased, mouseEntered, and mouseExited. You are lazy and only want to define the mouseClicked function. This is where adapter classes come in very handy.
"An adapter class implements an interface and provides a default implementation (with an empty method body) of every method in the interface." (page 646, Deitel)
The following table (from page 646 of Deitel) summarizes the Adapter Classes and the Interfaces that they implement:
Event-adapter class | Implements Interface |
---|---|
ComponentAdapter |
ComponentListener |
ContainerAdapter | ContainerListener |
FocusAdapter | FocusListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
MouseMotionAdapter | MouseMotionListener |
WindowAdapter | WindowListener |
The following code shows an example of using a MouseMotionAdapter:
A few points about this code:
You might be wondering how you can tell if a double click was made or if the mouse was a right mouse click or a left mouse click. The mouseEvent object contains information not only about the location (x and y coordinates) of the event, but also about which mouse was clicked and how many times it was clicked.
The following code demonstrates accessing more details about a mouse click:
Pressing a key generates a KeyEvent, which is handled by a KeyListener.
If you implement a KeyListener, you must provide the following three functions:
keyPressed
is invoked when you press any key. keyTyped
is invoked when you press any key that is not an action key. (Action keys are any arrow key, Home, End, Page Up, Page Down, any function key, Num Lock, Print Screen, Scroll Lock, Caps Lock and Pause).keyReleased
is invoked when the key is released after any keyPressed
or keyTyped
event (page 651, Deitel)
The following program demonstrates these KeyListener events:
A few notes::
getKeyCode
--gets the vitual key code of the key that was pressed."Class KeyEvent
maintains a set of constants--the virtual key-code constants--that represents every key on the keyboard. These constants can be compared with the return value of getKeyCode
to test for individual keys on the keyboard." (page 653, Deitel) getKeyCode
is passed to KeyEvent
method getKeyText
, which returns a string containing the name of the key that was pressed." (pages 653 and 654, Deitel). This is useful for keys such as the Alt key, which does not show up when you hit it. getKeyChar
gets the Unicode value of the character typedDifferences between JTextField and JTextArea:
Check out this example of using JTextArea:
Notes:
textArea2.setText(textArea1.getSelectedText()
To try:
box.add( new JScrollPane( textArea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ) );
"Combining graphics and Swing GUI components can lead to incorrect display of the graphics, the GUI components or both. Using JPanels for drawing can eliminate this problem by providing a dedicated graphics area" (page 682, Deitel)
"Swing components that inherit from class JComponent
contain method paintComponent for drawing in the context of lightweight Swing GUI. When customizing a JPanel
for use as a dedicated drawing area, the subclass should override method paintComponent and should call the superclass version of the paintComponent as the first statement in the body of the overridden method" (page 682, Deitel)
See the following code for an example of customizing Panels for drawing:
This program draws either a circle or a square depending on what button has been pressed.
Note:
"calling repaint for the CustomPanel
schedules a painting operation only for the CustomPanel
, not for the entire GUI. Method paintComponent will be called to repaint the CustomPanel
and draw the appropriate shape." (page 684, Deitel)
The following example demonstrates how a panel can be designed to handle it's own events. Notice how mouse events to the panel are handled differently from mouse events to the frame:
This code draws an oval based on the starting click and drag of a mouse.
Click and Drag on the panel. Click and Drag on the frame.
Notice how there are separate mouse listeners for the panel and for the frame in the code.
See what happens when start the click on the panel and drag into background of the application and vice versa.
"A mouse drag operation begins with a mouse-pressed event. All subsequent mouse drag events (until the user releases the mouse button) are sent to the GUI component that receives the original mouse-pressed event" (page 691, Deitel)
Instigator | Event Object | Listener | Function needed to implement Listener Interface |
---|---|---|---|
JButton |
ActionEvent | ActionListener | actionPerfomed |
JCheckBox |
ItemEvent | ItemListener | itemStateChanged |
JList | ListSelectionEvent | ListSelectionListener | valueChanged |
Component | MouseEvent | MouseListener |
mousePressed |
MouseMotionListener | mouseDragged mouseMoved |
||
AnyKey Press | KeyEvent | KeyListener |
keyPressed |