开发者

itemlistener gets event after focus is removed from jcombobox

This must be a misunderstanding from my side, but I have the following:

I have added an ItemListener to a Jcombobox.

In the item listener, I check in the event if it is of type ItemSelected.

If it is, I update a value in a JTextPane. The problem is that it works as follows:

I click on a new value in the jcombobox and nothing is changed in the jtextfield. I have to actually click on another component e.g. the jtextfield and then the jtextfield is u开发者_如何学Cpdated.

It seems that the focus must be removed from the jcombobox so that the event change is dispatched to the itemlistener code.

Is this how it is supposed to work, or am I doing something wrong? Is it possible to handle the event without needing to remove focus?

UPDATE: This my code and the method updateJTextPane is called after I click on another component an not when I select a new value from the combo. I.e. combo has value "1", I click on the drop-down list and click on "2". Current selected item is now "2". My method is not being called at this point. I click on a second combo box, second combo has focus and then my method updateJTextPane() is called. Why?

UPDATE 2:

//Code from Netbeans generator
JComboBox myCbx = new javax.swing.JComboBox();    
myCbx.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
myCbx.setName("myCbx"); // NOI18N 
//My ItemListener
class myItemListener implements ItemListener{
public void itemStateChanged(ItemEvent ie) {
            if (ie.getStateChange() == ItemEvent.SELECTED) { // Item was just selected
                updateJTextPane();
            }    
         }
    }
//add item listener to combo
myCbx.addItemListener(new myItemListener());

UPDATE 3:Inner class myItemListener is added to 5 more comboboxes in the same JDialog if this matters in a way I do not know Thank you


This is not how it is supposed to work. Item events should be generated immediately whenever the selected item changes.

I suggest to start with a simple example such as this one and see if events are actually being generated. If this works, then you only need to search for the differences between this and your own code.

Update:

Your itemStateChanged method looks fine, the problem must be elsewhere. Perhaps there is something wrong with updateJTextPane. What happens if you replace the call to updateJTextPane with a System.out.println? Also, can you print out the source of the event (ie.getSource()) and verify that the event is actually coming from the first combo box?

If you post a self-contained example that can be compiled and run, it should be much easier to pinpoint the problem.


It should work as you described it, the following runs just fine:

class Frame extends JFrame {
    JComboBox  box;
    JTextField field;
    String[]   entries = { "one", "two", "three" };

    Frame() {
        setLayout(new FlowLayout());

        box = new JComboBox(entries);
        box.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    field.setText((String) box.getSelectedItem());
                }
            }
        });
        add(box);

        field = new JTextField();
        field.setColumns(10);
        add(field);

        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Frame frame = new Frame();
            }
        });

    }
}


Is it possible to handle the event without needing to remove focus? Sure it is! :)

Here's a sample of how it could work, assuming my understanding of your requirements is correct:

class ComboListener implements ItemListener {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println("Selected Item: \""
                + ((JComboBox)e.getSource()).getSelectedItem() + "\"");
        }
    } 
}

If you don't add the if check, you are going to perform the action for both the first item being unselected and the second one being selected.


I have encountered the similar situation on Choice. To me it more likes a bug in Java. My case is I have two Choices A and B. B depends on A's selection. For example, A={a1, a2, a3}. If A=a1 B is a Choice list of {1, 2, 3}. If A=a2, B is a list of {4,5,6}., If A= a3, B is a list of {7,8,9}. The flow is select a1 and then select 2 on B. Select a2 and B is in default index 0 (4) and select 5 on B, the itemStateChanged() funcation will not be called. It looks like the view control does not sync with the data set to it. The reason the itemStateChanged() is not called since the 5 has the same index of previous selection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜