Solved

Processing a TAB KeyEvent

Posted on 2004-09-05
6
1,310 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
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 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

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

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 44
Starting to learn JAVA, 7 59
even odd program using while loop 3 25
configure dependency in POM for new database 3 13
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

919 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

15 Experts available now in Live!

Get 1:1 Help Now