Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

How can I perform an action on <Enter> key in JTextArea?

I want to call a method when <Enter> key is pressed in JTextArea.


Thanks
Theta.
0
theta
Asked:
theta
  • 9
  • 5
1 Solution
 
Sasha_MapaCommented:
java.awt.TextField sends an action event when you hit enter in it. I think JTextField should to... What you have to do is implement ActionListener and register it as the ActionListener for your button with myButton.addActionListener(myActionListener); . In the actionPerformed method you can do whatever you wanted to...
0
 
thetaAuthor Commented:
Sasha, how do I know in the actionPerformed method that Enter key is pressed?

Theta.
0
 
thetaAuthor Commented:
Can't call addActionListener for JTextArea.
Theta.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Sasha_MapaCommented:
Well, that's the only key a textfield sends an action event on... so if an action event happened on a textfield, then you know enter was pressed...
0
 
Sasha_MapaCommented:
Oops, sorry, thought we were talking about JTextField...apologies...
0
 
Sasha_MapaCommented:
Hmm, you can just add a KeyListener and see when the key that was pressed is KeyEvent.VK_Enter...
0
 
Sasha_MapaCommented:

public void keyTyped(KeyEvent evt){
  if (evt.getKeyCode()==KeyEvent.VK_ENTER){
    //do something here


 }

}
0
 
Sasha_MapaCommented:
Would you like a complete working example or is this enough?
0
 
thetaAuthor Commented:
here is what I just tried and it didn't work:

myTextArea.registerKeyboardAction(
  listener,
  KeyStroke.getKeyStroke
  (KeyEvent.VK_ENTER, 0),
   JComponent.WHEN_IN_FOCUSED_WINDOW
   );

 ...
class Listener implements ActionListener {
  public void actionPerformed (ActionEvent e) {

  Object src = e.getSource();
  String cname = src.getClass().getName();
  if (src instanceof JTextArea) {
     // Execute my code
     
   }
  }
}

I have put a message in the actionPerformed, but it didn't executed, which means the Listener is not being called.

Working example would be great.

Thanks.
Theta.
0
 
Sasha_MapaCommented:
Apparently, the enter key event isn't sent into the keyTyped method, only to the keyPressed and keyReleased methods, this is a complete working example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyApp implements KeyListener{

  public static void main(String [] args){
    Frame f = new Frame();
    f.reshape(50,50,500,500);
    f.setLayout(null);
    JTextArea ta = new JTextArea();
    ta.reshape(50,50,200,200);
    ta.addKeyListener(new MyApp());
    f.add(ta);
    f.show();
  }


  public void keyPressed(KeyEvent evt){
    if (evt.getKeyCode()==KeyEvent.VK_ENTER)
      System.out.println("enter was pressed");

  }

  public void keyReleased(KeyEvent evt){}

  public void keyTyped(KeyEvent evt){}

}
0
 
thetaAuthor Commented:
Thanks for the example.

I am instantiating the class which has
JTextArea, so I cannot implement interface on it. Is there a work around to this?

Theta.
0
 
Sasha_MapaCommented:
Umm, no actually, there is no good workaround if the JTextArea is a private member. You can use a workaround with the deprecated 1.0 event handling system and just implement a keyDown(Event, int key) method in the container class that contains the component that contains the JTextArea and the key event fired in the JTextArea will propagade to that container, where you can get it, check whether they key pressed is enter and do something with it...
Here's an example, I haven't compiled it so I can't say it's a working example :-)

public class MyApp extends Panel{

  public MyApp(){
   setLayout(null);
   YourComponentWithJTextArea com = new YourComponentWithJTextArea();
    comp.reshape(50,50,100,100);
    add(comp);
  }


  public boolean keyDown(Event evt, int key){
    if (evt.target instanceof JTextArea) // This is the closest check you can do,
                                                           // if there are more JTextFields in your
                                                           // container or that component, it will
                                                           // not work.  
       if (key == Event.ENTER)
         System.out.println("enter was pressed in some JTextArea");
     return true;
  }


  public static void main(String[] args){
    Frame f = new Frame();
    f.reshape(50,50,500,500);
    f.add(new MyApp());
    f.show();
  }



}



0
 
thetaAuthor Commented:
I tried you example, but couldn't get it to work. I got it working using registerKeyboardAction(). I just have one problem, I am getting an extra line feed when I pass the string entered in myTextArea to another method. Is there a way to remove a characters from the end of a String?

Thanks.

Theta.
0
 
Sasha_MapaCommented:
yes, myString.substring(0,myString.length()-1); would return you the string without its last character...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now