Solved

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

Posted on 2000-04-24
14
308 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

13 Experts available now in Live!

Get 1:1 Help Now