Skip to content

Instantly share code, notes, and snippets.

@mansueli
Created July 21, 2014 16:30
Show Gist options
  • Save mansueli/2459466c3be938a52e31 to your computer and use it in GitHub Desktop.
Save mansueli/2459466c3be938a52e31 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
TagField tf = new TagField("find","nemo");
Pane root = new Pane();
root.getChildren().add(tf);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){launch(args); }
}
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
public class TagField extends TextField{
String initTag;
String closeTag;
public TagField(String tag,String text){
this.initTag = "<" + tag + ">";
this.closeTag = "</" + tag + ">";
setTagText(initTag + text + closeTag);
super.textProperty().addListener(new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if(!newValue.contains(initTag)||!newValue.contains(closeTag)){
setTagText(oldValue);
}
}
});
}
public void setTagText(String text){
super.setText(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment