Skip to content

Instantly share code, notes, and snippets.

@ricemery
Created January 15, 2013 00:31
Show Gist options
  • Save ricemery/4534892 to your computer and use it in GitHub Desktop.
Save ricemery/4534892 to your computer and use it in GitHub Desktop.
JavaFx TextField subclass that limits input to positive and negative double values.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DoubleTextField extends TextField {
private static final Logger logger = LoggerFactory.getLogger(DoubleTextField.class);
public DoubleTextField() {
super();
addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (!isValid(getText())) {
event.consume();
}
}
});
textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue,
String oldValue, String newValue) {
if(!isValid(newValue)) {
setText(oldValue);
}
}
});
}
private boolean isValid(final String value) {
if (value.length() == 0 || value.equals("-")) {
return true;
}
if (StringUtils.countMatches(value, ".") > 1) {
return false;
} if (value.endsWith(".")) {
return true;
}
try {
Double.parseDouble(value);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
public double getDouble() {
try {
return Double.parseDouble(getText());
}
catch (NumberFormatException e) {
logger.error("Error parsing double (" + getText() +") from field.", e);
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment