The Artima Developer Community
Sponsored Link

Java Answers Forum
JComboBox problem

1 reply on 1 page. Most recent reply: Jul 5, 2002 11:03 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jany maker

Posts: 2
Nickname: jany
Registered: Jul, 2002

JComboBox problem Posted: Jul 5, 2002 7:15 AM
Reply to this message Reply
Advertisement
hello everybody!
it is intresting! comboBox.addActionListener dont work!
look at this, run this program,
and type letters in comboBox. no "hello" is showing,
and type letters in textField. "hello" is showing;
thanks from befor

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
cl*** Test extends JFrame
{
public Test()
{
super( "Customer" );
String s[] = {"five","six","seven"};
JComboBox co = new JComboBox(s);
JTextField t = new JTextField();
Container c = getContentPane();
c.add( co, BorderLayout.NORTH );
c.add(t,BorderLayout.SOUTH);
setSize(300,300);setVisible( true );
co.setEditable(true);
t.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{ System.out.println("hello");}
});
co.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{ System.out.println("hello");}
});
}
public static void main( String args[] ){ new Test(); }
}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Use this: co.getEditor().getEditorComponent().addKeyListener Posted: Jul 5, 2002 11:03 AM
Reply to this message Reply
Hi
JComboBox uses an editor to let you edit the content. So, when you are typing in JComboBox then it is firing key pressed events on the underlying editor and not the JComboBox. You need to add key listener to the underlying editor. To get the reference of that editor you can use getEditor().getEditorComponent() on you JComboBox. The following code works and it prints "hi" when you type in your combo box.
Thanks
Kishori
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ComboB extends JFrame {
public ComboB() {
	super( "Customer" );
	String s[] = {"five","six","seven"};
	JComboBox co = new JComboBox(s);
	JTextField t = new JTextField();
	Container c = getContentPane();
	c.add( co, BorderLayout.NORTH );
	c.add(t,BorderLayout.SOUTH);
	setSize(300,300);
	setVisible( true );
	co.setEditable(true);
	t.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent k){
				System.out.println("hello");}
			});
			/* Don't use it
		co.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent k){
				System.out.println("hello");
			}
			});
			*/
		/* Use the following */
		co.getEditor().getEditorComponent().addKeyListener ( new KeyAdapter() {
			public void keyPressed(KeyEvent k){
				System.out.println("hi");
			}
		}		);
}
 
	public static void main( String args[] ){
		new ComboB();
	}
} 

Flat View: This topic has 1 reply on 1 page
Topic: Listener Problem(Not working well) Previous Topic   Next Topic Topic: Help with applets and the object tag

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use