JComboBox Problem
Hi all
Can anyone please help me out of this problem
Actually i have a editable JComboBox
On that comboBox i am not understanding which Listener should i add and on which component i should add it so that ,
whenever i select an item from the dropdown list of comboBox the event gets fired and i can call some other method
I tried few codes but none of them seem to work
ItemListeners and ActionListeners are of no use as they are fired even when i just scroll through the drop down list without clicking any Item
Please help me
Thanking in advance
[606 byte] By [
Arti_mda] at [2007-10-2 3:54:33]

comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// do something with comboBox.getSelectedIndex());
// or with comboBox.getSelectedItem();
// or with comboBox.getSelectedObjects();
}
}
});
Hint: read the documentation...
The solution suggested did not work
Because as expected the condition
e.getStateChange() == ItemEvent.SELECTED is true even when a user just scrolls throught the drop down list using UP an DOWN keys of keyboard
This will hence lead to many unwanted database hits
Any more suggestions ?
Thanks
Put an Event handler ItemStateChanged(ItemEvent e)In that ...String str=e.getSelectedItem().toString() if(str.equals(.....))..........i hope it will work...RegsSudhir
> The solution suggested did not work
>
> Because as expected the condition
> e.getStateChange() == ItemEvent.SELECTED is true even
> when a user just scrolls throught the drop down list
> using UP an DOWN keys of keyboard
>
> This will hence lead to many unwanted database hits
i does work and it does just what it is supposed to do, when you use the up/down keys! you select an item with the keys and hence you get a state change event. how else would you expect the combo box to know, when/what you selected. e.g if you use the mouse/scroll bar you get an eventonce you click on an item in the list.
pls read the following, maybe it helps you understand the mechanism behind the combo box class: http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#listeners
thomas
I am sorry but somehow didnt understand what you are trying to say I need a method of some Listener which will be fired only when i select an Item from the drop down list by clicking the Mouse
> I need a method of some Listener which will be fired
> only when i select an Item from the drop down list by
> clicking the Mouse
this listener is the itemStateListener, as shown in my previous posting.
however, if you use the up/down key to scroll through the list, then each up/down will fire the event, because each up/down is equal to a mouse click, ie item selection.
thomas
i guess the only solution is to add a addtional button the user has to click to make a selection.
> ItemListeners and ActionListeners are of no use as they are fired
> even when i just scroll through the drop down list without clicking any Item
Check out this posting, the Action will only fire when the item is selected:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=569767
Works great for non-editable combo boxes.
For editable combo boxes, the ActionEvent fires twice, but you may be able to work around that easier than your current problem.
I just checked out the code u had posted I tried out the code and found that the user wont be able to actually scroll through the list since we hide the popup dropdown list
you want to
1) type something into the combobox and hit enter
2) scroll through the dropdown with no event until selected by click
if so, try something like this
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing extends JFrame
{
String cities[] = {"London","Madrid","New York","Rome","Sydney"};
JComboBox cbo = new JComboBox(cities);
JTextArea ta = new JTextArea();
public Testing()
{
setLocation(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel();
cbo.setEditable(true);
JTextField tf = (JTextField)cbo.getEditor().getEditorComponent();
top.add(cbo);
JScrollPane sp = new JScrollPane(ta);
sp.setPreferredSize(new Dimension(200,100));
getContentPane().add(top,BorderLayout.NORTH);
getContentPane().add(sp,BorderLayout.CENTER);
pack();
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setTextAreaText();}});
cbo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
if(ie.getStateChange() == ItemEvent.SELECTED);
setTextAreaText();}});
}
public void setTextAreaText()
{
ta.setText((String)cbo.getSelectedItem());
}
public static void main(String[] args){new Testing().setVisible(true);}
}
Hi Michael
I have already incorporated your suggestions and tried before but of no use
Again i did compile your code and on executing i found that
itemStateChanged() method is called even while scrolling through the drop down list
So it still does not provide any workaround
yep, didn't test for scrolling via arrow keys
swap the itemListener for this
(but it loses the click event)
cbo.getComponent(0).addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
{
setTextAreaText();
}}});
but now (1.5.0_03), the editable combo box requires enter to be hit twice
before changing the text
anyway, somewhere in all this is your combo, just a matter of experimenting
To restate your problem:
A JComboBox fires an ItemEvent each time the selection in the JComboBox is changed, whether that be through keyboard control or selection by mouse. You want the ItemEvent to be handled only if the change was the result of a mouse operation and not the result of a keyboard operation.
First, you must understand that:
* The JComboBox doesn't actually "drop down" per se. It creates a popup item which then happens to be drawn in a specific location; this is how it extends past the component's actual display boundaries. (Someone correct me if I'm wrong about this, although I'm relatively certain that's how it's handled.)
* The purpose of ItemEvent is to notify you that the contents of the JComboBox (that is, the one line selection of text and not the aforementioned popup) have changed. If you use the keyboard, this occurs without the intervention of the popup. If you use the mouse, you circumvent passing through the intermediaries by using a method of control more complex than the previous/next choice provided by the keyboard; this is why you only get one event.
You should consider:
If you change the behavior of this JComboBox and it still looks like a normal JComboBox, your users may expect to be able to change it via keyboard. I personally use the mouse as little as possible when I am not playing games; I am much faster on the keyboard most of the time. Thus, your application would require me to switch from my usual routine and, if you're not familiar with how fussy users can be about that, you're sure to find out quickly enough. :)
If you decide to go ahead with this plan:
It seems the best way for you to accomplish this is to allow the ItemEvent to be fired and then only respond if it was the result of mouse input. The easiest way to determine this is to create a JComboBox subclass which attaches to itself two listeners: a MouseListener and a KeyListener. Whenever any method on the KeyListener is called, that listener sets a field variable on your subclass to false, representing that no response is warranted. Whenever any method on the MouseListener is called, that listener sets the same field to true. Then, provide an accessor for this variable so that, when an ItemListener receives an ItemEvent, it first calls that accessor to determine whether or not it should pay attention.
Good luck, and happy coding. :)
tvynra at 2007-7-15 23:15:34 >

Has anybody actually tried the code I provided?
It allows you to use the keyboard to navigate through the items in the dropdown without firing any events. The only time the event is fired is when you use the enter key to finallly select an item. This allows you to use the escape key at any time without affecting the original state of the editor component.
The only problem, as I mentioned earlier, is that once you do select an item, 2 ActionEvents are fired. Maybe it is easier to code around this problem. For example keep the time of the last ActionEvent fired. If the time is within "x" milliseconds, then ignore the ActionEvent.
> Has anybody actually tried the code I provided?
I didn't because the OP commented:
> I tried out the code and found that the user wont be able to actually scroll
> through the list since we hide the popup dropdown list
On (now) running it , it does seem to do exactly what he wants (1.5.0_03)
(so I don't know what he's talking about)
I'll know better next time.
I am not certain, but in many of my experiences the user will select any info for their combo box ( basically I ignore the events fired from changing the value in the box), and then I have them click on a "submit" button or something similar that gets that the value from the the combo box and then do whatever I need to with that data.
would that help you?
I seriuosly tried your code
Hope this is the code that are refering to
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class ComboBoxAction extends JFrame implements ActionListener
{
JComboBox comboBox;
public ComboBoxAction()
{
comboBox = new JComboBox();
comboBox.addActionListener( this );
//
comboBox.addItem( "Item 1" );
comboBox.addItem( "Item 2" );
comboBox.addItem( "Item 3" );
comboBox.addItem( "Item 4" );
// This prevents action events from being fired when the
// up/down arrow keys are used on the dropdown menu
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//
getContentPane().add( comboBox );
}
public void actionPerformed(ActionEvent e)
{
System.out.println( comboBox.getSelectedItem() );
// make sure popup is closed when 'isTableCellEditor' is used
comboBox.hidePopup();
}
public static void main(String[] args)
{
final ComboBoxAction frame = new ComboBoxAction();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
If yes then it is not what i am trying for
