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


JLists

You have three selection modes to chose from when using JLists:

  1. SINGLE_SELECTION-- which means that you only select one item from a list. (CTRL-click and Shift-click are not supported in this mode)
  2. SINGLE_INTERVAL_SELECTION--which means that you can select a group of items (Shift-click is support)
  3. MULTIPLE_INTERVAL_SELECTION--Ctrl-click and Shift-click are supported in this mode

Check out this example which allows only one item (a color) to be selected at a time.

ListTest.java

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:

MultipeSelectionTest.java

Notice that there are two JLists. Try using the Shift-click and CTRL-click to see how both lists respond.


Mouse Events

"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:

MouseTracker.java

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:

Painter.java

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:

MouseDetails.java


Key Events

Pressing a key generates a KeyEvent, which is handled by a KeyListener.

If you implement a KeyListener, you must provide the following three functions:

  1. keyPressed
  2. keyReleased
  3. keyTyped

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:

KeyDemo.java

A few notes::


JTextArea

Differences between JTextField and JTextArea:

Check out this example of using JTextArea:

TextAreaDemo.java

Notes:

To try:


Panels for Drawing

"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)


Summary of Events and Listeners Covered in the Study Group

Instigator Event Object Listener Function needed to implement Listener Interface

JButton
JTextField

ActionEvent ActionListener actionPerfomed

JCheckBox
JRadioButton
JComboBox

ItemEvent ItemListener itemStateChanged
JList ListSelectionEvent ListSelectionListener valueChanged
Component MouseEvent

MouseListener
 
 
 
 

mousePressed
mouseClicked
mouseReleased
mouseEntered
mouseExited

MouseMotionListener mouseDragged
mouseMoved
AnyKey Press KeyEvent

KeyListener
 
 

keyPressed
keyReleased
keyTyped