Board index » jbuilder » Action performed event handler fires twice

Action performed event handler fires twice


2004-06-25 07:58:48 PM
jbuilder4
Hi,
I have a JComboBox event handler which reads a file corresponding to the
selection, using BufferedReader, then populates a JTextArea with the file
contents. This is the code I'm concerned about:
void showManifestFile(String filename) throws IOException {
// Populate the list box with the contents of the specified file
String inputLine;
try {
inStream = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
inStream.close();
showMessageBox("Unable to open " + filename + " on local machine.", true);
}
while (true) {
inputLine = inStream.readLine();
if (inputLine == null) break;
if (inputLine.length() == 0) continue;
jManifestInfoTextArea.append(inputLine + "\n");
}
inStream.close();
} // showManifestFile
The problem is that the event handler is being called twice, so the text
ends up in the text area twice. I read somewhere that it may be because the
event handler doesn't register completion until the input stream has closed,
so that the next time it checks for events, it hasn't been satisfied yet and
gets fired again.
Is this true? I have other combo boxes with very similar code, except that a
BufferedReader is not involved, and the action performed events only fire
once, as expected.
Any suggestions please?
Brian
 
 

Re:Action performed event handler fires twice

"BrianW" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

I have a JComboBox event handler which reads a file corresponding to the
selection, using BufferedReader, then populates a JTextArea with the file
contents. This is the code I'm concerned about:

void showManifestFile(String filename) throws IOException {

// Populate the list box with the contents of the specified file

String inputLine;

try {

inStream = new BufferedReader(new FileReader(filename));

} catch (FileNotFoundException e) {

inStream.close();

showMessageBox("Unable to open " + filename + " on local machine.",
true);

}

while (true) {

inputLine = inStream.readLine();

if (inputLine == null) break;

if (inputLine.length() == 0) continue;

jManifestInfoTextArea.append(inputLine + "\n");

}

inStream.close();

} // showManifestFile



The problem is that the event handler is being called twice, so the text
ends up in the text area twice. I read somewhere that it may be because
the
event handler doesn't register completion until the input stream has
closed,
so that the next time it checks for events, it hasn't been satisfied yet
and
gets fired again.



Is this true? I have other combo boxes with very similar code, except that
a
BufferedReader is not involved, and the action performed events only fire
once, as expected.



Any suggestions please?



Brian
I wondered if it was because I was running it in debug mode, but it still
does it in release mode. However I have implemented the obvious workaround -
toggle a boolean and return without doing anything if it is false. There has
to be a better solution though!
Brian
 

Re:Action performed event handler fires twice

On 6/25/2004 at 7:58:48 AM, BrianW wrote:
Quote
This is the code I'm concerned about:

void showManifestFile(String filename) throws IOException {
[snip]
Quote
The problem is that the event handler is being called twice,
so the text ends up in the text area twice.
It seems likely that the problem is related to how you register listeners
for the events and which events you respond to, rather than in the code
that you posted, which seems to implement the actions taken in response to
the events.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:Action performed event handler fires twice

"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
On 6/25/2004 at 7:58:48 AM, BrianW wrote:

>This is the code I'm concerned about:
>
>void showManifestFile(String filename) throws IOException {
[snip]

>The problem is that the event handler is being called twice,
>so the text ends up in the text area twice.

It seems likely that the problem is related to how you register listeners
for the events and which events you respond to, rather than in the code
that you posted, which seems to implement the actions taken in response to
the events.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
Not the case I think. I've moved the BufferedReader stuff into a different
component, and it now also fires the action-performed event twice. I made no
other changes.
Brian
 

Re:Action performed event handler fires twice

On 6/28/2004 at 6:00:37 PM, BrianW wrote:
Quote
>It seems likely that the problem is related to how you register
>listeners for the events and which events you respond to,
Not the case I think...
If you will provide a runnable example of the problem, I will be happy
to take a look at it.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Action performed event handler fires twice

BrianW wrote:
Quote
>
Not the case I think. I've moved the BufferedReader stuff into a different
component, and it now also fires the action-performed event twice. I made
no other changes.

Brian
Make sure that in your initialization procedures (ie like jbInit()) The
method is not declared twice. This will happen when you click in the object
inspector on a ActionPerformed() event more than once.
 

Re:Action performed event handler fires twice

BrianW wrote:
Quote
The problem is that the event handler is being called twice, so the text
ends up in the text area twice. I read somewhere that it may be because the
event handler doesn't register completion until the input stream has closed,
so that the next time it checks for events, it hasn't been satisfied yet and
gets fired again.
Right, because the ListSelectionEvent from the JComboBox fires twice,
first time for unselecting, second time for selecting the new item.
You can select just one by using the getValueIsAdjusting() method of the
event e.g.
doSomething(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting()) justDoThatThing();
}
leo
 

Re:Action performed event handler fires twice

Ive had a similar problem before...i found that the problem was that the
action performed handler was recognising the button push as a action and the
button release as action so when the button was pushed in, it ran the code
and when the button was released it ran it again.
i changed to mousePressed for the button and it solved the problem, dont
know if its the same for you, but try it anyway!
Goodluck
"leov" < XXXX@XXXXX.COM >wrote in message
Quote
BrianW wrote:


>The problem is that the event handler is being called twice, so the text
>ends up in the text area twice. I read somewhere that it may be because
the
>event handler doesn't register completion until the input stream has
closed,
>so that the next time it checks for events, it hasn't been satisfied yet
and
>gets fired again.

Right, because the ListSelectionEvent from the JComboBox fires twice,
first time for unselecting, second time for selecting the new item.
You can select just one by using the getValueIsAdjusting() method of the
event e.g.

doSomething(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting()) justDoThatThing();
}

leo
 

Re:Action performed event handler fires twice

I was having a similar problem with a JButton. I have an application
that consists of a bunch of "sub-applications" (for lack of a better
term). The "sub-application" is a JPanel with a bunch of other JPanels
(which are additional sub-applications that are unified under this
sub-application). Example. The sub-application is called "ERO". it
consists of several other JPanels (on a JTabbedPane), one of which is
"NewEro". Whenever I pressed the "Submit" button on "NewEro" the
database save would fire twice.
To fix this problem I moved the "actionAdapter" and the assigment of
.addActionListener for the button on the "NewEro" panel to the "ERO"
panels, code.
This is the code for the "actionAdapter" created by JBuilder:
class JPanelNewEro_jButtonSubmit_actionAdapter implements
java.awt.event.ActionListener {
JPanelNewEro adaptee;
JPanelNewEro_jButtonSubmit_actionAdapter(JPanelNewEro adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButtonSubmit_actionPerformed(e);
// The jButtonSubmit_actionPerformed(e); method remains in the //
"newEro" class.
}
}
What was happening here is that "adaptee" is created as a panel and
"ERO" had instantiated a "NewEro" panel and even though I was using the
"this" keyword it seemed that there were two panels associated with the
"this" keyword, therefore, whenever this "actionAdapter" processed the
call it processed it for both references to the "this" keyword.
Assignment of the "addActionListener" looks like this:
jPanelNewEro.jButtonSubmit.addActionListener(new
JPanelNewEro_jButtonSubmit_actionAdapter(this.jPanelNewEro));
Hopefully that helps!
BrianW wrote:
Quote
Hi,

I have a JComboBox event handler which reads a file corresponding to the
selection, using BufferedReader, then populates a JTextArea with the file
contents. This is the code I'm concerned about:

void showManifestFile(String filename) throws IOException {

// Populate the list box with the contents of the specified file

String inputLine;

try {

inStream = new BufferedReader(new FileReader(filename));

} catch (FileNotFoundException e) {

inStream.close();

showMessageBox("Unable to open " + filename + " on local machine.", true);

}

while (true) {

inputLine = inStream.readLine();

if (inputLine == null) break;

if (inputLine.length() == 0) continue;

jManifestInfoTextArea.append(inputLine + "\n");

}

inStream.close();

} // showManifestFile



The problem is that the event handler is being called twice, so the text
ends up in the text area twice. I read somewhere that it may be because the
event handler doesn't register completion until the input stream has closed,
so that the next time it checks for events, it hasn't been satisfied yet and
gets fired again.



Is this true? I have other combo boxes with very similar code, except that a
BufferedReader is not involved, and the action performed events only fire
once, as expected.



Any suggestions please?



Brian