Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JComponent accepting Keyboard input

Status
Not open for further replies.

Toyman

Programmer
Jun 19, 2001
68
0
0
GB
Hi

I've got a class that extends JComponent.
The class implements KeyListener.
Nothing happens when I pless a key !!!
Any ideas why ?

NOTE: ShapeViewer extends JComponent !!!!!

Here is the code.

import java.awt.event.*;

public class ShapeEditor extends ShapeViewer implements KeyListener{

//Constructor(s)
public ShapeEditor(){
addListeners();
}

//Private Methods
private void addListeners(){
addKeyListener(this);
}

//Public Methods
//Overwrite Methods
//Keyboard Events
public void keyPressed(KeyEvent e){
//Invoked when a key has been pressed.
System.out.println("Key " + e.getKeyCode());
}
public void keyReleased(KeyEvent e){
//Invoked when a key has been released.
System.out.println("Key " + e.getKeyCode());
}
public void keyTyped(KeyEvent e){
//Invoked when a key has been typed.
System.out.println("Key " + e.getKeyCode());
}
}

Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
 
Not sure what your error is (don't know what is in "ShapeViewer"), but try this simple example, it works (as long as the window is in focus) :

Code:
import java.awt.event.*;
import java.awt.*;

public class KeyBoard extends Frame implements KeyListener{

	public KeyBoard() {
		addKeyListener(this);
		setSize(400, 400);
		setVisible(true);
	}

	public static void main(String args[]) {
		KeyBoard kb = new KeyBoard();
	}
    public void keyPressed(KeyEvent e){
          //Invoked when a key has been pressed.
          System.out.println("Key " + e.getKeyCode());
    }
    public void keyReleased(KeyEvent e){
        //Invoked when a key has been released.
        System.out.println("Key " + e.getKeyCode());
    }
      public void keyTyped(KeyEvent e){
          //Invoked when a key has been typed.
          System.out.println("Key " + e.getKeyCode());
      }
}




--------------------------------------------------
Free Database Connection Pooling Software
 
HI sedj

Thanks for the reply. But...
This is not wat I want to do....
ShapeViewer is just a class extended from JComponent....
What I want to do is to have a KeyListener on a Class that is extended from a JComponent....

So that when you add the JComponent class to a Frame that one don't need to add KeyListeners to the Frame but it is should already by "activated" through the JComponent.

Think of a JTextField.... surely the KeyListeners is not on the Form to which you add it... but internal to the JTextField Keyhandler Events.....I want to do the same thing !!!!

Thanks


Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
 
I'm not entirely sure what you're getting at - this is the closest I can imagine you can do ...

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

public class KeyBoardFrame extends JFrame {

	public KeyBoardFrame() {
		setSize(400, 400);
		JTextField jtf = new JTextField();
		jtf.addKeyListener(new KeyBoard());
		getContentPane().add(jtf);
		setVisible(true);
	}

	public static void main(String args[]) {
		KeyBoardFrame kb = new KeyBoardFrame();
	}

}

class KeyBoard extends Component implements KeyListener{

	public KeyBoard() {
		this.addKeyListener(this);
	}

    public void keyPressed(KeyEvent e){
          //Invoked when a key has been pressed.
          System.out.println("Key " + e.getKeyCode());
    }
    public void keyReleased(KeyEvent e){
        //Invoked when a key has been released.
        System.out.println("Key " + e.getKeyCode());
    }

    public void keyTyped(KeyEvent e){
        //Invoked when a key has been released.
        System.out.println("Key " + e.getKeyCode());
    }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top