This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField Watch "JTextField New topic
Author

JTextField's text property not being set?

Trevor Pereira
Greenhorn

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:
//************************************************************************
vehicleFinalPanel.getVehicleANDDriverMainPanelView1().
getVehicleANDDriverScrollingPanelView1().
getVehicleANDDriverCollectionPanelView1().
getVehicleInforamtionPanelView1().
getJtxtYearPlateExpires().setText(dbData.get(49).toString());
//************************************************************************
//Simple text to see if the value has been passed to the textfield...
//test *****************
System.out.println("Vehicle Plate Expire: ");
System.out.print(vehicleFinalPanel.getVehicleANDDriverMainPanelView1().
getVehicleANDDriverScrollingPanelView1().
getVehicleANDDriverCollectionPanelView1().
getVehicleInforamtionPanelView1().
getJtxtYearPlateExpires().getText());
//************************
Note:
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
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

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
Greenhorn

Joined: Sep 11, 2002
Posts: 12
Hi Joe,
Thanks for your advice.
Don't know why this would happen...my NumericOnly Utility class that I attached to my JTextField's document property was causing the problem?
Here is the code to my document class...
//Start
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(" "))
return;
//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);
}
else
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
Bartender

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField's text property not being set?