Solved

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

Posted on 2002-05-31
6
6,952 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 67
changePi Challenge 15 75
topping3 challenge 14 48
unix in java example 9 38
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now