Solved

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

Posted on 2002-05-31
6
6,999 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 78
mockito example issue 8 73
Error in @AspectJ Based AOP with Spring 2 14
servlet example 17 32
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

831 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