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

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.
Who is Participating?
objectsConnect With a Mentor Commented:
Can you post where submitButton is defined?
It's not defined as static is it?  If so then thats your problem.
Jim CakalicSenior Developer/ArchitectCommented:
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.

javaQQAuthor Commented:

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.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Jim CakalicSenior Developer/ArchitectCommented:
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);


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);

    public DuplicateButton() {
        _frame = new JFrame("Duplicate Button");
        _frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
        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));
    public static void main(String[] args) {
        new DuplicateButton();
javaQQAuthor Commented:

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

Many thanks.
Good to hear you got it fixed :-)

Brainbench MVP for Java 1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.