Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping default behavior of events in Swing

I have the following bit of code in a method called by clicking the send button, or pressing enter in the message text field in a piece of code.

// In class ChatWindow
private void messageTextAreaKeyPressed(java.awt.event.KeyEvent evt) { // Event handler created by Netbeans GUI designer to call this method.           
    if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
        sendMessage();
    }
}   
public void sendMessage() {
    String currentMessage = messageTextArea.getText();
    addMessage("You", currentMessage);
    app.sendMessage(currentMessage, 1);
    messageTextArea.setText("");
}

The last bit of code blanks the text area. However, after a message is sent by pressing the enter button, rather than being empty, the text box contains a newline.

My guess is that after my event handler runs, THEN the newline character is being added. How to I stop the newline being added?

like image 898
Macha Avatar asked Jul 01 '09 12:07

Macha


People also ask

How to stop default Action in javascript?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form.

Why event preventDefault() is used?

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link.

What is event prevent default?

Event.preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

How are events handled in swing?

Step 1 − The user clicks the button and the event is generated. Step 2 − The object of concerned event class is created automatically and information about the source and the event get populated within the same object. Step 3 − Event object is forwarded to the method of the registered listener class.


2 Answers

try adding evt.consume() after your call to sendMessage()

private void messageTextAreaKeyPressed(java.awt.event.KeyEvent evt) { 
 if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
    sendMessage();
    evt.consume();
 }
}  
like image 119
akf Avatar answered Nov 11 '22 01:11

akf


The default Action for the Enter key in a JTextArea is to insert a new line as you have seen. So the solution is to replace the default Action with a custom Action. The benefit of this approach is that this Action can also be used by the JButton (or JMenuItem etc.). An Action is basically the same as an ActionListener, all you need to do is implement the actionPerformed() method.

Read up on Key Bindings to see how this is done. All Swing components use Key Bindings.

like image 34
camickr Avatar answered Nov 11 '22 02:11

camickr