My idea: i wanted to have a textfield where a user can enter commands. All his inputs should be saved inside a list as a kind of history that the user can scroll through them with his arrowkeys.
My problem: i debugged my code multiple times, the commands are getting added to the history (textarea) and to the list propperly but inside my event handler all references to my textarea, textfield and list are always null.
My code:
public class Controller extends Application implements Initializable {
@FXML
private TextField commandInput;
@FXML
private TextArea commandHistory;
private LinkedList<String> commandList = new LinkedList<>();
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));
primaryStage.setTitle("Airport");
Scene scene = new Scene(root, 700, 275);
primaryStage.setScene(scene);
primaryStage.show();
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (!commandList.isEmpty()) {
switch (event.getCode()) {
case UP:
commandInput.setText(commandList.getFirst());
commandList.addLast(commandList.getFirst());
commandList.removeFirst();
break;
case DOWN:
commandInput.setText(commandList.getLast());
commandList.addFirst(commandList.getLast());
commandList.removeLast();
break;
}
}
System.out.println("Pressed: " + event.getCode());
});
}
private boolean parseString(String input) {
//TODO: Write Parser
return true;
}
public void enterPressed(ActionEvent actionEvent) {
String input = commandInput.getText();
if (parseString(input)) {
addCommandToHistory(input);
commandList.add(input);
System.out.println(commandList.getFirst());
commandInput.setText("");
} else {
addCommandToHistory("input is no command");
}