• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextField's text property not being set?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Trevor Pereira
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic