[英]How to populate ArrayList in text file to JCombobox
I am trying to pass an ArrayList
to a JComboBox
when it is clicked. 我正在尝试将
ArrayList
传递给JComboBox
。
Here are the pictures, and I also want to how to get and information after ":" so i make calculation eg 1 USA:1.02 -> get the value after :1.02 when ID 1 is selected then calculate the value from the users input from the text field 1 to populate the result. 这是图片,我也想获取“:”之后的信息,因此我进行了计算,例如1 USA:1.02->当选择ID 1时,获取:1.02之后的值,然后根据用户输入的值来计算文本字段1以填充结果。
This is the code : 这是代码:
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);
Don't create a string of all the text in the file. 不要在文件中创建所有文本的字符串。 You need to add each line of text to the combo box as a separate item:
您需要将文本的每一行作为单独的项目添加到组合框中:
while (line != null)
{
cbCountry.addItem(line);
line = reader.readLine();
}
Also, if you want to store multiple pieces of data in the combo box, then you need to create a custom Object for the data and then create a custom renderer to display the data. 另外,如果要在组合框中存储多个数据,则需要为数据创建一个自定义对象,然后创建一个自定义渲染器以显示数据。 See Combo Box With Custom Renderer for more information and examples.
有关更多信息和示例,请参见带有自定义渲染器的组合框 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.