Jformattedtextfield Is Not Properly Cleared

JFormattedTextField is not properly cleared

Ok so now I found it, "One of the shortcomings of the mask formatter is that as of the current implementation (Java 5), it has no support for letting a user revert a field to the blank value (the initial value of the field prior to any user input) once they have left the field at any point."

So since I am using a MaskFormatter I cannot clear the field.

JTextField stops using MaskFormatter after being cleared

I can't tell you the exact reason but setText seems to drive your JFormattedTextField crazy because "" is a String and it is against the current mask.

Please try using setValue(null) instead.

I've just made sure that this method works. The next piece of code proves it:

public class Two extends JFrame {

public static void main(String[] args) throws Exception {
new Two().a();
}

void a() throws Exception {
this.setLayout(new GridLayout(2, 1));
MaskFormatter formatter = new MaskFormatter("#");
formatter.setValidCharacters("123456789");
final JFormattedTextField field = new JFormattedTextField(formatter);
JButton b = new JButton("null!");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
field.setValue(null);
}
});
this.add(field);
this.add(b);
this.setSize(100, 100);
this.setVisible(true);
}
}

After clicking the null! button formatter continues to work as it is supposed to work.

putting On Change listener on jFormattedTextField

register a PropertyChangeListener for the property "value" to the formattedField

    PropertyChangeListener l = new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
String text = evt.getNewValue() != null ? evt.getNewValue().toString() : "";
label.setText(evt.getNewValue());
}
};
formattedTextField.addPropertyChangeListener("value", l);

Do not use DocumentListener nor FocusListener: the former is notified too often (on every keytyped, before parsing happened) the latter is too brittle.

JTextField's setText method doesn't work from a KeyListener

This behavior is due to the fact that the KeyEvent will be processed by the field after your KeyListener was fired. You can circumvent it by consuming the event via

ke.consume();

inside your method keyTyped.

Depending on your requirements another way would be to encapsulate the clearing calls inside a SwingUtilities.invokeLater which will be processed after your current event and thus clear the field after it was updated.

how to get the index of two dimensional Jtextfield?

Please edit your question including your code in a formatted block, also consider to provide a Minimal, Complete, and Verifiable example.

Since you are creating a new layoutSudkou object (ls) every time focusGained method is invoked, the event source will never be a jtextfield of that particular instance (ls.jf [row][column]).

You can save your 2D array as an instance field of your class (the one where you create and add the textfields) and than check which textfield has gained the focus.

You should also take a look at Java Naming Conventions for your code (class names should not begin with a lowercase letter) and, assuming that jf is public field of layoutSudkou class, Why declare variables private in a class or similar questions.

Java - Check if JTextField is empty or not

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not.

Below is what I found from this thread.

yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
changed();
}
public void removeUpdate(DocumentEvent e) {
changed();
}
public void insertUpdate(DocumentEvent e) {
changed();
}

public void changed() {
if (yourJTextField.getText().equals("")){
loginButton.setEnabled(false);
}
else {
loginButton.setEnabled(true);
}

}
});


Related Topics



Leave a reply



Submit