ch14 - Chapter 14 Advanced GUI Topics Mouse Events...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 14 Advanced GUI
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Mouse Events MouseListener MouseMotionListener JPanels Layout Managers Flow Layout Border Layout GridLayout BoxLayout Event Listeners ItemEvents ChangeEvents Other components JCheckBox JRadioButton JComboBox JList JScrollPane
Background image of page 2
Handling Mouse Events Mouse events include such user interactions as moving the mouse, dragging the mouse, and clicking the mouse buttons. Java divides mouse-related events into two categories clicking generates a MouseEvent moving or dragging the mouse generates a MouseMotionEvent
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Handling Mouse Events A Ch14TrackMouseFrame object is an event source for mouse events. We will allow this object to be an event listener as well. Its class must therefore implement the MouseListener interface. Class Ch14TrackMouseFrame extends Frame implements MouseListener { }
Background image of page 4
Handling Mouse Events The MouseListener interface has five abstract methods: mouseClicked mouseEntered mouseExited mousePressed mouseReleased The argument to all five methods is an instance of MouseEvent .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Handling Mouse Events The mouseClicked method is called every time the left mouse button is clicked (pressed down and released). If we want to detect the mouse button press and release separately, we can provide a method body to the mousePressed and mouseReleased methods, respectively. mouseEntered and mouseExited are called when the mouse over and away from the component generating the events.
Background image of page 6
Mouse Events The getX and getY methods of MouseEvent retrieve the x and y coordinate values of wherever the mouse is clicked. The getClickCount method of MouseEvent will detect the number of mouse clicks performed, so different events may be triggered by a single click or double click, for example.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Mouse Buttons When either the right or left mouse button is pressed, the event listener’s mousePressed method is called. To determine which mouse button is pressed inside the mousePressed method, we call the isMetaDown method of MouseEvent . The isMetaDown method returns true if the right button is pressed. There is also an isAltDown method which can be used to check for the middle mouse button
Background image of page 8
MouseMotionEvents Implementing the MouseMotionListener interface will allow us to track mouse dragging. The MouseMotionListener interface has two abstract methods: mouseDragged mouseMoved The argument to both methods is an instance of MouseEvent .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Similar to the mousePressed method, the mouseDragged method is called regardless of which button was down as the mouse was dragged. The
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 65

ch14 - Chapter 14 Advanced GUI Topics Mouse Events...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online