Solved

Attaching several buttons to one mouse listener

Posted on 2002-05-29
3
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

691 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