Tuesday, October 18, 2011

Mouse Events in Java AWT

Let's consider the class we had in the article on how to close a frame in Java AWT. To use a mouse event we need to define another adapter class that will extend the MouseAdapter class. The MouseAdapter subclass will also be nested in the "main" class, since it's easier this way to access the members and methods of the "main" class (just like the CloseWindowAdapter class in the previous example).
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame1 extends Frame
{
    //This controls should be declared as internal variables
    //in order to be accessed by the nested classes
    private Button but;
    private Label lbl;
    
    private class CloseWindowAdapter extends WindowAdapter 
    {        
        public void windowClosing(WindowEvent we)
        {
            TestFrame1.this.setVisible(false);
            TestFrame1.this.dispose();
            System.exit(0);
        }
    }
    
    private class TestMouseAdapter extends MouseAdapter
    {
        public void mouseClicked(MouseEvent e) 
        {    
            //Checks if it's right click
            if(e.isMetaDown()==true)
                lbl.setText("RIGHT CLICK");
            //If it's not right-click, then it's left click
            else
                lbl.setText("LEFT CLICK");
        }
    }
    
    public TestFrame1()
    {
        super("Test frame");
        this.addWindowListener(new CloseWindowAdapter());
        this.setLayout(new FlowLayout());
        //Creating a button
        but = new Button("Trigger click event");
        //Adding the a mouse listener for the button
        but.addMouseListener(new TestMouseAdapter());
        //Creating a label to show what event will be triggered
        lbl = new Label("Nothing happened yet");
        //Adding the controls to the frame
        this.add(lbl);
        this.add(but);
        //Packing the frame
        this.pack();
    }
    
}
In the example above, the frame contains two controls : a button and a label. If you would right click the button, the label's text would become "RIGHT-CLICK". If you would left click the button, the label's text would become "LEFT-CLICK". The MouseEvent object can also provide several other checks:
 //Returns true if the ALT key was pressed at the time of the click
 e.isAltDown();
 //Returns true if the ALT GR key was pressed at the time of the click
 e.isAltGraphDown();
 //Returns true if the CTRL key was pressed at the time of the click
 e.isControlDown();
 //Returns true if the SHIFT key was pressed at the time of the click
 e.isShiftDown();
 //Returns a reference to the control who triggered the event
 e.getSource();
 //Returns a Point object containing the x,y coordinates 
 //relative to the source component
 e.getPoint();
The TestMouseAdapter subclass implements only the mouseClicked event trigger. The MouseAdapter ancestor class would had allowed the implementation of several other event triggers:
  • mouseEntered - occurs when the mouse cursor enters the component
  • mouseExited - occurs when the mouse cursor leaves the component
  • mousePressed - occurs when a mouse button is pressed on the component
  • mouseReleased - occurs when a mouse button is no longer pressed on the component 
The mouseClicked event trigger which we had used occurs when the component was clicked (pressed and then released).

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger