Solved

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

Posted on 2000-04-24
14
315 Views
Last Modified: 2008-02-01
I want to call a method when <Enter> key is pressed in JTextArea.


Thanks
Theta.
0
Comment
Question by:theta
  • 9
  • 5
14 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2743975
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
 

Author Comment

by:theta
ID: 2744086
Sasha, how do I know in the actionPerformed method that Enter key is pressed?

Theta.
0
 

Author Comment

by:theta
ID: 2744102
Can't call addActionListener for JTextArea.
Theta.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744107
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744140
Oops, sorry, thought we were talking about JTextField...apologies...
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744155
Hmm, you can just add a KeyListener and see when the key that was pressed is KeyEvent.VK_Enter...
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744167

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


 }

}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744168
Would you like a complete working example or is this enough?
0
 

Author Comment

by:theta
ID: 2744268
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
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 30 total points
ID: 2744322
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
 

Author Comment

by:theta
ID: 2744732
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2744783
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
 

Author Comment

by:theta
ID: 2745295
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2745468
yes, myString.substring(0,myString.length()-1); would return you the string without its last character...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now