Results 1 to 8 of 8
  1. #1
    Victoria is offline Member
    Join Date
    Dec 2011
    Location
    Lund, Sweden
    Posts
    3
    Rep Power
    0

    Default Old value reappears in JFormattedTextField

    Hello!

    I'm writing a sudoku solving program for school but run into a bug.

    I've got 81 JFormattedTextFields in in a panel in a frame and two buttons for solving and clearing the window.
    But when I have typed in numbers, solved the sudoku, cleared and click in the fields I typed in, the value reappears. When the "clear"-button is pressed it should be like starting the program from fresh.

    When I clear the window, the program sets every textfield to null, and the matrix where the sudoku-numbers are stored in while the program solve the sudoku is filled with 0 when the "clear"-button is pressed. The window shouldn't be updated if not any of the buttons are pressed. Therefore I suspect the JFormattedTextField.

    Could it be that the field "remembers" values that have been typed in in the past? And why do they reappear when you click in the field?

    I do not know much about Swing and do not understand everything when I try to read at Java Platform SE 6
    so I humbly ask for help :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Old value reappears in JFormattedTextField

    Could it be that the field "remembers" values that have been typed in in the past? And why do they reappear when you click in the field?
    Post a SSCCE that illustrates the problem. It doesn't need all the program logic: just a JFormattedTextField and a clear button that reproduces the problem.

    (Part of the value of constructing a SSCCE is that it addresses the questions you pose. If the problem goes away when you eliminate other aspects of the code, that lets JFormattedTextField off the hook. The other part of the value is if the problem remains: because then we have something specific to address.)

  3. #3
    Victoria is offline Member
    Join Date
    Dec 2011
    Location
    Lund, Sweden
    Posts
    3
    Rep Power
    0

    Default Re: Old value reappears in JFormattedTextField

    Thank you for the input!

    Below is my SSCCE, I hope. It compiled and ran when I checked it.

    It is a frame with 81 textfields that you can fill with one digit each and then clear. When you have done that, the digit still reappears if you click in the textfield you typed in earlier and then click in an other field. This puzzels me. I don't want that to happen.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JFormattedTextField;
    import javax.swing.text.MaskFormatter;
    
    public class Gui2 extends JFrame{
    	private JPanel textFieldPanel2 = new JPanel(new GridLayout(9, 9));
    	private JPanel southPanel2 = new JPanel();
    	private JFormattedTextField[][] textFields = new JFormattedTextField[9][9];	
    
    	public static void main(String[] args){
    		Gui2 frame = new Gui2();
    		frame.setSize(500, 500);
    	}
    	public Gui2(){
    		super("Super Sudoku Solver!"); 
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		add(textFieldPanel2, BorderLayout.CENTER);
    		add(southPanel2, BorderLayout.SOUTH);
    		
    		createTextField(); //creates the 81 textfields
    
    		Clear clear = new Clear(this);
    		southPanel2.add(clear);
    
    		setVisible(true);
    	}
    	protected MaskFormatter createFormatter(String s) { // makes sure that the user only can insert one digit in a textfield
    		MaskFormatter formatter = null;
    		try {
    			formatter = new MaskFormatter(s);
    		} catch (java.text.ParseException exc) {
    			String message = "Write only one digit!";
    			JOptionPane.showMessageDialog(null, message);
    		}
    		return formatter;
    	}
    	
    	private void createTextField() {
    		for(int row = 0; row < 9; row++) {
    			for(int col = 0; col < 9; col++) {
    				JFormattedTextField textField = new JFormattedTextField(createFormatter("#"));				
    				textField.setHorizontalAlignment(JTextField.CENTER); 
    				textFields[row][col] = textField;				
    				textFieldPanel2.add(textFields[row][col]);
    			}
    		}
    	}
    	public void clearUpdate(){  //should empty all textfield
    		for(int row = 0; row < 9; row++) {
    			for(int col = 0; col < 9; col++) {
    				textFields[row][col].setText(null);
    			}
    		}
    	}
    }
    
    This is the Clear-button:
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    
    public class Clear extends JButton implements ActionListener {
    	private Gui2 graphic;
    	
    	public Clear(Gui2 graphic) {
    		super("Clear");
    		this.graphic = graphic;
    		addActionListener(this);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		graphic.clearUpdate();
    	}
    }
    Last edited by pbrockway2; 12-12-2011 at 12:17 AM. Reason: code tags added

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Old value reappears in JFormattedTextField

    I've added "code" tags. If you put [code] at the start of the code and [/code] at the end the formatting will be preserved.

    -----

    you can fill with one digit each and then clear. ... the digit still reappears if you click in the textfield you typed in earlier and then click in an other field.
    I'm no expert, but according to the JFormattedTextField API docs the default behaviour of these things when the focus is lost is COMMIT_OR_REVERT: ie commit the currently displayed text if it is legal, otherwise revert the displayed text to match whatever the value is.

    What you are seeing (the "memory") is the value of the field. As How To Use Formatted Text Fields in Oracle's Tutorial points out "A formatted text field's text and its value are two different properties, and the value often lags behind the text." The reason the text gets reverted is because when you click on a text field its text is " " (mind the space!) but that isn't valid so when you click on another the first one's text changes to the old value.

    Another gem from that Tutorial page: "Note that although the JFormattedTextField class inherits the setText method from the JTextField class, you do not usually call the setText method on a formatted text field. If you do, the field's display changes accordingly but the value is not updated (unless the field's formatter updates it constantly)." Again related to what you are seeing because you call setText(null) which removes the displayed text but leaves the field's value unchanged (to come back and haunt you).

    So what happens if you use setValue() instead of setText()? If you're like me you'd be wondering at this point "set the value to what?" since the point of clearing the fields was to remove their values. Well, those fields had a value when the program started (a default value) and that would be a good thing to reset them to. (See setValue().)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Old value reappears in JFormattedTextField

    Myself, I'd use a plain JTextField and would set the DocumentFilter of the Document held by it, one that would allow either only the 9 number Strings (not allowing 0) or "" as valid text for the document. Perhaps hold the allowed text in a HashSet<String> and in the 3 filter methods, construct a String that holds what the user is trying to make the document to be, test this against the HashSet, and if OK, call the DocumentFilter's super method, and if not, either display a warning or simply ignore the input (don't call the super method).

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Old value reappears in JFormattedTextField

    I agree with Fubarable, I'd be looking at a DocumentFilter. First because your code doesn't really use the text vs value distinction that lies at the heart of JFormattedTextField. And secondly because the filter could be quite clever and disallow entries based on the currently entered values in the rest of a field's row/column/block.

    Also posting because I realised that I forgot to say that you seem to have misunderstood the exception thrown by the MaskFormatter constructor: this is really reporting a logic error on your part ("Hey programmer, the mask string was bad!") not a user input error ("Hey user, what you tried to type here was bad!").

  7. #7
    Victoria is offline Member
    Join Date
    Dec 2011
    Location
    Lund, Sweden
    Posts
    3
    Rep Power
    0

    Default Re: Old value reappears in JFormattedTextField

    Thank you for your help! Your explanations of what actually says in the tutorials helped a lot.

    I changed setText() to setValue(), and now it works! :D
    The DocumentFilter sounds interesting, though I think the teacher wants us to write code to check the inserted values ourself, but I will definitely try it myself.

    I wouldn't say I understand the MaskFormatter either, so thanks for pointing that out. I.e the exception!
    (Wondered why I had to have a try catch at all if values always correct.)

    Yes, the "code" tags makes it more readable. Remember that to next time :)

    I really appreciate that you took time to help me, I've learned a lot!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Old value reappears in JFormattedTextField

    You're welcome.

Similar Threads

  1. JFormattedTextField and MaskFormatter
    By javaw in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2010, 07:42 PM
  2. add jformattedTextfield in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2009, 06:42 PM
  3. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 11:53 AM
  4. Problems with JFormattedTextField
    By Gatts79 in forum New To Java
    Replies: 0
    Last Post: 10-03-2008, 05:59 PM
  5. JFormattedTextField Issue
    By teracomp in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •