JavaFX: How to make enter key submit TextArea
Asked Answered
S

3

9

Sorry if this seems a little too easy, I'm brand new to JavaFX, this is my first little app built with it.

I am trying to make a bare bones chat client. I am using the JavaFX Scene builder to make the client UI, and a controller class connected to the FXML.

How can I make is so that the current text of in the text area is submitted to the server and the text area is cleared upon the enter key press, instead of using some kind of "send" button?

EDIT: Here is the code that is not working:

//...

public class FXMLDocumentController
{

//...

@FXML private TextArea messageBox;

//...

messageBox.setOnKeyPressed(new EventHandler<KeyEvent>() 
{
    @Override
    public void handle(KeyEvent keyEvent) 
    {
        if(keyEvent.getCode() == KeyCode.ENTER)
        {
            //sendMessage();
        }
    }
});

//...
Schema answered 11/8, 2014 at 21:15 Comment(0)
R
14

This should get you what you want:

TextArea area;
//... (initialize all your JavaFX objects here...)

// wherever you assign event handlers...
area.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.ENTER)  {
            String text = area.getText();

            // do your thing...

            // clear text
            area.setText("");
        }
    }
});

I might add, that if you are so inclined to provide both a button and an enter key event, you could tie the event handler functions of both controls to a single common function in a way such as this:

Button sendButton;
TextArea area;
// init...

// set handlers
sendButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
         sendFunction();
    }
});

area.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.ENTER)  {
             sendFunction();
        }
    }
});

// define send function
public void sendFunction() {
    String text = this.area.getText();

    // do the send stuff

    // clear text (you may or may not want to do this here)
    this.area.setText("");
}

Either way works, good luck.

Rounding answered 11/8, 2014 at 21:20 Comment(11)
Get the text using area.getText() and clear using area.setText(""); presumably. See my edit and see if it works for youRounding
What I've provided is the building blocks to get you what you need, I can't write the exact implementation for you, that's for you to do.Rounding
Sorry, I understand that, what I was saying is that that doesn't seems to be the complete code to write an event handler. I'm only familiar with writing actionlisteners. I can handle the details of my send() function, I'm just confused on how to set up the event handler. My compiler whines that "package messageBox does not exist"Schema
You're not an idiot, you're learning. Good luck going forward.Rounding
Okay, yeah. I have code that is just like yours, (with my variables of course.) the compiler is whining that package messageBox(my text area variable) does not exist.Schema
Maybe post what you have then? That's valid code, so you probably have something else going on.Rounding
Your problem is that code (everything aside from the variable declaration) needs to be inside a method, you've got it at the class level. My example probably wasn't the best to show that you need to declare the variable and put the code in the appropriate place (ie: global variable, initialized in a method).Rounding
What, like the initialize method?Schema
Yes, the initialize method would be a good place, or you can create your own that is called from the initialize method.Rounding
Thanks for the help, was getting really frustrated!Schema
Not to revive an old post, but I should mention for this functionality to work as intended, I must also consume the key event to keep it from adding a new line to the message box.Schema
S
10

You can use lambda expressions also ... I think it is more elegant and simply

textArea.setOnKeyPressed(event -> {
   if(event.getCode() == KeyCode.ENTER){
     //type here what you want
   }
}); 
Shyamal answered 7/4, 2016 at 19:58 Comment(0)
G
3

In addition to the other answers, I think it might be useful in some applications to not actually invoke the send function if the user pressed SHIFT+ENTER. In that case he/she maybe actually wanted a new line.

textArea.setOnKeyPressed(event -> {
    if (event.getCode() == KeyCode.ENTER) {
        event.consume(); // otherwise a new line will be added to the textArea after the sendFunction() call
        if (event.isShiftDown()) {
            textArea.appendText(System.getProperty("line.separator"));
        } else {
            sendFunction();
        }
    }
});

If you don't want to send empty messages you can do something like this:

textArea.setOnKeyPressed(event -> {
    if (event.getCode() == KeyCode.ENTER) {
        event.consume();
        if (event.isShiftDown()) {
            textArea.appendText(System.getProperty("line.separator"));
        } else {
            if(!textArea.getText().isEmpty()){
                sendFunction();
            }
        }
    }
});
Gardenia answered 4/6, 2018 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.