🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
9 messages
9 total messages Started by BrianW Fri, 25 Jun 2004 11:58
Action performed event handler fires twice
#789
Author: BrianW
Date: Fri, 25 Jun 2004 11:58
61 lines
2058 bytes
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
#791
Author: BrianW
Date: Fri, 25 Jun 2004 20:22
76 lines
2560 bytes
"BrianW" <brian@nospam.net> wrote in message
news:2k2icaF16gj6pU1@uni-berlin.de...
> 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
#792
Author: John McGrath [TeamB]
Date: Fri, 25 Jun 2004 23:19
23 lines
1535 bytes
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:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Action performed event handler fires twice
#794
Author: BrianW
Date: Mon, 28 Jun 2004 22:00
32 lines
1785 bytes
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:40dcb30b$1@newsgroups.borland.com...
> 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:
>     http://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
#795
Author: John McGrath [TeamB]
Date: Tue, 29 Jun 2004 01:25
18 lines
1404 bytes
On 6/28/2004 at 6:00:37 PM, BrianW wrote:

> > 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:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Action performed event handler fires twice
#796
Author: Paul Nichols (TeamB)
Date: Wed, 30 Jun 2004 04:11
14 lines
1360 bytes
BrianW wrote:


>>

> 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
#798
Author: leov
Date: Wed, 30 Jun 2004 10:16
20 lines
1499 bytes
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
#803
Author: Ed
Date: Wed, 14 Jul 2004 14:27
33 lines
2096 bytes
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" <leov@emailaccount.com> wrote in message
news:2kfiblF1o5g8U1@uni-berlin.de...
> 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
#817
Author: Dante
Date: Tue, 20 Jul 2004 14:10
108 lines
4281 bytes
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:
> 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
> 
> 
> 
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads