简体   繁体   English

JavaFX:如何使Enter键提交TextArea

[英]JavaFX: How to make enter key submit TextArea

Sorry if this seems a little too easy, I'm brand new to JavaFX, this is my first little app built with it. 抱歉,如果这看起来有点容易,我是JavaFX的新手,这是我第一个使用它构建的小应用程序。

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. 我正在使用JavaFX Scene构建器制作客户端UI,以及连接到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? 如何使文本区域中的当前文本提交到服务器,并在按Enter键时清除文本区域,而不是使用某种“发送”按钮?

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();
        }
    }
});

//...

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. 无论哪种方法,祝您好运。

You can use lambda expressions also ... I think it is more elegant and simply 您也可以使用lambda表达式...我认为它更优雅,更简单

textArea.setOnKeyPressed(event -> {
   if(event.getCode() == KeyCode.ENTER){
     //type here what you want
   }
}); 

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. 除了其他答案,我认为如果用户按下SHIFT + ENTER,实际上不调用send函数可能在某些应用程序中很有用。 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();
            }
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM