Solved

Processing a TAB KeyEvent

Posted on 2004-09-05
6
1,311 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

809 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