When I enter keyboard input into the JFormattedTextField object, the input
is not being retained when the field has lost focus.
I am using SDK/JRE 1.4.0, Windows XP Professional.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.text.MaskFormatter;
import java.text.ParseException;
public class TestFrame extends JFrame
{
public JFormattedTextField low;
public JFormattedTextField high;
public JFormattedTextField incr;
public TestFrame() {
JPanel panel = new JPanel(new BorderLayout());
// add selection panel
MaskFormatter mf1 = null;
MaskFormatter mf2 = null;
try
{
mf1 = new MaskFormatter("####");
mf1.setPlaceholderCharacter('_');
mf2 = new MaskFormatter("###");
mf2.setPlaceholderCharacter('_');
}
catch (ParseException e){
}
low = new JFormattedTextField(mf1);
high = new JFormattedTextField(mf1);
incr = new JFormattedTextField(mf2);
JPanel select = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
addgbc(select, new JLabel("Low Temperature Range . . ."),gbc,0,0);
addgbc(select, low,gbc,1,0);
addgbc(select, new JLabel("High Temperature Range . ."),gbc,0,1);
addgbc(select, high,gbc,1,1);
addgbc(select, new JLabel("Increment Degrees . . . . ."),gbc,0,2);
addgbc(select, incr,gbc,1,2);
panel.add(select, BorderLayout.NORTH);
JFrame frame = new JFrame("Test JFormattedTextField");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
private void addgbc(Container cont, JComponent comp, GridBagConstraints
gbc, int x, int y) {
gbc.gridx=x; gbc.gridy=y;
cont.add(comp,gbc);
}
public static void main (String[] args) {
new TestFrame();
}
}
Gerard H. Pille - 29 May 2004 20:41 GMT
> When I enter keyboard input into the JFormattedTextField object, the input
> is not being retained when the field has lost focus.
[quoted text clipped - 62 lines]
>
> }
The input IS being retained in your example.
That is, when the input conforms to the mast.
Try providing four digits in the first two fields, input will be
retained. The default behaviour of JFormattedTextField kicks in when
you provide less digits then required by the mask: COMMIT_OR_REVERT.
It reverts.
Maybe you want different masks? Or a numberformat iso. a maskformat?
Geert