Home arrow static arrow Java Programming [Archive] - KeyEvent
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - KeyEvent
This topic has 4 replies on 1 page.

Registered: 4/9/04
Aug 5, 2004 7:09 AM


I'm developing an applet based on Swing.
I implemented the KeyListener interface and overwrote the keyPressed() method like this: (this is only a prt of the code)
	/** Clase que implementa los eventos del teclado */	class GeneradorEventos implements KeyListener	{		/** Metodo sobrecargado de tecla oprimida		 *  Si se Oprime un BID,OFFER, HIT � TAKE se realizara una accion		 * @param tecla Tecla orpimida */		public void keyPressed(KeyEvent tecla)		{			switch (tecla.getKeyCode())			{				// F1 -> BID	      case KeyEvent.VK_TAB :  { foo(); } ; break;                           default : break;                                                               }                                           }		/** Metodo que no lo sobrecargo pero debe estar		 * @param e Tecla tipeada */		public void keyTyped(KeyEvent e) {};		/** Metodo que no lo sobrecargo pero debe estar    	* @param e Tecla soltada */		public void keyReleased(KeyEvent e) {}}

The applet has some JTextFields that add the KeyListener done by me. I did this:
GeneradorEventos generador = new GeneradorEventos();texto.addKeyListener(generador); // where texto is a JTextField

The problem i'm having is that nothing happens when i press TAB and i don't know why, beacause when other key is pressed everything is OK.


P.S. :Sorry for my english!!

Registered: 99-04-02
Re: KeyEvent  
Aug 5, 2004 8:35 AM (reply 1 of 4)

I don't think the text field itself, and thus it's listeners, actually receive the tab key presses... it's consumed before the field gets it.

Do a search for Java key bindings... that may work.

Registered: 4/9/04
Re: KeyEvent  
Aug 6, 2004 12:17 PM (reply 2 of 4)

Hi again!!!!!!!!!

i've been trying with ur suggestion but it was impossible. I did this and didn't work:
Action doNothing = new AbstractAction(){	public void actionPerformed(ActionEvent e) 	{		System.out.println("A traves del actionnnnnn.\n");	}};// textoInstrumento is a JTextFieldtextoInstrumento.getInputMap().put(KeyStroke.getKeyStroke("TAB"),	"doNothing");textoInstrumento.getActionMap().put("doNothing", doNothing);

How can i get a "TAB" event???? I can't do it and i'm don't know what to do. Please help me


P.S.: as i always say: "sorry for my english"

Registered: 17/08/03
Re: KeyEvent  
Aug 6, 2004 2:13 PM (reply 3 of 4)

How can i get a "TAB" event???? I can't do it and i'm
don't know what to do. Please help me

not sure this is what you're after
import java.awt.event.*;import java.awt.*;import javax.swing.*;import java.util.*; class Testing extends JFrame implements KeyListener{  JTextField tf1 = new JTextField(10);  JTextField tf2 = new JTextField(10);  public Testing()  {    setTitle("Testing");    setDefaultCloseOperation(EXIT_ON_CLOSE);    setSize(300,75);    setLocationRelativeTo(null);    JPanel jp = new JPanel();    tf1.addKeyListener(this);    tf1.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,Collections.EMPTY_SET);    tf2.addKeyListener(this);    tf2.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,Collections.EMPTY_SET);    jp.add(tf1);    jp.add(tf2);    getContentPane().add(jp);  }   public void keyPressed(KeyEvent ke)  {    if(ke.getKeyCode() == KeyEvent.VK_TAB)JOptionPane.showMessageDialog(null,"TAB pressed");  }  public void keyTyped(KeyEvent ke){}  public void keyReleased(KeyEvent ke){}  public static void main( String args[] ){new Testing().setVisible(true);}}

Registered: 4/9/04
Re: KeyEvent  
Aug 9, 2004 5:39 AM (reply 4 of 4)

Thanks a lot. That's exactly what i was looking for

This topic has 4 replies on 1 page.