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.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.text.ParseException; import java.text.Format;
import java.text.ParsePosition;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.text.Document; import javax.swing.text.Document;
import javax.swing.text.InternationalFormatter;
import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyKey;
@ -86,12 +88,12 @@ public class DarkFormattedTextFieldUI extends DarkTextFieldUI implements Propert
@Override @Override
public void insertUpdate(final DocumentEvent e) { public void insertUpdate(final DocumentEvent e) {
update(); SwingUtilities.invokeLater(this::update);
} }
@Override @Override
public void removeUpdate(final DocumentEvent e) { public void removeUpdate(final DocumentEvent e) {
update(); SwingUtilities.invokeLater(this::update);
} }
@Override @Override
@ -99,11 +101,19 @@ public class DarkFormattedTextFieldUI extends DarkTextFieldUI implements Propert
protected void update() { protected void update() {
if (textField == null) return; if (textField == null) return;
try { textField.putClientProperty(DarkTextUI.KEY_HAS_ERROR, !isInputValid(textField));
textField.getFormatter().stringToValue(textField.getText()); }
textField.putClientProperty("JTextComponent.hasError", false);
} catch (ParseException e) { protected boolean isInputValid(final JFormattedTextField textField) {
textField.putClientProperty("JTextComponent.hasError", true); 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; package ui.text;
import java.text.NumberFormat;
import javax.swing.*; import javax.swing.*;
import ui.ComponentDemo; import ui.ComponentDemo;
@ -36,7 +38,14 @@ public class FormattedTextFieldDemo extends TextFieldDemo {
@Override @Override
protected JTextField createTextField() { 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 @Override

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

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

Loading…
Cancel
Save