Solved

e.getSource() -- distinguishing between instances of the source button

Posted on 2002-05-31
6
7,016 Views
Last Modified: 2010-05-18
UPDATED: Attaching several buttons to one mouse listener
e.getSource() -- distinguishing between instances of the source button


Responses to a previous post have told me that the following code is supposed to work:

      public void mouseClicked( MouseEvent e ) {
            if( e.getSource() == StartingClass.entryform1.submitButton ) {
                  System.out.println( "Clicked: entryform1.submitButton" );
            }  
            if( e.getSource() == StartingClass.entryform2.submitButton ) {
                  System.out.println( "Clicked: entryform2.submitButton" );
            }  
            ...
      }

In my case, the code does not work, and I have no idea why.
Please take a look at the code fragments below (102 lines) and give me a checklist of the sorts of errors in the surrounding code that would cause the code shown to fail.
For example, would the "static" in the declarations (the compiler demands them) cause problems in the mouse listener?


The application consists of a GUI form, "EntryForm," and "DataForm," a set of variables.
Clicking an instance of EntryForm's submit button should send the entries to the appropriate instance of DataForm.
The submit button is attached to a mouse listener "SubmitButtonListener."
I attach an arbitrary number up to five instances of EntryForm to ONE mouse listener, "SubmitButtonListener"
THE PROBLEM: The mouse listener registers that a submit button has been clicked, but is not able to distinguish WHICH of the instances' button.
                        Strangely, clicking on the last instances's button responds for all the instances (please see the printout below).

--------------------------------- the code: ---------------------------------

class EntryForm {
...
}

class DataForm {
...
}



In the class "EntryForm":

      final SubmitButtonListener sbl = new SubmitButtonListener( ... );

      submitButton.addMouseListener( sbl );

In the instantiating class "StartingClass":

      static EntryForm entryform1;
      static EntryForm entryform2;
      static EntryForm entryform3;
      static EntryForm entryform4;
      static EntryForm entryform5;

      ...
      
      entryform1 = new EntryForm();
      entryform1.f.setLocation( 20, 20 );

      entryform2 = new EntryForm();
      entryform2.f.setLocation( 40, 40 );

      entryform3 = new EntryForm();
      entryform3.f.setLocation( 60, 60 );

      entryform4 = new EntryForm();
      entryform4.f.setLocation( 80, 80 );

      entryform5 = new EntryForm();
      entryform5.f.setLocation( 100, 100 );


In the mouse listener "SubmitButtonListener":
      public void mouseClicked( MouseEvent e ) { // mouseClicked

            System.out.println( "StartingClass.entryform1: " + StartingClass.entryform1 );
            System.out.println( "StartingClass.entryform2: " + StartingClass.entryform2 );
            System.out.println( "StartingClass.entryform3: " + StartingClass.entryform3 );
            System.out.println( "StartingClass.entryform4: " + StartingClass.entryform4 );
            System.out.println( "StartingClass.entryform5: " + StartingClass.entryform5 );
            System.out.println( "" );

            if( e.getSource() == StartingClass.entryform1.submitButton ) {
                  System.out.println( "Mouse Clicked: " + e );
                  System.out.println( "Clicked: entryform1.submitButton" );
                  //      send data to dataForm1;
            }  
            if( e.getSource() == StartingClass.entryform2.submitButton ) {
                  System.out.println( "Mouse Clicked: " + e );
                  System.out.println( "Clicked: entryform2.submitButton" );
                  //      send data to dataForm2;
            }  
            if( e.getSource() == StartingClass.entryform3.submitButton ) {
                  System.out.println( "Mouse Clicked: " + e );
                  System.out.println( "Clicked: entryform3.submitButton" );
                  //      send data to dataForm3;
            }  
            if( e.getSource() == StartingClass.entryform4.submitButton ) {
                  System.out.println( "Mouse Clicked: " + e );
                  System.out.println( "Clicked: entryform4.submitButton" );
                  //      send data to dataForm4;
            }  
            if( e.getSource() == StartingClass.entryform5.submitButton ) {
                  System.out.println( "Mouse Clicked: " + e );
                  System.out.println( "Clicked: entryform5.submitButton" );
                  //      send data to dataForm5;
            }

      }

The results:
      1. The console prints out references to the entryforms properly, but

            "MainMenuHandler6_XPTL.orderform1: Orderform4[,0,0,0x0,invalid,layout= ...
            MainMenuHandler6_XPTL.orderform2: Orderform4[,0,0,0x0,invalid,layout= ...
            MainMenuHandler6_XPTL.orderform3: Orderform4[,0,0,0x0,invalid,layout= ...
            MainMenuHandler6_XPTL.orderform4: Orderform4[,0,0,0x0,invalid,layout= ...
            MainMenuHandler6_XPTL.orderform5: Orderform4[,0,0,0x0,invalid,layout= ... "

      2. Only entryform5's submit button responds to a click, but it prints out ALL of the other buttons lines:

            "Mouse Clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(20,25), ...
            Clicked: orderform1.secBarBtnE
            Mouse Clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(20,25), ...
            Clicked: orderform2.secBarBtnE
            Mouse Clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(20,25), ...
            Clicked: orderform3.secBarBtnE
            Mouse Clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(20,25), ...
            Clicked: orderform4.secBarBtnE
            Mouse Clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(20,25), ...
            Clicked: orderform5.secBarBtnE"


What adjustments are needed to fix my code?
Is there another way to accomplish the goal of sending the data from entryForm## to dataForm##.

Many thanks in advance.
0
Comment
Question by:javaQQ
  • 2
  • 2
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7047512
You aren't by chance adding the same JButton to all the form objects are you? The only way I can tend to duplicate your problem is by using the same JButton repeatedly. If I do that then there is only one JButton object and so the listener cannot differentiate them. But if I use different JButtons and register a single listener instance with each, then the one common listener can distinguish between them because they are different objects.

Jim
0
 

Author Comment

by:javaQQ
ID: 7047553
jim;

There is one class, "EnterForm" which contains a JButton, "submitButton"

I  create five instances of the form (each with a button)

submitButton is attached to the listener in the form.

As I understand what you are saying,
"use different JButtons and register
a single listener instance with each,"
that would require me to use five separate forms, not five instances of the same form object.

Please clarify.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7047855
I've tried to duplicate the problem as you've described but with no success. Here's my code which seems to work just as I'd expect. There must be something else going on here. To help any further, I'm afraid that I'll need to see substantially more of your code related to construction of the UI.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class EntryForm {
    public JPanel _panel;
    public JButton _button;

    public EntryForm(int n, MouseListener ml) {
        _panel = new JPanel();
        _button = new JButton("Submit " + n);
        _button.addMouseListener(ml);
        _panel.add(_button);
    }

}

public class DuplicateButton {
    JFrame _frame;
    EntryForm _form1, _form2, _form3, _form4, _form5;

    private class SubmitListener extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if (e.getSource() == _form1._button) {
                System.out.println("form1: " + e);
            }
            if (e.getSource() == _form2._button) {
                System.out.println("form2: " + e);
            }
            if (e.getSource() == _form3._button) {
                System.out.println("form3: " + e);
            }
            if (e.getSource() == _form4._button) {
                System.out.println("form4: " + e);
            }
            if (e.getSource() == _form5._button) {
                System.out.println("form5: " + e);
            }
            //System.out.println(e);
        }
    }


    public DuplicateButton() {
        _frame = new JFrame("Duplicate Button");
        _frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        SubmitListener l = new SubmitListener();
        _form1 = new EntryForm(1, l);
        _form2 = new EntryForm(2, l);
        _form3 = new EntryForm(3, l);
        _form4 = new EntryForm(4, l);
        _form5 = new EntryForm(5, l);
        Container pane = _frame.getContentPane();
        pane.setLayout(new FlowLayout(5));
        pane.add(_form1._panel);
        pane.add(_form2._panel);
        pane.add(_form3._panel);
        pane.add(_form4._panel);
        pane.add(_form5._panel);
        _frame.pack();
        _frame.show();
    }
    public static void main(String[] args) {
        new DuplicateButton();
    }
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7048054
Can you post where submitButton is defined?
It's not defined as static is it?  If so then thats your problem.
0
 

Author Comment

by:javaQQ
ID: 7048364
objects;

Bingo!
The button was defined as static.
Changing it (and making other required adjustments ) solves the problem.

Many thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 7049027
Good to hear you got it fixed :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 71
eclipse apache tomcat admin console 52 121
Selenium docs api java index 3 48
swing controls 2 16
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question