0

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.

Community
  • 1
  • 1
Andrew
  • 43
  • 6
  • 1
    *"I have tried many codes on google but always giving me compile errors."* It would be better to solve those errors. *"Please help."* Please ask a (specific) question. – Andrew Thompson Jan 16 '13 at 21:36
  • 1
    i.e. Put your code that causes error –  Jan 16 '13 at 21:42

1 Answers1

3
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()]);
}