Solved

JComboBox key pressed

Posted on 2000-05-02
11
692 Views
Last Modified: 2010-05-18
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(KeyEvent e)

void jComboBox1_keyReleased(KeyEvent e)

void jComboBox1_keyTyped(KeyEvent e)

But none helped.

???
0
Comment
Question by:s_lavie
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:adam923
Comment Utility
what you're doing works in visual basic, not in java
in java you must call
jComoBox1.addKeyListener(this);
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
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
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 :-)
0
 
LVL 4

Expert Comment

by:adam923
Comment Utility
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(myComboBox);
                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
0
 

Expert Comment

by:funduraghu
Comment Utility
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(true);
      myComboBox.addItem("Hello");
      myComboBox.addItem("There");
      getContentPane().add(myComboBox);
      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.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Author Comment

by:s_lavie
Comment Utility
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!

0
 
LVL 4

Accepted Solution

by:
kylar earned 75 total points
Comment Utility
I'm sorry, I misinterpereted what you wanted. For everyone's information: JComboBox DOES support KeyListeners (All things descended from java.awt.Component do) but in this case, it doesn't produce the desired effect. Try this instead:

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.setEditable(true);
                      myComboBox.getEditor().getEditorComponent().addKeyListener(this)
                   
                      }

                      public void keyPressed(KeyEvent e){
                       System.out.println("KeyPressed:"+e.getKeyChar());
                      }

                      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

that should do what you want. Sorry for the confusion.

Kylar
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
Kylar,
As I noticed, the only different
is in the addKeyListener() line:
myComboBox.getEditor().getEditorComponent().addKeyListener(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(true);
                addKeyListener(this);
                jComboBox1.getEditor().getEditorComponent().addKeyListener(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("KeyPressed: " + e.getKeyChar());
      }

      public void keyReleased(KeyEvent e)
       {
             System.out.println("keyReleased: " + e.getKeyChar());
      }

      public void keyTyped(KeyEvent e)
      {
             System.out.println("keyTyped: " + e.getKeyChar());
      }

      public static void main(String[] a)
      {
            JFrame frame = new myFrame();
            frame.pack();
            frame.show();
      }

      private void jbInit() throws Exception
      {
            jPanel1.setLayout(flowLayout1);
            jButton1.setText("jButton1");
            this.getContentPane().add(jPanel1, BorderLayout.CENTER);
            jPanel1.add(jComboBox1, null);
            jPanel1.add(jButton1, null);
      }

}//end main class
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
Adjusted points from 25 to 75
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
Sorry Kylar,
My mistake - (copy-paste problem)...
Your code works fine :-)
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
Hehe, no problem. I do that all the time when I cut and paste. Darn self-replicating lines ;)

Cheers
Kylar
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Modeling a class in java 5 31
Eclipse Neon and jdk 1.8.0 11 65
Python Assistance 7 31
Non-recursive backtracking, using a stack 1 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now