显示 jcombobox 中特定列的文本文件项

display the text file item from certain column in jcombobox

我正在从 data.txt 文件填充 jComboBox1。我正在使用 this code 来填充 jComboBox1

文本文件结构如下:

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

我想要实现的目标是在jComboBox1中显示不是所有数据,只是特定列的数据。

我发现 substring 是合适的选择。

例如如下代码所示,在jComboBox1中我想显示位置为substring(12,17) + substring(58,97)的数据。

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

但不幸的是我不知道如何实现这个目标。

任何帮助将不胜感激。

使用您链接到的 code,您可以将其更改为执行以下操作:

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));
    }
}