Populating JCombobox with a text file [duplicate]

三世轮回 提交于 2019-12-31 07:17:08

问题


Possible Duplicate:
How do I populate JComboBox from a text file?

I am new to programming Java with only 2 months of experience. Can anyone help me to populate a JComboBox with a text file, consisting of 5 lines? I have looked at code on Google, but I keep getting errors.


回答1:


private void populate() {
    String[] lines;
    lines = readFile();

    jComboBox1.removeAllItems();

    for (String str : lines) {
       jComboBox1.addItem(str);
    }
}

Here is readFile(). From this site

private String[] readFile() {
  ArrayList<String> arr = new ArrayList<>();
  try {
     FileInputStream fstream = new FileInputStream("textfile.txt");
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
     String strLine;
     while ((strLine = br.readLine()) != null) {
        arr.add(strLine);
     }
     in.close();
  } catch (Exception e) {
  }
  return arr.toArray(new String[arr.size()]);
}


来源:https://stackoverflow.com/questions/14368268/populating-jcombobox-with-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!