File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextField's text property not being set? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField Watch "JTextField New topic

JTextField's text property not being set?

Trevor Pereira

Joined: Sep 11, 2002
Posts: 12
Hi all,
I have a JTextField on a JPanel and i am trying to pass a value from an element in an ArrayList to a JTextField using it's "setText(String str)" method. Using the debugger I have confirmed that a value does exist in the element of the ArrayList and I have called "toString()" on that element in order to convert it back to a "String" value (as ArrayList elements are stored as generic "Objects").
Here is my Code:
//Simple text to see if the value has been passed to the textfield...
//test *****************
System.out.println("Vehicle Plate Expire: ");
Nothing is printed to the console except...
Vehicle Plate Expire:
Wierd thing is...some textfield work while others do not?
Help anyone?
Thanks in advance
Vehicle Plate Expire:
Joe Ess

Joined: Oct 29, 2001
Posts: 8710

I can assure you that JTextField.setText() works. We can't see enough of your code to know what's going on, but I'd wager that either getJtxtYearPlateExpires() doesn't do what you think it does (i.e. it creates a new instance of the JTextField overwriting the old one) or another one of your methods overwrites the JTextField instance or it's text value. Try using your debugger to put a watch on the instance and the setText() method to catch the side effects.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Trevor Pereira

Joined: Sep 11, 2002
Posts: 12
Hi Joe,
Thanks for your advice.
Don't know why this would NumericOnly Utility class that I attached to my JTextField's document property was causing the problem?
Here is the code to my document class...
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
* Stolen directly from Suns' generaltext.html
* Swing tutorial page.
* @author Someone at Sun
public class NumericStyledDocument extends DefaultStyledDocument {
int maxCharacters;
public NumericStyledDocument(int maxChars) {
maxCharacters = maxChars;
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
//System.out.println("LSD string *" + str + "*");
if (str.equals("") || str.equals(" "))
//This rejects the entire insertion if it would make
//the contents too long. Another option would be
//to truncate the inserted string so the contents
//would be exactly maxCharacters in length.
if ((getLength() + str.length()) <= maxCharacters){
// ====================================
if (str.equals("0") || str.equals("1")
|| str.equals("2") || str.equals("3")
|| str.equals("4") || str.equals("5")
|| str.equals("6") || str.equals("7")
|| str.equals("8") || str.equals("9"))
//|| str.equals(" "))
super.insertString(offs, str, a);
// else
//System.out.println(" else LSD string *" + str + "*");
// JOptionPane.showMessageDialog(null, "Numeric only allowed", "Bad Data", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "No more than " + maxCharacters + " characters allowed", "Maximum Exceeded", JOptionPane.ERROR_MESSAGE);
}//end class
//Finish code sample...
is it because I am inserting a whole date number rather than one number at a time, so the number, say "2003", does not pass the check....I think so.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Yes, that would be the reason why. The insertString() method assumes that only one character will be entered at a time. Add a loop in there to test each character and it should work.

If you're using JDK 1.4.x you could also use JFormattedTextField and not have to mess with the Document stuff yourself.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: JTextField's text property not being set?
Similar Threads
Regarding JTextField
regarding JTable issue
JDOM - need help ... simple problem driving me insane
String to integer??