dcsimg
e.getSource() -- distinguishing between instances of the source button
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bill_Baxter
Posted On:   Friday, May 31, 2002 10:03 AM

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 e   More>>

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.    <<Less

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

Posted By:   Samir_Randive  
Posted On:   Saturday, June 1, 2002 04:45 PM

First of all why do you want to use a mouselistener to get a button clicked event? You can use an actionlistener instead.


Do you instantiate both the entryForm and dataForm in the StartingClass? If so you can associate an entryForm with a dataForm by passing a reference to the dataForm to the constructor of the enrtyForm.


You can have the entryForm class in this way..

public class EntryForm
{
DataForm data_form;
....
....

public EntryForm(Dataform dataForm)
{
this.data_form = dataForm;
....
....
submitButton.addActionListener
( new ActionListener ()
{
public void actionPerformed(ActionEvent ae)
{
//Add data to the dataform.
}
});
...
}


Hope this helps.




cheers....


Samir
About | Sitemap | Contact