【问题标题】:display the text file item from certain column in jcombobox显示 jcombobox 中某列的文本文件项
【发布时间】:2015-05-07 09:54:14
【问题描述】:

我正在从 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);

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

我们将不胜感激。

【问题讨论】:

  • 您要提取每行的哪些位?另外,这些行只有 46 个字符,所以strLine.substring(58,97) 会抛出一个错误。
  • @moarCoffee 我只展示了文本文件的样本,没有完整的行。但是在我的文本文件中超过 100 行。
  • 啊,我明白了。这些行是否总是具有相同的长度/相同的格式?如果是这样,使用 `substring()' 获取你想要的位有什么问题?
  • @moarCoffee 长度相同。问题是我不知道如何在 JComboBox 中显示提取的列。

标签: java string swing text-files jcombobox


【解决方案1】:

使用您链接到的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));
    }
}

【讨论】:

  • 非常感谢。有用。你的回答被接受了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多