Solved

Processing a TAB KeyEvent

Posted on 2004-09-05
6
1,320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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