• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField  RSS feed

 
Paul Wright
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to get a formatted textfield working. I need it to only accept a single digit from 1 to 9 or be empty. Its for a Sudoku program.

It is working fine but will only let me enter numbers in a certain order.
Sudoku is a 9x9 grid of textboxes. This is made up of 9 3x3 subgrids. It wont let me just place a number in the grid at random. I have to find the one that is editable then its fine from there when what I want is for all of them to be editable.

Im using a MaskFormatter and its working fine in that it only allows me to enter a single digit when it allows me to enter anything at all.

Is there an easier way to get a textfield to only accept one digit from 1 to 9 or blank?

Any help would be much appreciated.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to work OK like this

 
Paul Wright
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx heaps but im still getting an error when the solver extracts the numbers from the grid.
Ive set up my grid different from yours.


The problem im getting is you can still enter more than one number in certain grids and not others. If you enter a number then delete it when you click out of the grid it comes back. The solver is finding more than one number in the grid as well.
I am at a loss as to how to fix this.

Anyway its doing my head in and I know the code ive given isnt much help.
How do I navigate around the grid using the arrow keys? Im not sure how to set up the key listener to get focus from one textfield to another.
[ October 22, 2005: Message edited by: Paul Wright ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does seem to work OK with me, java 1.5.0_03

here's a different version, using just a JTextField and its Document to filter the input
see if you have the same problems with this

 
Paul Wright
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude your a legend. Wrote it up and compiled ok and runs sweet.

If your ever in Perth, Western Australia I will buy you a beer.

Thanx heaps.

PS. I sorted out moving the cursor around the grid easy enough. I just used:



Or something of thereabouts.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad that Dunn's subclass of PlainDocument works
for you, but I'm curious about your original problem
with JFormattedTextField. I don't quite understand
what was going wrong.
 
Paul Wright
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either do I actually. What I will do is next week sometiime I will stick the entire source code up on my webpage and you can compile it and pull it to bits. Or play it.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was I don't understand
your description of the unexpected
behaviour you were experiencing.

Im using a MaskFormatter and its working fine in that it only allows me to enter a single digit when it allows me to enter anything at all.


huh? Is it "working fine" or not?
Presumably not, so how not. What
are you hoping for and what was
your JFormattedTextField doing
instead?
 
Alejandro Barrero
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextFormattedField is not easy to use. An easy solution is to use a class that extends javax.swing.InputVerifier:
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class DigitVerifier extends InputVerifier {
public boolean verify(JComponent input) {
if (input instanceof JTextField) {
JTextField textfield = (JTextField)input;
String text = textfield.getText();
String message = "";
if (text.length() == 0) // Accept empty strings.
return true;
try {
if (text.length() > 1)
message += "Must be a single digit.\n";
int value = Integer.parseInt(textfield.getText());
} catch (NumberFormatException e) {
message += text + "is not an integer.\n";
}
if (message.length() > 0) {
JOptionPane.showMessageDialog(null, message, "INVALID INPUT", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
return true;
}
}

You set it in your text field as
myTextField.setInputVerifier(new DigitVerifier);
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!