Created
July 21, 2014 16:30
-
-
Save mansueli/2459466c3be938a52e31 to your computer and use it in GitHub Desktop.
how to mark a portion of a TextField as readonly in javafx? (http://stackoverflow.com/questions/24864133/how-to-mark-a-portion-of-a-textfield-as-readonly-in-javafx)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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