如何将有序映射转换为JTable的二维数组

时间:2016-05-01 16:00:39

标签: java swing dictionary multidimensional-array jtable

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

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

}

我仍然无处可去。

1 个答案:

答案 0 :(得分:2)

将您的Map<String, String>作为参数传递给自定义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()]);
    }
    …
}

image