Browse Source

Fix detection of invalid values in FormattedTextField.

pull/198/head
weisj 4 years ago
parent
commit
101fc067f1
  1. 26
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkFormattedTextFieldUI.java
  2. 11
      core/src/test/java/ui/text/FormattedTextFieldDemo.java
  3. 6
      core/src/test/java/ui/text/TextFieldDemo.java

26
core/src/main/java/com/github/weisj/darklaf/ui/text/DarkFormattedTextFieldUI.java

@ -26,13 +26,15 @@ package com.github.weisj.darklaf.ui.text;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.text.Format;
import java.text.ParsePosition;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.text.Document;
import javax.swing.text.InternationalFormatter;
import com.github.weisj.darklaf.util.PropertyKey;
@ -86,12 +88,12 @@ public class DarkFormattedTextFieldUI extends DarkTextFieldUI implements Propert
@Override
public void insertUpdate(final DocumentEvent e) {
update();
SwingUtilities.invokeLater(this::update);
}
@Override
public void removeUpdate(final DocumentEvent e) {
update();
SwingUtilities.invokeLater(this::update);
}
@Override
@ -99,11 +101,19 @@ public class DarkFormattedTextFieldUI extends DarkTextFieldUI implements Propert
protected void update() {
if (textField == null) return;
try {
textField.getFormatter().stringToValue(textField.getText());
textField.putClientProperty("JTextComponent.hasError", false);
} catch (ParseException e) {
textField.putClientProperty("JTextComponent.hasError", true);
textField.putClientProperty(DarkTextUI.KEY_HAS_ERROR, !isInputValid(textField));
}
protected boolean isInputValid(final JFormattedTextField textField) {
String value = textField.getText();
JFormattedTextField.AbstractFormatter formatter = textField.getFormatter();
if (formatter instanceof InternationalFormatter) {
Format format = ((InternationalFormatter) formatter).getFormat();
ParsePosition position = new ParsePosition(0);
format.parseObject(value, position);
return position.getIndex() == value.length();
} else {
return textField.isEditValid();
}
}
}

11
core/src/test/java/ui/text/FormattedTextFieldDemo.java

@ -24,6 +24,8 @@
*/
package ui.text;
import java.text.NumberFormat;
import javax.swing.*;
import ui.ComponentDemo;
@ -36,7 +38,14 @@ public class FormattedTextFieldDemo extends TextFieldDemo {
@Override
protected JTextField createTextField() {
return new JFormattedTextField("Demo FormattedTextField");
JFormattedTextField textField = new JFormattedTextField(NumberFormat.getIntegerInstance());
textField.setValue(1234);
return textField;
}
@Override
protected String getDefaultText() {
return "Only accepts numbers";
}
@Override

6
core/src/test/java/ui/text/TextFieldDemo.java

@ -45,7 +45,7 @@ public class TextFieldDemo implements ComponentDemo {
@Override
public JComponent createComponent() {
JTextField textField = createTextField();
textField.putClientProperty(DarkTextUI.KEY_DEFAULT_TEXT, "Default Text");
textField.putClientProperty(DarkTextUI.KEY_DEFAULT_TEXT, getDefaultText());
// textField.setFont(new Font("Roboto", Font.PLAIN, 13));
DemoPanel panel = new DemoPanel(textField);
@ -104,6 +104,10 @@ public class TextFieldDemo implements ComponentDemo {
return panel;
}
protected String getDefaultText() {
return "Default Text";
}
protected JTextField createTextField() {
return new JTextField("Demo TextField");
}

Loading…
Cancel
Save