Article View: borland.public.jbuilder.java.swing
Article #817Re: Action performed event handler fires twice
From: Dante
Date: Tue, 20 Jul 2004 14:10
Date: Tue, 20 Jul 2004 14:10
108 lines
4281 bytes
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
>
>
>
>
Technical Details
Message-ID:
<40fd281c@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!news-out.visi.com!petbe.visi.com!news.octanews.net!ash.uu.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<2k2icaF16gj6pU1@uni-berlin.de>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,11c992ff0658e7ea X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 20 Jul 2004 10:10:06 -0400 From: Dante <hamingjur@yahoo.com> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Action performed event handler fires twice References: <2k2icaF16gj6pU1@uni-berlin.de> In-Reply-To: <2k2icaF16gj6pU1@uni-berlin.de> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 141.210.24.23 Message-ID: <40fd281c@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1090332700 141.210.24.23 (20 Jul 2004 07:11:40 -0700) Lines: 109 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!news-out.visi.com!petbe.visi.com!news.octanews.net!ash.uu.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail