简体   繁体   English

如何防止我的jComboBox重置?

[英]How can I prevent my jComboBox from resetting?

I have a ResultSet . 我有一个ResultSet This ResultSet is filling a JComboBox with data like this: ResultSet使用以下数据填充JComboBox

DatabaseHandler dh = new DatabaseHandler();

public ResultSet Klanten;

public BestellingenNieuwPanel() {
    initComponents();
    //Removes all items from KlantBox.
    KlantBox.removeAllItems();
    //Removes all from ProductBox.
    ProductBox.removeAllItems();
        try {
        Klanten = dh.getDataFromDB("select * from klanten");
        while (Klanten.next()) {
            String strKlanten = Klanten.getString("Achternaam");
            KlantBox.addItem(Klanten.getString(3));
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

This workes just fine. 这很好用。 But, whenever I try to loop through ResultSet Klanten again like this: 但是,每当我尝试再次遍历ResultSet Klanten时,就像这样:

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

    if(Klanten != null){
        String strSelected = KlantBox.getSelectedItem().toString();            
        try {
            while(Klanten.next()){
                String KlantVoornaam = Klanten.getString(1);
                String KlantAchternaam = Klanten.getString(2);
                String KlantWoonplaats = Klanten.getString(4);
                if(strSelected == KlantAchternaam){
                    txtKlantNaam.setText(KlantAchternaam);
                    txtKlantWoonplaats.setText(KlantWoonplaats);
                }
            }
        } catch (SQLException ex) {
        }

    }
}      

My JComboBox Klanten only has it's last value. 我的JComboBox Klanten仅具有最后一个值。 I've fiddled around with the Klanten.next() function and found that that function is the source of the problem. 我摆弄了Klanten.next()函数,发现该函数是问题的根源。

Are there any other ways to loop through a ResultSet ? 还有其他方法可以遍历ResultSet吗? Or is there a way to loop through a ResultSet WITHOUT resetting my JComboBox ? 还是有一种方法可以遍历ResultSet无需重置JComboBox

Don't compare strings like this if(strSelected == KlantAchternaam){ use equals() instead. 不要比较这样的字符串if(strSelected == KlantAchternaam){使用equals()代替。

But in fact after refreshing the combobox just call setSelectedItem(strSelected ) 但实际上在刷新组合框后,只需调用setSelectedItem(strSelected )

I think the error is with your comparison of string using == rather than with your use of Klanteen.next() 我认为错误是您使用==比较字符串而不是您使用Klanteen.next()

Try using the following code 尝试使用以下代码

if(strSelected.equals(KlantAchternaam)){
                txtKlantNaam.setText(KlantAchternaam);
                txtKlantWoonplaats.setText(KlantWoonplaats);
            }

or this one, if you want to ignore case 还是这个,如果你想忽略大小写

if(strSelected.equalsIgnoreCase(KlantAchternaam)){
                txtKlantNaam.setText(KlantAchternaam);
                txtKlantWoonplaats.setText(KlantWoonplaats);
            }

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

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