Link to home
Start Free TrialLog in
Avatar of theta
theta

asked on

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.
Avatar of Sasha_Mapa
Sasha_Mapa

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...
Avatar of theta

ASKER

Sasha, how do I know in the actionPerformed method that Enter key is pressed?

Theta.
Avatar of theta

ASKER

Can't call addActionListener for JTextArea.
Theta.
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...
Oops, sorry, thought we were talking about JTextField...apologies...
Hmm, you can just add a KeyListener and see when the key that was pressed is KeyEvent.VK_Enter...

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


 }

}
Would you like a complete working example or is this enough?
Avatar of theta

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Sasha_Mapa
Sasha_Mapa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of theta

ASKER

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



}



Avatar of theta

ASKER

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.
yes, myString.substring(0,myString.length()-1); would return you the string without its last character...