Solved

Attaching several buttons to one mouse listener

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

17 Experts available now in Live!

Get 1:1 Help Now