Reputation: 445
I am trying to build a task manager. I am trying to let the user edit a task, and the program will respond to this command without the user pressing the enter button.
For example, if I have a list of tasks:
If the user types "edit 2" in the text field, I would like the program to append the content of the 2nd task at the back of the input without having to press the enter button i.e. the text field should change to edit 2 good day
. Then the user can modify the content.
Is this possible?
If yes, what are the necessary things I need to learn?
Upvotes: 1
Views: 1509
Reputation: 36742
You can get this done using the textProperty()
of a TextField
and playing around with it.
I have created a demo for you :
INPUT
edit 1
OUTPUT
edit 1 go to school
CODE
import java.util.HashMap;
import java.util.Map;
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.Pane;
import javafx.stage.Stage;
public class TextFieldAutoAppend extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Map<String, String> mapOfTasks = new HashMap<String, String>();
mapOfTasks.put("1", "go to school");
mapOfTasks.put("2", "good day");
Pane pane = new Pane();
TextField textField = new TextField();
pane.getChildren().add(textField);
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
String[] subStrings = newValue.split(" ");
if(subStrings.length ==2){
if(subStrings[0].equalsIgnoreCase("edit") && mapOfTasks.keySet().contains(subStrings[1])){
textField.setText(newValue + " " + mapOfTasks.get(subStrings[1]));
}
}
}
});
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1