Article View: borland.public.jbuilder.java.swing
Article #11637JTextField mask?
From: Denis Sarrazin
Date: Mon, 03 May 1999 00:00
Date: Mon, 03 May 1999 00:00
67 lines
2637 bytes
2637 bytes
Hi,
I'm using JBuilder 2 (latest patch) and Swing under Windows 95
(w/64MB RAM -- not the fastest thing in the world <s>).
I'm trying to make it so that a JTextField will only accept valid
integer values (but I'd like to use a similar method for other kinds
of masked data entry). I'm probably doing this the wrong way since
I'm still learning Java/Swing, but here is what I've done so far:
First, I've added the "implements DocumentListener" to the JDialog
class that contains the JTextFields in question.
Then, I add the following event listeners to each fields (I know
that the event works because they do other things as well, such as
enable the "Save" button):
txtST.getDocument().addDocumentListener(this);
txtDX.getDocument().addDocumentListener(this);
txtIQ.getDocument().addDocumentListener(this);
txtHT.getDocument().addDocumentListener(this);
In the class itself, I handle the three events defined in the
DocumentListener interface (the first event handler -- ie the
insertUpdate event handler -- is the one I'm trying to use to
validate the number for now):
public void insertUpdate(DocumentEvent e) {
Document thisDoc = e.getDocument();
if (thisDoc == txtDX.getDocument()) {
try {
int nValue = Integer.parseInt(txtDX.getText());
// all is okay
}
catch (NumberFormatException exc) {
// invalid number
txtDX.setText("10");
}
}
pc.setModified(true);
refreshButtons();
}
public void removeUpdate(DocumentEvent e) {
pc.setModified(true);
refreshButtons();
}
public void changedUpdate(DocumentEvent e) {
pc.setModified(true);
refreshButtons();
}
When I run the application and enter an invalid value in the txtDX
JTextField control, the application freezes.
What am I doing wrong? How should I implement this (and similar)
functionality?
Thanks,
Denis
The GURU guru
*New* GURPS tool now available at
http://www.io.com/~denis/GURU.htm
Technical Details
Message-ID:
<372e00f9.34307606@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,9e3dbd6ef4af1098,start X-Google-Attributes: gid111d45,public From: dsarrazin@cyberus.ca (Denis Sarrazin) Subject: JTextField mask? Date: 1999/05/03 Message-ID: <372e00f9.34307606@forums.borland.com> X-Deja-AN: 473625641 Content-Transfer-Encoding: 7bit Organization: Another Netscape Collabra Server User Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing