Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Multiple Dialog Opening

This is a hard question to explain - I will do my best.

The okButton on my dialog is only active when there is something in the table and a project is selected.  That works great !!
The problem is....

when I first open the dialog the button is not enabled (great) select my project and add something to the table - then button is now enabled (great )  everything is working as planned.  I click the button and dialog B opens - still everything is perfect.  If I close the dialog and reopen it - select my project and click the button now 2 dialog B opens.  If I close the dialog and reopen it and click the button - 3 dialog Bs open - etc....

I do understand what is going on - there is no looping that I am aware of
0
jkteater
Asked:
jkteater
  • 20
  • 18
  • 2
1 Solution
 
ALaRivaCommented:
You would need to post your code in order for us to really see what is going on and possibly answer this.

- Anthony
0
 
jkteaterAuthor Commented:
Does this code look OK

myModel.getOKButton().addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
    	  System.out.println("Got Here\n");   
    	  startGetReqInfoOperation(); 
    	  myModel.clearList();
         }
      });

Open in new window

0
 
ALaRivaCommented:
Yes.

That's not where the issue will be found. You'll need to post more code than just that.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
Frst quaestion - did you make the dialog instance variable and are you checking if the dialog already exists?

public class MyClass {

JDialog mydialog;



// sospmehere in  some event handling piece after you clicck the button:


if(myDialog == null) myDialog = new MyDialog(....);



}


with this it cannot open more that one dialog


Then next thing - in your actionPerformed  iimediately at the top of the method
pur some printout

System.out.println("tttt");
 so that yiu know how many events are fired when you click




0
 
for_yanCommented:
Where in this code you create yoyur dialog?

0
 
for_yanCommented:
How many times you see "Got here " printed when you click once on youtr button ?
0
 
for_yanCommented:

if you create in this method :
 startGetReqInfoOperation();

do this check on onon nul dialog inside this method when you create dialog.

Even if it  fires several events on one click once you have this check it dhoul not create more than one dialog
 
0
 
jkteaterAuthor Commented:
I placed I Got Here in the actionPerformed - an it is firing mulitple events

startGetReqInfoOperation();  that is a method setups up the call to  the dialog

private void startGetReqInfoOperation() {
      getReqOp = new EdiGetRequestTransmittalInfoOperation(appReg.getString("edi.message.getReqInfo"), session, this);
      getReqOp.addOperationListener(new EdiOperationListener(this) {
         protected void endOperationImpl() {
            try {
               reqTransInfo = getReqOp.getTransmittalInfo();
                 SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                     try {
                    	 showRequestTransmittalDialog(); [b] <====== Method to create the dialog[/b]
                     }
                     catch (Exception e) { }
                  } 
               }); 
            }
            finally {
               getReqOp.removeOperationListener(this);
               getReqOp = null;
            }
         }
      });
      session.queueOperation(getReqOp);
   } // end startGetReqInfoOperation()

Open in new window


Where the dialog is created

private void showRequestTransmittalDialog() {
	 // List workItems = workItemsModel.getAsListOfItems();
	  RequestTransmittalDialog ar = new RequestTransmittalDialog(reqTransInfo, ThreedTransIds, currentProject, this, session, appReg);
	  ar.setModal(true);
	  ar.setVisible(true);
   }

Open in new window

0
 
jkteaterAuthor Commented:
it fires a event for everything I close the dialog or it could where I a select a project. Every time I close the dialog I have to re select the project to get the button to show again.  

If I have closed the dialog 6 times it will fire 6 events
0
 
for_yanCommented:
this thing with multiple events it is sometimes  difficult to debug

But it cannot open two dialogs if you check for null - des it still open second window?
0
 
for_yanCommented:
Your closing dialog should not be connected to the same action listener - it shoyuld be connected to the button in the dialg itself - correct?
0
 
jkteaterAuthor Commented:
I don't see how the closing the dialog would effect that listener - I am starting to wonder if it does not have something to do with the selecting Project listener
0
 
jkteaterAuthor Commented:
I moved the dialog call to my cancel button

cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
               startGetReqInfoOperation();
               //dispose();
         }

Could not get it to reproduce the problem
0
 
for_yanCommented:
Check the place where you create buttons - maybe you happen to put two in one place or something like that
0
 
jkteaterAuthor Commented:
It has to have something to do with the logic of that button.  We are creating it in a singleton class and then we are enabling and disabling it in a few different places.  I am not sure how that would nest events - but somewhere it is
0
 
for_yanCommented:
No, it is perfectly OK and fine.
You are actually enabling and bisabling it in one method  in the same class ewhere the button is, but even if you were passing its reference to some other class and disabling it form there - this would not have nbeen a problem. That's perfectly OK.
 
0
 
for_yanCommented:
This is not reasonable:
myModel.getOKButton().addActionListener

why are you adding listenere through that myModel.getOKButton() ?
Even thouugh there is no prohibition but it is weird.
You should do it directly in the place where you cretae the button
0
 
for_yanCommented:
So you are sayng that when you close the dialog then another one pops up
and you see your listener firing with "Got here" ?

with this construction:
myModel.getOKButton().addActionListener()
are you sure you don't have another listener attached to the same button at
the place where  the button is created in its own class ?
0
 
jkteaterAuthor Commented:
This is step by step on what is happening
open EdiBaseDialog
make selections so the okButton enables
click okButton
listener fires GOT HERE
Dialog B opens
Close Dialog B
Close EdiBaseDailog
+++++++++++++++++++++++++++++++++++++
open  EdiBaseDailog
make selections so the okButton enables
click okButton
listener fires GOT HERE
listener fires GOT HERE
Dialog B opens
Dialog B opens
Close Dialog B
Close EdiBaseDailog
++++++++++++++++++++++++++++++++++++
open  EdiBaseDailog
make selections so the okButton enables
click okButton
listener fires GOT HERE
listener fires GOT HERE
listener fires GOT HERE
Dialog B opens
Dialog B opens
Dialog B opens

Here are the places I have anything to do with the button in the SingletonSelectTable

public class SingletonSelectTable extends AbstractTableModel{
   
   JButton okButton = new JButton("Send Transmittal");

Open in new window


public JButton getOKButton() {
	   if(edb != null && edb.getProjectStatus() && doIHaveDataInTable())enableOKButton(true);
       else enableOKButton(false);
	  return okButton;
   }

Open in new window


public void enableOKButton(boolean b) {
	   okButton.setEnabled(b);

   }

Open in new window


public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset){
      //rds.add(new RevDataset(tcRevision,selectedDataset));
	  //System.out.println("Key :" + tcRevision.toString() + " Value : " + selectedDataset.toString() + " \n");
	   RevDataset pp = new RevDataset(tcRevision,selectedDataset);
	   if(!rds.contains(pp))rds.add(pp); 
	   fireTableDataChanged();
	   getButton();
	   getOKButton();	   
   }// end add

Open in new window


public void removeSelectedRow(int row) {
	   boolean b = (rds.remove(row) != null);
	   fireTableRowsDeleted(row, row);
	   getOKButton();
	   
   }

Open in new window


This is all in the EdiBaseDialog class

private JPanel OKCancelButtons() {
	  
	  myModel.getOKButton().addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent e) {
    	  System.out.println("Got Here\n");   
    	  startGetReqInfoOperation(); 
    	  myModel.clearList();
    	 }
      })

 p.add(myModel.getOKButton());

Open in new window


The myModel Stuff

 SingletonSelectTable myModel;

public EdiBaseDialog(Frame parent, TCSession theSession){
	   super(parent, false);
	   session = theSession;
	   myModel = SingletonSelectTable.getInstance();
	   myModel.setEdb(this);
	   createDialog();  
   } //end Constructor

Open in new window


The edb stuff in SingleselectTable

EdiBaseDialog edb;

void setEdb(EdiBaseDialog edb){
	   this.edb = edb;

   }

Open in new window

0
 
jkteaterAuthor Commented:
Something like this for the action listener

 public JButton getOKButton() {
         getOKButton().addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Got Here\n");  
                        edb.startGetReqInfoOperation();
                        clearList();
                       }
                  });
        
         if(edb != null && edb.getProjectStatus() && doIHaveDataInTable())enableOKButton(true);
       else enableOKButton(false);
        
        return okButton;
   }
0
 
for_yanCommented:
It is all srtrange - you shoule not be adding llisteners right and left in side such methods:
 getOKButton().addActionListener(new ActionListener()

listenere shkould be added to the button only once after it is created in constructore or wherevere
and never added agagin.
Here every time you call the method you add a listener - no surprise it then responds to your click ten times

0
 
jkteaterAuthor Commented:
I thought that getOKButton() was actually getting my button.  that is why i did that
getOKButton().addActionListener(new ActionListener()

I see now that is not right and I do need to create the listener to the button not a method.

Where would I create the button listener for my button in the SingletonSelectTable class?

I though that this was creating the memory location and address for the button

JButton okButton = new JButton("Send Transmittal");

Open in new window


and this was actually creating the button

 public JButton getOKButton() {
	   if(edb != null && edb.getProjectStatus()&& doIHaveDataInTable())enableOKButton(true);
       else enableOKButton(false);
	  
	  return okButton;
   }

Open in new window

0
 
jkteaterAuthor Commented:
I do think that the whole issue is based of the listener and probably the way it is being used
0
 
for_yanCommented:
Yes, the button should be created once and the listener should be attached once - ususlaly in the
same class. If you attach many listeners (espcially these very inconvenient anonymous listeners which I frankly never use)
then no surprise you'll have your event handled as many times.
If you want to have method getButton() then the only thing it should do - it should return handle to
the button which you could use to change buttons properties and nothing else.
0
 
jkteaterAuthor Commented:
ok I have remove the listener from the EdiBaseDialog Class

This is what I have tried

public class SingletonSelectTable extends AbstractTableModel{

     
      JButton okButton = new JButton("Send Transmittal");
      ActionListener actionListener = new ActionListener() {
	      public void actionPerformed(ActionEvent actionEvent) {
	        System.out.println("Selected \n");
	        //clearList();
	      }
   };

Open in new window


Then in this method I am attaching it

public JButton getOKButton() {
	   if(edb != null && edb.getProjectStatus()&& doIHaveDataInTable()){
		   enableOKButton(true);
		   okButton.addActionListener(actionListener);
	   }
       else {
    	   enableOKButton(false);
       }
	  
	  return okButton;
   }

Open in new window


but that does not seem right either
0
 
jkteaterAuthor Commented:
I think if I understand what you are saying is everytime that method gets called a new Listener is created

That makes sense looking at the results
0
 
for_yanCommented:

here you are not addding actionlistenere to the button at all:
public class SingletonSelectTable extends AbstractTableModel{

     
      JButton okButton = new JButton("Send Transmittal");
      ActionListener actionListener = new ActionListener() {
	      public void actionPerformed(ActionEvent actionEvent) {
	        System.out.println("Selected \n");
	        //clearList();
	      }
   };

Open in new window

0
 
jkteaterAuthor Commented:
I added it in the getOKButton method

okButton.addActionListener(actionListener);
0
 
for_yanCommented:
Now, that you haev it this way still stick to them the way they are

okButton.addCationListeneer(new actionListener() {...});

but you do it onl;y once - immediately after the button is cretaed.
and never do it again.

The much better (but by now forgotten by many) way to cretae listeners is
to decalre at the top if your method

public class EdibaseDialog implemnets ActionLisrtener {
...

then when you creta button you say;

okButton.addActionListener(this);


and then somewiere among the methods of you claass
you should put method

public void actionPerformed(ActionEvent ae) {
//handle event here

}


A little bit more tyoping but far more undrestandable and trackable - why they added these innere classes to java - just to spoil the language!

But for now don't change to that - it will be too mauhc change for now for you.
leave them as they are -anonymouse,
but make sure you add listener only once immediately after  creation of button

0
 
jkteaterAuthor Commented:
Help me understand something

JButton okButton = new JButton("Send Transmittal"); this is a class variable so okButton can be used through out the  class.  But it seems that I can not add a listener to the button unless it is a method
0
 
for_yanCommented:


>I added it in the getOKButton method

>okButton.addActionListener(actionListener);

still not good

It should not be in any method. It should be where you create the btton - that qwill make it once
as you create button once and add listener once
0
 
jkteaterAuthor Commented:
JButton okButton = new JButton("Send Transmittal");
 
   okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              System.out.println("Selected \n");  
        }
   });       
             

I get a error on the addActionListener - saying  Syntax error on token "addActionListener", = expected after
       this token
0
 
for_yanCommented:


JButton okButton = new JButton("Send Transmittal");  - this is not instance varaiable (don't call them 'class" - class is usualy about static and you don't need to have it static)

Insatnce will be likwe that:

public class Whatevere {

JButton okButton;  <--- not in method not in constructore - before everything!

public  Whatever() {

okButton = new JButton(...);  <--- this si instance !



}

then you can get accvess to okButton everywheer in the class, and you can have method
getbuttion() to be called from another class.
This okButton will beciomme property  of the instance of your class
Otherwise it is just local variable - quit e different wanimal


read about it - this is the main stone of java - instance vs local - nowhere to go without it!
stop everything and understand that - nothing can be more important about java - guaranteed.



0
 
jkteaterAuthor Commented:
but if I put it in a method it does not fail
0
 
for_yanCommented:
Instance variable should be decalred outside of any method.

It should be constructed inside constructor or method.

therefore thsi should not happedn with instance varaiable:

JButton okButton = new JButton("Send Transmittal");

Stop doing everything else and understadn this, without this undersatding nothing wuill work

0
 
for_yanCommented:
I don't know - hat's becaue tythere is anothe mistake cancelling it.

You should know the difference and you canniot do getButton() from another cvlass if it
is bnot instance variable.

You' rahter follow my suggestion as it was before - you dion't need
any getButton()

you need to have button instanc, to have method enableButton(boolean)
and you should pass handles to your EdibaseDialog and selectSomething classes to each other and with taht youdon't need to pass any buttons.
Thsi si much more logicla 0 read my post to the previous quiuestion.
 
0
 
jkteaterAuthor Commented:
That was the problem - You hit it right on the head.  See if the way I done this is OK - it works

JButton okButton;

\\constructor
private SingletonSelectTable() {       
	   okButtonMethod();
   }

Open in new window


void okButtonMethod() {
	   okButton = new JButton("HelpMe");
	   okButton.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
		  System.out.println("Selected \n");
		  edb.startGetReqInfoOperation();
		  clearList();
		  }
	   });	  
   }

Open in new window

0
 
for_yanCommented:
yes, this is fine- it was decalred at the top of the class not in the method, in the method youninstantiate it, but it is avaialable everwhere in the class
and also can be exposed on the methods to another class - this is instance varaiable - the actual property of the class
 okButton = new JButton("HelpMe");

if it is variable inside the method it is accessible only to the method - it is something in the process of calculation - by no measn the
prioprty of the whole instance of ther class, so you cannot expose it and pass it to another class - such would be local variable

And theye are two very different things.

And actionlistnere which you add to the button, even anonymous is also
a property of the bbutton. So you add it only once - and wehere ever you get hold of the instance of the button
the listener will already be attached to taht button; so you defiitely don't want to attach it again.


0
 
jkteaterAuthor Commented:
for_Yan you have thought me so much, and for that I thank you.  It seems like each day you are teaching me something and I appreciate that very much.  I know there are times you are ready to throw in the towel, but you stay  with me and help me.


THANKS!!!
0
 
for_yanCommented:
OK. You are always welcome.

But do review this stuff - there are actually not that many things you really need to know absoolutely 100%.
You just can't live without them.

They spoiled language in the later years significantly with all these inner classes, generics, etc and now
there are much more subtle things which are difficult to understand that people get digressed
from the basics.  Well, that's unfortunately not up to me to change.

But this stuff about loocal, instance and static(or class)  varaibles  and about how classes interact passing references to
instances to each other - this is very crucial - and not so much about
syntax, like those later changes, but about the essence of object oriented programming - this
needs to be understood aboslutely. Read about it and if you don't understand anything - ask questions - I'm sure we'll
resolve them - but it will save you a lot of time.



0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 20
  • 18
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now