简体   繁体   English

如何在 JComboBox 中显示数据库中的所有值?

[英]How to display all the value from database in a JComboBox?

This code only display only one va, and repeat many times.这段代码只显示一个va,并且重复多次。 Hope someone can help me.希望可以有人帮帮我。 Thanks a lot.非常感谢。

private void originComboBox() {

        scheControl = new ScheduleControl();
        Schedule schedule = scheControl.getRecord();
        String origin = schedule.getDeparture();

        for(int i = 0; i < origin.length(); i++){
            originCB.addItem(origin);
        }       
    }

You are adding the same origin each time.您每次都添加相同的来源。 Shouln't it be origin[i] ?不应该是 origin[i] 吗?

for(int i = 0; i < origin.length(); i++){
    originCB.addItem(origin[i]);
} 
String origin = schedule.getDeparture();
  • Your getting single string only.你只得到一个字符串。
  • you have to get array of values.你必须得到值的数组。 Do
  • the modification than you will get result as you need.修改比你会得到你需要的结果。 you can use您可以使用
  • for-each loop to check the values of array. for-each 循环来检查数组的值。

you could try this你可以试试这个

 for(int i = 0; i < origin.length(); i++){
    originCB.addItem(origin[i]);
} 

where origin should be Array .其中 origin 应该是 Array 。

    String origin[]={"A","B","C"};

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

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