【问题标题】:How to convert from a sorted Map to a two dimensional array for JTable如何将已排序的 Map 转换为 JTable 的二维数组
【发布时间】:2016-05-01 16:00:38
【问题描述】:

我有一个包含以下信息的文本文件:

dog
sheep
cat
horse
cat
tiger
cat
cat
tiger

在另一个Q&A,我问人们如何计算所有动物的出现次数,我用这种方法做到了:

void countAnimals(){
    Map m1 = new HashMap();

    try (BufferedReader br = new BufferedReader(new FileReader("Animals.txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            String[] words = line.split(" ");//those are your words
            for (int i = 0; i < words.length; i++) {
                if (m1.get(words[i]) == null) {
                    m1.put(words[i], 1);
                } else {
                    int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
                    newValue++;
                    m1.put(words[i], newValue);
                }
            }
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Map<String, String> sorted = new TreeMap<String, String>(m1);
    for (Object key : sorted.keySet()) {
        System.out.println(key + "\t Votes: " + m1.get(key));
    }
}

这是控制台打印出来的:

cat  Votes: 4
dog  Votes: 1
horse    Votes: 1
sheep    Votes: 1
tiger    Votes: 2

谁能帮我把现在打印到控制台的信息放入一个二维数组中,我将把它放入JTable

更新:这是我添加到 countAnimals 方法中的内容:

Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
    System.out.println(key + "\t Votes: " + m1.get(key));
}
AnimalSummaryModel SOCLOSE = new AnimalSummaryModel(sorted);
SOCLOSE.run();

您提供的AnimalSummaryModel 类如下所示:

class AnimalSummaryModel extends AbstractTableModel {

    private final String[] keys;
    private Map<String, String> data;

    public AnimalSummaryModel(Map<String, String> data) {
        this.data = data;
        keys = data.keySet().toArray(new String[data.size()]);
    }

    public int getColumnCount() {
        return 2;
    }

    public int getRowCount() {
        return data.size();
    }

    public Object getValueAt(int row, int col) {
         if (col == 0) {
             return keys[row];
         } else {
             return data.get(keys[row]);
         }
    }

    private void display() {
        JFrame f = new JFrame("EnvTableTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(f, this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public void run() {
        display();
    }

}

我还是一无所获。

【问题讨论】:

  • 为什么要将数据推送到二维数组而不保存在地图中?
  • 我认为这是我可以将所有信息放入 jtable 的唯一方法。可以直接从地图添加吗?
  • 如果有 jtable 代码可以粘贴一下吗?
  • 对不起,我没有

标签: java swing dictionary multidimensional-array jtable


【解决方案1】:

将您的Map&lt;String, String&gt; 作为参数传递给自定义TableModel,并使用该模型构造您的JTable。一个完整的例子显示在here

Map<String, String> sorted = new TreeMap<>();
data.put("cat", "Votes: 4");
data.put("tiger", "Votes: 2");
data.put("horse", "Votes: 1");
data.put("sheep", "Votes: 1");
data.put("dog", "Votes: 1");
JTable table = new JScrollPane(new JTable(new AnimalSummaryModel(sorted)));
…
private static class AnimalSummaryModel extends AbstractTableModel {

    private final Map<String, String> data;
    private final String[] keys;

    public AnimalSummaryModel(Map<String, String> data) {
        this.data = data;
        keys = data.keySet().toArray(new String[data.size()]);
    }
    …
}

【讨论】:

  • 你成功了,太好了!我正在尝试实现您的代码,但似乎还不能正常工作。
  • @Gediminas:对于data 参数,我传入了一个TreeMap,您将其命名为sorted。很抱歉造成混乱。
  • @Gediminas:从引用的示例开始,并进行所示的更改;如果您有问题,请编辑您的问题以包含一个minimal reproducible example,以显示您当前的方法。
  • 为什么不让countAnimals() 返回Map&lt;String, String&gt;
  • 好的,让我试试
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 2011-07-05
  • 2019-07-09
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多