Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems with MaskFormatter and JFormattedTextfield

843805Mar 8 2007 — edited Mar 26 2007
Hi,
I'm new to the forum and Java programming so if anyone is willing to answer this query with a small degree of patience I would be immensely, humbly grateful!

I'll say firstly that I wrote the program in jdk1.4.2 then recompiled it in 1.6.0 in the vain hope that the problem would go a way, but no such luck.

Right, I'm using one Formatted textfield with a MaskFormatter on which I change the mask according to what information I want from the user:
--------------------------------------------------------------------
class myFormatter extends MaskFormatter {
		
		String key;
		
		public void setMask(String k){
			
			key = k;	
		
			if (key.equals("text")) {
				try{
					super.setMask("*************"); // for text, eg star names
					super.setValidCharacters("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\u0020");
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
			}
				
			else if (key.equals("num")) { 
				try{
				super.setMask("****#"); // for numbers 1-10000 eg frame num
                                super.setValidCharacters("0123456789\u0020");
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
			}
				
			else if (key.equals("epoch")) try{
				super.setMask("####.#"); // for epoch
				super.setValueContainsLiteralCharacters(true);
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
				
			
			else if (key.equals("yna")) try{
				super.setMask("L"); // for single lower case characters eg y/n/a
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}	
			
			else if (key.equals("coord")) try{
				super.setMask("*## ## ##"); // for RA/Dec
				super.setValueContainsLiteralCharacters(true);
				super.setValidCharacters("0123456789+-\u0020");
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
			
			else if (key.equals("reset")) try{
				super.setMask("*********************"); // accept anything
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
				
			else  try{
				super.setMask("********************"); // accept anything
				}catch (java.text.ParseException e) {
				System.err.println("Problem with formatting: " + e.getMessage());
				System.exit(-1);
				}
		}
	}
-----------------------------------------------------------------------------------------------------
Ok, now I've only gotten as far as checking the implementation of the "epoch", "text", "yna" and "coord" keys. I've discovered two main problems:
1. The A, ? and H masks in MaskFormatter just simply did not work; the textfield would not let me enter anything, even when setting the valid the characters, hence having to use * for implementing the "text" key.
But most importantly:
2. The "coord" mask will not let me enter anything (example coordinates -32 44 55) and when I try (in particular press the backspace to start entering at the beginning of the ttextfield instead of the middle, where the cursor is put) I presented with this horrendous complaint from the compiler:

-------------------------------------------------------------------------------------------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.MaskFormatter.isLiteral(MaskFormatter.java:566)
at javax.swing.text.MaskFormatter.canReplace(MaskFormatter.java:711)
at javax.swing.text.DefaultFormatter.replace(DefaultFormatter.java:560)
at javax.swing.text.DefaultFormatter.replace(DefaultFormatter.java:533)
at javax.swing.text.DefaultFormatter$DefaultDocumentFilter.remove(DefaultFormatter.java:711)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:573)
at javax.swing.text.DefaultEditorKit$DeletePrevCharAction.actionPerformed(DefaultEditorKit.java:1045)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2879)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)
at java.awt.Component.processEvent(Component.java:5815)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
at java.awt.Component.dispatchEventImpl(Component.java:4282)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
-----------------------------------------------------------------------------------------------------------------

A NullPointerException??? I even tried priming the textfield by setting the value using formatTxt.setValue(format.stringToValue("+00 00 00")).

Does anyone have any ideas on this error (or how I can get around this without delving to much into other filtering classes)? Has anyone had similar problems with the MaskFormatter masks not accepting what they claim they do (I looked through the posts on the subject).

Thanking you in advance for you patience........
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2007
Added on Mar 8 2007
5 comments
1,080 views