For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2005 > Buttons events dispatched twice









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Buttons events dispatched twice
Aldemar Cuartas Carvajal via JavaKB.com

2005-04-19, 8:58 pm

Hi!

Into my main class I Create JDialogs, and I use JDialog.DISPOSE_ON_CLOSE as
defaultCloseOperation and setVisible(true) in order to show it. When I run
my application for first time, and clicking on menu item I create a
JDialog, so, I can use Toolbar buttons with no problems, actionPerformed is
executed normally, just once, everything works OK. I close my Dialog and re-
entering for second time begin problems. Toolbar buttons actionPerformed
are executed twice, what happen?, this problem I noted, happens exactly
when JDialog are loaded (created with new) for second time or more times.
It is not supposed closing JDialog, will dispose every component content
into it?, I tried using windowClosing event doing super.dispose(); and
this.dispose(); but nothing at all. Is like closing JDialog it leave it
hidden but not destroyed even using DISPOSE_ON_CLOSE. It is just only thing
I guess actionPerformed on button events be dispatched twice, I don't know!

I use jdk1.5.0_02 (update 2)

Thanks
klynn47@comcast.net

2005-04-19, 8:58 pm

In your actionPerformed, do you check to see if the button is the
source of the event?

Aldemar Cuartas Carvajal via JavaKB.com

2005-04-19, 8:58 pm

I'm adding JButtons on toolbar by for loop like this:

for (int i = 0; .....; i++) {
JButton myButton = new JButton(..);
myButton.addActionListener(this);
myButton.addActionCommand(String.Format("%d", i));
....
}

So you can Note I use actionCommand as Button ID. and...

public void actionPerformed(...e) {
if (((JButton)e.getSource).getActionCommand.equals("0") {
<instructions>
}
if (((JButton)e.getSource).getActionCommand.equals("1") {
<instructions>
}

...
}

when JDialog loas for first time (created using new) actionPerformed
dispatched well and instructions are executed OK. BUt exiting from JDialog
and re-entering againg actionPerformed are dispatched twice so,
instructions are executed twice.
sanjay manohar

2005-04-19, 8:58 pm

Does your class have any static members?
If your JButtons are stored static class members, this could result in
the problem you are describing.

Aldemar Cuartas Carvajal via JavaKB.com

2005-04-20, 4:00 am

No!

How I said buttons are dynamically created like as: JButton myButton = new
JButton(); no static member.

I just tried on windowClosing - JDialog, setVisible(false); and dispose();
but nothing happen, trying to destroy whatever dialog visible and hidden
could have.
Nigel Wade

2005-04-20, 8:58 am

Aldemar Cuartas Carvajal via JavaKB.com wrote:

> Hi!
>
> Into my main class I Create JDialogs, and I use JDialog.DISPOSE_ON_CLOSE

as
> defaultCloseOperation and setVisible(true) in order to show it. When I run
> my application for first time, and clicking on menu item I create a
> JDialog, so, I can use Toolbar buttons with no problems, actionPerformed

is
> executed normally, just once, everything works OK. I close my Dialog and

re-
> entering for second time begin problems. Toolbar buttons actionPerformed
> are executed twice, what happen?, this problem I noted, happens exactly
> when JDialog are loaded (created with new) for second time or more times.
> It is not supposed closing JDialog, will dispose every component content
> into it?, I tried using windowClosing event doing super.dispose(); and
> this.dispose(); but nothing at all. Is like closing JDialog it leave it
> hidden but not destroyed even using DISPOSE_ON_CLOSE. It is just only

thing
> I guess actionPerformed on button events be dispatched twice, I don't

know!
>
> I use jdk1.5.0_02 (update 2)
>
> Thanks


Are you adding an additional actionListener, so that two actions are
performed for the one event?

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
sanjay manohar

2005-04-22, 8:58 pm

Note that, although all Objects (eg windows and frames) are disposed on
closing, the Classes are not always. This would mean that any
information stored in static variables would remain in memory.
Are you creating the window in a static member variable? like this

class MyApp{
static JFrame myFrame = new MyFrame();
public static void main(String[] s){
myFrame.setVisible(true);
}
}

if so, your frame object may persist across different instatiations of
the app.

You could try using System.exit() when the application is closed. if it
is a static problem, this might fix it.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com