Java/Swing  Step-by-Step Guide

Tutorial 4

Application Defined Events


GOAL: Experience event-driven application with a Timer and Mouse events
PREREQUISITES: Tutorial 3

Add A Timer
  1. Add a JLabel to echo the Timer, and Change Variable Name... to  "timerEcho".  Set the text on the JLabel to be "0 seconds have passed"
  2. Add another JLabel, position it to the left of the first JLabel, and change the text on it to be "Timer echo:"
  3. Left-Mouse-Button single-click on the 'Source' button (above the picture of the JFrame, but below all the icons for running the program, etc) in order to view the source for the project.
  4. You will need to add a private variable to keep track of how many seconds have passed, so add this into the class definition:
     private int m_seconds = 0;
  5. Since NetBeans.org doesn't seem to have a 'Timer' GUI element, we'll have to create it directly in the code.  To do that, add the following code inside the constructor function for the SwingExampleFrame():
    initComponents();

    // Creates a timer that will go off every 1000 ms.
    // Each time the timer goes off the total number of seconds will be
    // incremented and the timer text field will be updated to display the
    // total seconds elapsed.
    javax.swing.Timer t = new javax.swing.Timer(1000,
        new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                    m_seconds++;
                    timerEcho.setText( m_seconds + " seconds have passed");
         }});
    t.start(); // Start the timer
     

Create Mouse Echo Text
  1. Add a JLabel to echo the Mouse position, nd Change Variable Name... to mouseEcho.  Change the text to be "(0,0)" (or anything else you'd like)
  2. Add another JLabel, position it to the left of the previous JLabel, and change the text to be "Mouse Echo:"
Mouse Move Event
  1. Right-Mouse-Button single-click on the JFrame (not on any of the GUI elements on the JFrame, but on the JFrame itself), and then Left-Mouse-Button single-click on the Events >> MouseMotion >> mouseMoved menu item to create the event handler for the mouse being moved.
  2. Modify your 'formMouseMoved' method so that it looks like the following:
    private void formMouseMoved(java.awt.event.MouseEvent evt) {
    mouseEcho.setText( "Mouse move at (" + evt.getX() + ", " + evt.getY() + ")" );
    }
Mouse-Button Events
  1. Right-Mouse-Button single-click on the JFrame (not on any of the GUI elements on the JFrame, but on the JFrame itself), and then Left-Mouse-Button single-click on the Events >> Mouse >> mouseClicked menu item to create the event handler for the mouse being moved.
  2. Modify your 'formMouseMoved' method so that it looks like the following:
     private void formMouseClicked(java.awt.event.MouseEvent evt) {

    String where = " at (" + evt.getX() + ", " + evt.getY() + ")";
    if(evt.getButton() == evt.BUTTON1)
    mouseEcho.setText("button #1 was clicked" + where);
    else if(evt.getButton() == evt.BUTTON2)
    mouseEcho.setText("button #2 was clicked" + where);
    else if(evt.getButton() == evt.BUTTON3)
    mouseEcho.setText("button #3 was clicked" + where);
    else if(evt.getButton() == evt.NOBUTTON)
    mouseEcho.setText("NO button was clicked" + where);

    }


» Written by William Frankhouser (wjf2@washington.edu)
» Adapted by Michael Panitz.
» Advised by Kelvin Sung (ksung@washington.edu) as part of the project sponsored by the National Science Foundation under Grant No. 0442420. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
» Produced in the "Essential Concepts for Building Interactive Computer Graphics Applications", A.K. Peters, Ltd.