🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #621

Re: Disable a button??

#621
From: John McGrath [TeamB]
Date: Sat, 26 Mar 2005 01:26
48 lines
2949 bytes
On 3/25/2005 at 1:36:55 PM, Davisote wrote:

> This should be the same code to disable the button, but sometimes
> For example, if I delete all the text the button doesn't disable
> 
>  void jTextField1_keyPressed(KeyEvent e) {
>     if (jTextField1.getText().length() > 0) {
>       jButton1.setEnabled(true);
>     }
>     else {
>       jButton1.setEnabled(false);
>     }
>   }

The real problem here is that the code is listening to the wrong thing.
The code needs to know when the text field is modified, but it listens for
keyboard events.  It is making the assumption that the contents of the
text field will only be modified as the result of a keystroke, and that
the text field will process the keystrokes before the listener.  Neither
of these assumptions are correct.

The text field could be modified in response to keystrokes, but it could
also change as the result of mouse events, bar code scans, voice commands,
or whatever.  In these cases, the code would not appropriately disable or
enable the button if it listens for keystrokes.

And although the order in which event listeners will be notified is not
specified, the listener code will run *before* the text field processes
the keystrokes.  So the code will always be one step behind in disabling
the button.

The code needs to know when the text of the field changes.  JTextField, as
with all standard Swing components, does not actually store any data; that
is stored in the model for the component.  In the case of a JTextField and
other JTextComponent subclasses, the model is a Document.

So what the code needs to know is when the contents of the Document for
the JTextField changes.  That can be achieved by adding a DocumentListener
to the Document.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <xn0e06k3u1mwvy000@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <42441118@newsgroups.borland.com> <4244454a$1@newsgroups.borland.com> <42445a44@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,cb273dd3cca84955
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "John McGrath  [TeamB]" <ng-b@JPMcGrath.net>
Subject: Re: Disable a button??
Newsgroups: borland.public.jbuilder.java.swing
References: <42441118@newsgroups.borland.com> <4244454a$1@newsgroups.borland.com> <42445a44@newsgroups.borland.com>
User-Agent: XanaNews/1.17.3.1
Message-ID: <xn0e06k3u1mwvy000@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:0000031D
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
NNTP-Posting-Host: 141.158.33.210
Date: 25 Mar 2005 17:26:20 -0800
X-Trace: newsgroups.borland.com 1111800380 141.158.33.210 (25 Mar 2005 17:26:20 -0800)
Lines: 48
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail