简体   繁体   English

JComboBox选择更改

[英]JComboBox Selection Change

everyone i am quite new to Java GUI, i am having an issue with a JComboBox , where it is firing when i removeAllItems from a combo box to refresh it, this is an issue because i am getting the selected items Details and populating a textboxes with them so as it is firing at that point i am getting a Null Pointer. 每个人,我对Java GUI都很陌生,我遇到了JComboBox的问题,当我从组合框中删除removeAllItems以刷新它时会触发,这是一个问题,因为我正在获取选定的项Details并使用填充文本框他们,因为它在那个时候开火我得到一个空指针。 Is there any simple(ish) way to have method on the ComboBox that is called when the selected item is changed not just when the combo box contents is changed? 是否有任何简单的方法可以在ComboBox上具有方法,而不仅是在更改组合框内容时,还可以在更改所选项目时调用该方法?

Code

comboBox current method comboBox当前方法

private void customerComboActionPerformed(java.awt.event.ActionEvent evt) {                                              

   setDetails();

} 

Method for setting the Items in the combo Box 在组合框中设置项目的方法

public void setCustomers()
{
customerCombo.removeAllItems();
for (Customer curr : Main.getNewCustomerList().getCustomers())
{

    customerCombo.addItem(curr);
}
}

method for setting the details 设置细节的方法

public void setDetails()
{
Customer selected = (Customer) customerCombo.getSelectedItem();
forenameText.setText(selected.getForename());
surnameText.setText(selected.getSurname());
costperkgText.setText(String.valueOf(selected.getDeliveryCost()));
line1Text.setText(String.valueOf(selected.getColAddress().getAddressLine1()));
line2Text.setText(String.valueOf(selected.getColAddress().getAddressLine2()));
cityText.setText(String.valueOf(selected.getColAddress().getCity()));
postcodeText.setText(String.valueOf(selected.getColAddress().getPostcode()));

}

You are not accounting for the case where there is no selection. 您不考虑没有选择的情况。

public void setDetails()
{
    Customer selected = (Customer) customerCombo.getSelectedItem();
    if (selected != null)
    {
        // there is a selection so use it
    }
    else
    {
        // for example, clear the text boxes
    }
}

We would also expect that changing the contents of the combo box might change its selection so we shouldn't ignore it. 我们还希望更改组合框的内容可能会更改其选择,因此我们不应忽略它。

I like to set a flag. 我喜欢设置一个标志。 The key is making sure the flag doesn't get stuck to false. 关键是要确保该标志不会陷入错误。

private volatile boolean fire = true;

public void setItems(Object[] items) {
    try {
        fire = false; // Don't fire updates
        updateItems(items);
    } finally {
        fire = true; // always reset no matter what!
    }
}

private JComboBox create() {
    JComboBox cb = new JComboBox();
    cb.addActionListener( new ActionListener() {
        public void actionPerformed( ActionEvent e ) {
            if(fire) {
                notifyListeners(); 
            }
        }
    });
}

You have to make sure this isn't being called by multiple threads, but since Swing isn't thread safe this should be happening anyway. 您必须确保不会被多个线程调用,但是由于Swing不是线程安全的,因此无论如何都应该发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM