如何从外部文件向 JComboBox 添加项目?
请我需要一些帮助来从外部文件向 JComboBox in Java 添加项目。
这是我到目前为止的代码:
//Loading the Names:
File Names_File = new File("Data" + File.separator + "Names.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String str_Data = ""; //For storing the input from the file.
try
{
fis = new FileInputStream(Names_File);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
str_Data = dis.readLine(); //Reading the line from the file.
StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.
//The below line adds only one item. The objective is adding all the items.
//*** Requesting help here ***
cmb_Name.addItem(st.nextToken("|"));
//*** Requesting help here ***
// Disposing and closing all the resources after using them.
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e)
{
System.err.println("Error: File not found!");
JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
System.err.println("Error: Unable to read from file!");
JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
主要是我的外部文件的格式如下:
James|Robert|Alice
名称之间用 "|"< 分隔/strong> 符号。
我的上面的代码在此示例中仅添加一个元素,即(“James”)。我需要将所有三个添加到循环或其他内容中。我的想法是,我不确定我的外部文件中有多少个名字。因此,使用简单的计数器没有帮助。
任何建议将不胜感激!
预先感谢您的帮助
Please i need some help in adding items to a JComboBox in Java from an external file.
Here is my code so far:
//Loading the Names:
File Names_File = new File("Data" + File.separator + "Names.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String str_Data = ""; //For storing the input from the file.
try
{
fis = new FileInputStream(Names_File);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
str_Data = dis.readLine(); //Reading the line from the file.
StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.
//The below line adds only one item. The objective is adding all the items.
//*** Requesting help here ***
cmb_Name.addItem(st.nextToken("|"));
//*** Requesting help here ***
// Disposing and closing all the resources after using them.
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e)
{
System.err.println("Error: File not found!");
JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
System.err.println("Error: Unable to read from file!");
JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
Mainly the format of my external file is as follows:
James|Robert|Alice
The names are separated by the "|" symbols.
My above code is adding only one element namely ("James") in this example. I need to add all three maybe in a loop or something. The idea is that i don't know for sure how many names will be in my external file. Thus using simple counters will not help.
Any suggestions are greatly appreciated!
Thanks in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: