Solved

Attaching several buttons to one mouse listener

Posted on 2002-05-29
3
181 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

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

813 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

18 Experts available now in Live!

Get 1:1 Help Now