Solved

Processing a TAB KeyEvent

Posted on 2004-09-05
6
1,309 Views
Last Modified: 2012-08-13
Hey Experts,

I wanted to programmatically process a TAB KeyEvent at a certain part of a program (as if the user has pressed the TAB Key). I wrote the following piece of code:

DefaultFocusManager fm = new DefaultFocusManager();
fm.processKeyEvent(jtablex,KeyEvent.VK_TAB); //where jtablex is a Component

The problem with this code was that KeyEvent.VK_TAB is an integer not a KeyEvent object (and there is no overloaded function that takes a component and a KeyEvent object).What do I do to process this KeyEvent?
0
Comment
Question by:mte01
  • 3
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 270 total points
ID: 11983664
Try

EventQueue.postEvent(new KeyEvent(......................));

and see

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/KeyEvent.html#KeyEvent(java.awt.Component, int, long, int, int, char)
0
 
LVL 3

Assisted Solution

by:JohnnyAffa
JohnnyAffa earned 230 total points
ID: 11984021
here you go!  

ive simulated the TAB key press by using a thread and tiggering it to act after 3 seconds, after which a message "Wow, how did that happen?" is displayed to the console.  you need to implement the KeyListener interface givent that you want to trigger a key event.

how its want you want ;-)

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

class TAB extends JFrame implements Runnable, KeyListener
{
      public TAB()
      {
            this.setInterface();
      }

      private void setInterface()
      {
            this.setBounds(50,80,400,400);

            this.setVisible(true);

            (new Thread(this)).start();
      }

      public void keyPressed( KeyEvent e)
      {
            if ( e.getID() == KeyEvent.VK_TAB )
                  System.out.println("Wow, how did that happen?");
      }
 
      public void keyReleased( KeyEvent e)
      {

      }

      public void keyTyped( KeyEvent e)
      {

      }

      public void processWindowEvent(WindowEvent event)
      {
            if ( event.getID() == WindowEvent.WINDOW_CLOSING )
                  System.exit(0);
      }

      public void run()
      {
            try
            {
                  Thread.sleep(3000);
                  this.keyPressed(new KeyEvent(this,KeyEvent.VK_TAB,0,0,0));
            }
            catch (Exception a)
            {

            }
      }

      public static void main(String args[])
      {
            new TAB();
      }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 270 total points
ID: 11984234
That of course won't act like a 'normal' key event as it's not posted on the event dispatch thread
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:mte01
ID: 11984379
Thanks for all your support guys, I'll try to award the points as fairly as possible.
0
 
LVL 3

Author Comment

by:mte01
ID: 11984384
I increased the number of points so that you get fairly rewarded
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11984394
:-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
computer science syllabus 3 52
wordlen challenge 3 47
Which is the latest version of Apache Tomcatl 3 35
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now