Skip to content

Instantly share code, notes, and snippets.

@ricemery
Created January 15, 2013 00:34
Show Gist options
  • Save ricemery/4534910 to your computer and use it in GitHub Desktop.
Save ricemery/4534910 to your computer and use it in GitHub Desktop.
Extends JavaFx TextField to limit input to positive and negative integers.
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IntegerTextField extends TextField {
private static final Logger logger = LoggerFactory.getLogger(IntegerTextField.class);
public IntegerTextField() {
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;
}
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
public int getInt() {
try {
return Integer.parseInt(getText());
}
catch (NumberFormatException e) {
logger.error("Error parsing int (" + getText() +") from field.", e);
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment