gpt4 book ai didi

java - 如何将文本文件中的ArrayList填充到JCombobox

转载 作者:行者123 更新时间:2023-12-02 05:46:23 26 4
gpt4 key购买 nike

我试图在单击时将 ArrayList 传递给 JComboBox

这是图片,我也想知道如何获取“:”之后的信息,所以我进行计算,例如1 USA:1.02 -> 当选择 ID 1 时获取 :1.02 之后的值,然后根据用户在文本字段 1 中输入的值计算该值以填充结果。

http://i57.tinypic.com/i54v0n.png

这是代码:

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

try{
//File reader method
FileReader file = new FileReader("/Users/MacbookDev/Desktop/countryrates.txt");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while (line != null)
{
text += line;
line = reader.readLine();

}
cbCountry.addItem(text);

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

最佳答案

    while (line != null)
{
text += line;
line = reader.readLine();

}
cbCountry.addItem(text);

不要创建文件中所有文本的字符串。您需要将每行文本作为单独的项目添加到组合框中:

    while (line != null)
{
cbCountry.addItem(line);

line = reader.readLine();

}

另外,如果你想在组合框中存储多条数据,那么你需要为数据创建一个自定义对象,然后创建一个自定义渲染器来显示数据。请参阅Combo Box With Custom Renderer了解更多信息和示例。

关于java - 如何将文本文件中的ArrayList填充到JCombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com