Okay so i am having a little trouble with my JFormattedTextField. I figured out how to work the MaskFormatter somewhat but not great. the formatting is fine, like when I want to restrict a field to only 15 characters that works but when I move the cursor to another field the text disappears. Not sure whats going on there. I can't find anything about it in the API... although I am quite new at using it so I may simply be looking in the wrong areas. here is a snippit of code that contains the JFormattedTextField:

JFormattedTextField nameArea = new JFormattedTextField(createFormatter("AAAAAAAAAAAAAAA"));

	// creates a MaskFormatter and makes sure the formating is correct
	// taken from java.sun.com
	protected MaskFormatter createFormatter(String s) 
	{
		MaskFormatter formatter = null;
		try 
		{
			formatter = new MaskFormatter(s);
		} 
		catch (java.text.ParseException exc) 
		{
			System.err.println("formatter is bad: " + exc.getMessage());
			System.exit(-1);
		}
		return formatter;
	}

Recommended Answers

All 2 Replies

smoore,
Use setFocusLostBehavior method of JFormattedTextField.

Thanks a lot! Guess I overlooked that when in the API. I'm assuming I want JFormattedTextField.PERSIST to keep the text in the box? Also will that ensure that I can grab the text later if I want to read it? for example if I have a "NAME" field and I hit a button called "SAVE" and I want the action to grab the text from the field and put it into a variable, will that work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.