JFormattedTextField:Allow Only numeric digits and asteriks
Hello friends,
I'm using JFormattedTextField to cater my needs wherein I should accept only "numbers or asteriks" and that too the maximum number of characters that I should receive is 20.
I've tried to achieve this, without much success though, with the following piece of code:
myFormatter = new MaskFormatter();
myFormatter.setValidCharacters("*1234567890");
myFormatter.setMask("********************");
myFormatter.setAllowsInvalid(false);
myTxtField = new JFormattedTextField(myFormatter);
myTxtField.setFocusLostBehavio(JFormattedTextField.PERSIST);
The above code is working fine in the sense:
-- it's not allowing more than 20 characters
-- it's not allowing user to type in anything other than numbers and stars
Problem:
When the application loads, this text field is having 20 spaces, this is where the problem is actually stemming from!
how to get to the problem?
1. type "123" in the "myTxtField"
2. move the cursor 1 field to the right using the right arrow
3. type the numbers "*4*5*" now
the problem with this is that -- here the text field is allowing numbers, astericks, and also spaces
but I don't want to have these spaces at all...
so, may I ask you to help me out please?
Thanks,
Srinivas
# 4
Then you don't want to use a MaskFormatter. Part of the definition of the
definition of the MaskFormatter is that the mask is present in the text.
For instance, if your mask was "***-**-****", the hyphens would be present
in the text. Note that you are not typing the spaces (you can't), you are merely
moving the cursor over the mask.
With a JFormattedTextField, you should be doing a getValue(), rather than
getText(). The value, rather than the text, is what will match the mask (note,
though, that any mask characters (such as the hyphens in the above
example) will still be present in the value).
You also need to use COMMIT rather than PERSIST as otherwise nothing
happens when the field loses focus -- the value is never updated. If you want
to allow up to 20 characters, you can use the [url http://forum.java.sun.com/thread.jspa?forumID=57&threadID=461577&start=3]VariableLengthMaskFormatter.[/url]
You could also use a DocumentFilter on a regular JTextField to accomplish
what you want.
# 5
Here is a piece of code, that depicts my problem:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class jfttDemo extends JPanel
implements PropertyChangeListener {
//Values for the fields
private String textEntry = "10000";
//Labels to identify the fields
private JLabel textEntryLabel;
private JLabel textDisplayLabel;
//Strings for the labels
private static String textEntryString = "textEntry: ";
private static String textDisplayString = "textDisplay: ";
//Fields for data entry
private JFormattedTextField textEntryField;
private JFormattedTextField textDisplayField;
//Formats to format and parse numbers
private MaskFormatter textEntryFormat;
private MaskFormatter textDisplayFormat;
public jfttDemo() {
super(new BorderLayout());
setUpFormats();
String textDisplay = textEntry;
//Create the labels.
textEntryLabel = new JLabel(textEntryString);
textDisplayLabel = new JLabel(textDisplayString);
//Create the text fields and set them up.
textEntryField = new JFormattedTextField(textEntryFormat);
textEntryField.setValue(textEntry);
textEntryField.setColumns(25);
textEntryField.setFocusLostBehavior(JFormattedTextField.COMMIT);
textEntryField.addPropertyChangeListener("value", this);
textDisplayField = new JFormattedTextField(textDisplayFormat);
textDisplayField.setValue(textDisplay);
textDisplayField.setColumns(25);
textDisplayField.setEditable(false);
textDisplayField.setForeground(Color.red);
//Tell accessibility tools about label/textfield pairs.
textEntryLabel.setLabelFor(textEntryField);
textDisplayLabel.setLabelFor(textDisplayField);
//Lay out the labels in a panel.
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(textEntryLabel);
labelPane.add(textDisplayLabel);
//Layout the text fields in a panel.
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(textEntryField);
fieldPane.add(textDisplayField);
//Put the panels in this panel, labels on left,
//text fields on right.
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
}
/** Called when a field's "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
textEntry = textEntryField.getText().trim();
String textDisplay = textEntry;
textDisplayField.setValue(textDisplay);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FormattedTextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new jfttDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
//Create and set up number formats. These objects also
//parse numbers input by user.
private void setUpFormats() {
try
{
MaskFormatter myFormat = new MaskFormatter();
myFormat.setValidCharacters("*1234567890");
myFormat.setMask("********************");
myFormat.setAllowsInvalid(false);
textEntryFormat = myFormat;
textDisplayFormat = myFormat;
}
catch (ParseException pe)
{
// Do nothing
}
}
}
Please consider the following scenarios when we run the program:
scenario-1: enter the text "1*2*3*4*5*6*7*8*9*0*" in the first text box(text Entry field) and press Tab
This act will update the second text box to the above text.
scenario-2: (run the program afresh)enter the text "1*"
move the cursor towards right(using right arrow key) by 3 positions
and enter the text "3*"
now press Tab
the display Text Field doesn't get updated as the effective text returned by the text Entry box is "1*3* "
Now, what I want is -- <b>I don't want those spaces....</b>
(I'm sorry, I wanted to attach an image, but couldn't find an option to upload an image here)