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 - >得到之后的值:1.02当选择ID 1时,然后计算用户输入的值文本字段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);
}
1 个解决方案
#1
2
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.
此外,如果要在组合框中存储多个数据,则需要为数据创建自定义对象,然后创建自定义渲染器以显示数据。有关更多信息和示例,请参阅具有自定义渲染器的组合框。
#1
2
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.
此外,如果要在组合框中存储多个数据,则需要为数据创建自定义对象,然后创建自定义渲染器以显示数据。有关更多信息和示例,请参阅具有自定义渲染器的组合框。