1

I have been trying to figure this out for a while now but can't quite get it to work. Essentially I have a JavaFX TextArea and I want to construct a new Object named CommandWrapper with the last line of input (ie. line above caret after the ENTER key is pressed). Whenever I hit ENTER after typing a command it works flawlessly but for some reason my String.split() function wont get the empty line if I enter no command ash shown in the GIF below:

https://gyazo.com/49ebd82be02fc271eeb7a879b194c63c

Here is the code concerning the issue:

package com.mswordhf.jnet.java.contollers;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import com.mswordhf.jnet.java.models.JnetModel;
import com.mswordhf.jnet.java.modules.CommandWrapper;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;

public class CmdController implements Initializable {

private JnetModel model;
private int clientIndex;

@FXML private TextArea commandTextArea;

public CmdController(JnetModel model, int clientIndex) {
    this.model = model;
    this.clientIndex = clientIndex;
}

@Override
public void initialize(URL url, ResourceBundle rb) {

    commandTextArea.setOnKeyPressed(keyEvent -> {

        if(keyEvent.getCode() == KeyCode.ENTER) {

            List<String> lines = Arrays.asList(commandTextArea.getText().split("\\n"));
            String command = lines.get(lines.size() - 1);

            System.out.println(command);

            if(command == "\n") {
                System.out.println("Worked");
            }else {
                CommandWrapper wrapper = new CommandWrapper(command);
                model.getClients().get(clientIndex).getHandle().sendModule(wrapper);

                if(!model.getCmdOutput.isRunning()) {
                    model.getCmdOutput.reset();
                    model.getCmdOutput.start();
                }
            }

        }

    });

    model.getCmdOutput.setOnSucceeded(event -> {

        for(String line : model.getCmdOutput.getValue()) {
            commandTextArea.appendText(line + "\n");
        }

        model.clearList();

    });

}

}
2
  • == checks for primitive (int, char, double, ...) equality, use .equals(...) to check for object (String, Object, ...) equality. Also String#split does not include the regex that the String was split with in the resulting array. Meaning that if(command.equals("\\n")) will always be false, perhaps you should check for the empty String, if(command.equals("")). Commented Dec 9, 2016 at 23:34
  • @JonnyHenly Thanks for the comment and you're right I should've been using equals() but the problem remains. I believe the last empty line isn't being picked up by 'String#split()' because the 'println(command)' always prints the line 2 up from the caret after pressing ENTER. Commented Dec 10, 2016 at 0:24

1 Answer 1

1

I'm really uncertain as to why, but using:

commandTextArea.setOnKeyReleased(keyevent -> {
    //code...
}

works totally as intended.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm puzzled why releasing a key, instead of pressing it, would produce the desired behaviour, but if it works it works.
Oh perhaps the key pressed event is fired before the \n is appended to the commandTextArea's text. That's the only reason I can think of. Nice job figuring this out, +1.
Yeah that's what I've figured must be the case, thanks for helping me out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.