0

I am populating jComboBox1 from data.txt file. I am using this code to populate jComboBox1.

The structure of text file as follows:

AAA 123456 BB                     22 Some text
AAA 234567 CC                     23 Some text
AAA 345678 DD                     24 Some text

The goal that I want to achieve is display in jComboBox1 not all data, just certain column of data.

I found that substring can be appropriate choice.

For example as shown in below code, in jComboBox1 I want to show data with position substring(12,17) + substring(58,97).

     String data = strLine.substring(12,17);
     String data2 = strLine.substring(58,97);
     System.out.println(data + " " + data2);

But unfortunately I don't know how to achieve this goal.

Any help will be appreciated.

Community
  • 1
  • 1
  • Exactly which bits of each line are you trying to extract? Also, the lines are only 46 characters long, so `strLine.substring(58,97)` will throw an error. – moarCoffee May 07 '15 at 10:03
  • @moarCoffee There I showed just sample of text file and without full line. But in my text file lines above 100. –  May 07 '15 at 10:14
  • Ah I see. Do the lines always have the same length/same format? If so, what's wrong with using `substring()' to get the bits you want? – moarCoffee May 07 '15 at 10:18
  • @moarCoffee Length is same. Problem is I don't know how to show exctracted column in JComboBox. –  May 07 '15 at 10:25

1 Answers1

0

Using the code which you linked to, you could alter it to do something like this:

private void populateCols(int a1, int a2, int b1, int b2) {
    String[] lines;
    lines = readFile();
    jComboBox1.removeAllItems();

    for (String str : lines) {
       jComboBox1.addItem(str.substring(a1, a2) + " " + str.substring(b1, b2));
    }
}
moarCoffee
  • 1,289
  • 1
  • 14
  • 26