Solved

Attaching several buttons to one mouse listener

Posted on 2002-05-29
3
177 Views
Last Modified: 2010-03-31

I have an application which consists of a GUI form, "EntryForm," and "DataForm," a collection of variables.
Clicking EntryForm's submit button sends the entries to DataForm.
The submit button is attached to a mouse listener "SubmitButtonHandler"

My Question: How can I attach an arbitrary number, up to five instances of EntryForm to ONE mouse listener?

I have tried this:

[code]
     final SubmitButtonHandler sbh = new SubmitButtonHandler( ...

     entryForm1 = new EntryForm();
     dataForm1 = new DataForm();
          entryForm1.submitButton.addMouseListener( sbh );
     entryForm2 = new EntryForm();
     dataForm2 = new DataForm();
          entryForm2.submitButton.addMouseListener( sbh );
     entryForm3 = new EntryForm();
     dataForm3 = new DataForm();
          entryForm3.submitButton.addMouseListener( sbh );
     entryForm4 = new EntryForm();
     dataForm4 = new DataForm();
          entryForm4.submitButton.addMouseListener( sbh );
     entryForm5 = new EntryForm();
     dataForm5 = new DataForm();
          entryForm5.submitButton.addMouseListener( sbh );
     ...
[/code]
In the mouse listener I have tried these approaches:

[code]
     public void mouseClicked( MouseEvent e ) {

          if ( e.getSource() == entryForm1.submitButton ) {
               System.out.println( "Clicked: entryForm1.submitButton" );
               // send the data to dataform1
          } else if( e.getSource() == entryForm2.submitButton ) {
               System.out.println( "Clicked: entryForm2.submitButton" );
               // send the data to dataform2
          } ...

     public void mouseClicked( MouseEvent e ) {

          if ( e.getSource() == submitButton ) {
               if( e.getSource() == entryForm1.submitButton ) {
               // send the data to dataform1
               } else if( e.getSource() == entryForm2.submitButton ) {
               // send the data to dataform2
               }
          } else if ( e.getSource() == anotherButton ) {
               ...
[/code]
In both cases, only ONE form's button registers and even that is screwy.

Can this approach be fixed so that the mous listener can discern between the various forms, or do I have to use five sets of forms and mouse listeners?

Many thanks in advance.

0
Comment
Question by:javaQQ
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7043276
The first approach should work (assuming they are accessing the same variables).

WHat class are your submit buttons?
If they are Button's or JButton's then an ActionListener would be more appropriate than a MouseListener
0
 

Author Comment

by:javaQQ
ID: 7047378
Thanks, objects, ActionListener solves one of my problems.
0
 
LVL 92

Expert Comment

by:objects
ID: 7047953
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

22 Experts available now in Live!

Get 1:1 Help Now