Skip to content

Instantly share code, notes, and snippets.

@abhinayagarwal
Last active August 29, 2015 13:57
Show Gist options
  • Save abhinayagarwal/9415872 to your computer and use it in GitHub Desktop.
Save abhinayagarwal/9415872 to your computer and use it in GitHub Desktop.
Focus Property of TextField
This is a small example doing what you want ! I have used the `focus property` of `textfield` to add and remove miles from it !
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextBinding extends Application {
@Override
public void start(Stage primaryStage) {
final TextField user = new TextField();
TextField demo = new TextField();
user.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
user.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
user.setText(user.getText().replace(" miles", ""));
}
else
{
user.setText(user.getText().concat(" miles"));
}
}
});
VBox root = new VBox();
root.getChildren().addAll(user,demo);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("transparent text test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
launch(args);
}
}
@abhinayagarwal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment