s_lavie
asked on
JComboBox key pressed
Hi,
I want to detect a key pressed event, when entering a text in an editable JComboBox.
I tried the following events:
void jComboBox1_keyPressed(KeyE vent e)
void jComboBox1_keyReleased(Key Event e)
void jComboBox1_keyTyped(KeyEve nt e)
But none helped.
???
I want to detect a key pressed event, when entering a text in an editable JComboBox.
I tried the following events:
void jComboBox1_keyPressed(KeyE
void jComboBox1_keyReleased(Key
void jComboBox1_keyTyped(KeyEve
But none helped.
???
To clear this up a little :)
import javax.swing.*;
import java.awt.event.*;
public myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
//set the combo box to do whatever you want.
myComboBox.addKeyListener( this);
}
public void keyPressed(KeyEvent e){
//put whatever you want in here
}
public void keyReleased(KeyEvent e){
//put whatever you want in here
}
public void keyTyped(KeyEvent e){
//put whatever you want in here
}
}//end main class
Hope this helps :)
Kylar
import javax.swing.*;
import java.awt.event.*;
public myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
//set the combo box to do whatever you want.
myComboBox.addKeyListener(
}
public void keyPressed(KeyEvent e){
//put whatever you want in here
}
public void keyReleased(KeyEvent e){
//put whatever you want in here
}
public void keyTyped(KeyEvent e){
//put whatever you want in here
}
}//end main class
Hope this helps :)
Kylar
ASKER
Kylar,
I did what you said, but I couldn't detect, any key event, while entering text in the combo's textfield :-(
If you can write a sample code, that when entering a text, something is printed (using System.out.print() or whatever).
Increasing points is a guaranty :-)
I did what you said, but I couldn't detect, any key event, while entering text in the combo's textfield :-(
If you can write a sample code, that when entering a text, something is printed (using System.out.print() or whatever).
Increasing points is a guaranty :-)
The above example doesn't really do anything... I made some modifications. The biggest modification is that the combo box doesn't generate the key events so you can just add the key listener to the whole frame.
import javax.swing.*;
import java.awt.event.*;
public class myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
myComboBox.addItem("Hello" );
myComboBox.addItem("There" );
getContentPane().add(myCom boBox);
addKeyListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
System.out.println("x");
}
public static void main(String[] a) {
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
}//end main class
import javax.swing.*;
import java.awt.event.*;
public class myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
myComboBox.addItem("Hello"
myComboBox.addItem("There"
getContentPane().add(myCom
addKeyListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
System.out.println("x");
}
public static void main(String[] a) {
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
}//end main class
The solution is the same as what adam suggested, but u would have to add ur key detection code in keyPressed() and ketReleased() methods.
JComboBox doesn't support KeyListeners. Hence add the keyListener to the main Frame and then check for the ketPressed and ketReleased events.
Here is the sample code rewritten.
import javax.swing.*;
import java.awt.event.*;
public class myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
myComboBox.setEditable(tru e);
myComboBox.addItem("Hello" );
myComboBox.addItem("There" );
getContentPane().add(myCom boBox);
addKeyListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void keyPressed(KeyEvent e) {
System.out.println("x");
}
public void keyReleased(KeyEvent e) {
System.out.println("y");
}
public static void main(String[] a) {
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
}//end main class
Hope this is what u want.
JComboBox doesn't support KeyListeners. Hence add the keyListener to the main Frame and then check for the ketPressed and ketReleased events.
Here is the sample code rewritten.
import javax.swing.*;
import java.awt.event.*;
public class myFrame extends JFrame implements KeyListener{
public myFrame(){
JComboBox myComboBox = new JComboBox();
myComboBox.setEditable(tru
myComboBox.addItem("Hello"
myComboBox.addItem("There"
getContentPane().add(myCom
addKeyListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void keyPressed(KeyEvent e) {
System.out.println("x");
}
public void keyReleased(KeyEvent e) {
System.out.println("y");
}
public static void main(String[] a) {
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
}//end main class
Hope this is what u want.
ASKER
Sorry,
But adding the key listener to the whole frame, generates a key event also when other control is in focus!
What I want is to response only for key events related to the combo box!
But adding the key listener to the whole frame, generates a key event also when other control is in focus!
What I want is to response only for key events related to the combo box!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kylar,
As I noticed, the only different
is in the addKeyListener() line:
myComboBox.getEditor().get EditorComp onent().ad dKeyListen er(this)
But, what I did was as follows:
I add a JPanel to the JFrame, than add a JComboBox and a JButton to that JPanel.
There was no different whether the JComboBox was in focus, or the JButton was in focus - the result was the same.
Maybe I missed something?
Here is my code:
public class myFrame extends JFrame implements KeyListener
{
JPanel jPanel1 = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
JComboBox jComboBox1 = new JComboBox();
JButton jButton1 = new JButton();
public myFrame()
{
jComboBox1.addItem("Hello" );
jComboBox1.addItem("There" );
jComboBox1.setEditable(tru e);
addKeyListener(this);
jComboBox1.getEditor().get EditorComp onent().ad dKeyListen er(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void keyPressed(KeyEvent e)
{
System.out.println("KeyPre ssed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e)
{
System.out.println("keyRel eased: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e)
{
System.out.println("keyTyp ed: " + e.getKeyChar());
}
public static void main(String[] a)
{
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
private void jbInit() throws Exception
{
jPanel1.setLayout(flowLayo ut1);
jButton1.setText("jButton1 ");
this.getContentPane().add( jPanel1, BorderLayout.CENTER);
jPanel1.add(jComboBox1, null);
jPanel1.add(jButton1, null);
}
}//end main class
As I noticed, the only different
is in the addKeyListener() line:
myComboBox.getEditor().get
But, what I did was as follows:
I add a JPanel to the JFrame, than add a JComboBox and a JButton to that JPanel.
There was no different whether the JComboBox was in focus, or the JButton was in focus - the result was the same.
Maybe I missed something?
Here is my code:
public class myFrame extends JFrame implements KeyListener
{
JPanel jPanel1 = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
JComboBox jComboBox1 = new JComboBox();
JButton jButton1 = new JButton();
public myFrame()
{
jComboBox1.addItem("Hello"
jComboBox1.addItem("There"
jComboBox1.setEditable(tru
addKeyListener(this);
jComboBox1.getEditor().get
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void keyPressed(KeyEvent e)
{
System.out.println("KeyPre
}
public void keyReleased(KeyEvent e)
{
System.out.println("keyRel
}
public void keyTyped(KeyEvent e)
{
System.out.println("keyTyp
}
public static void main(String[] a)
{
JFrame frame = new myFrame();
frame.pack();
frame.show();
}
private void jbInit() throws Exception
{
jPanel1.setLayout(flowLayo
jButton1.setText("jButton1
this.getContentPane().add(
jPanel1.add(jComboBox1, null);
jPanel1.add(jButton1, null);
}
}//end main class
ASKER
Adjusted points from 25 to 75
ASKER
Sorry Kylar,
My mistake - (copy-paste problem)...
Your code works fine :-)
My mistake - (copy-paste problem)...
Your code works fine :-)
Hehe, no problem. I do that all the time when I cut and paste. Darn self-replicating lines ;)
Cheers
Kylar
Cheers
Kylar
in java you must call
jComoBox1.addKeyListener(t
right after you create jComboBox1
then in your class definition add the phrase
implements KeyListener
and then add the methods
void keyPressed(KeyEvent e)
(and the other two) to your class