java - 如何将排序的 Map 转换为 JTable 的二维数组

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

}

我还是一事无成。

最佳答案

传递您的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

关于java - 如何将排序的 Map 转换为 JTable 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36968736/

相关文章:

java - 从数组中删除重复的字符串?

java - 尽管有足够的可用内存,但巨大的数组仍会导致内存不足

java - JTextArea 到 ScrollPane 不起作用

c++ - Const 映射元素访问

python 获取引用的对象名称

c++ - 标记化字符串时出现段错误

java - HttpGet 是否自动处理 cookie?

java - 如果双击运行如何停止jetty?

java - Java Swing FocusListener的MVC实现

java - 从 Jfilechooser 检索文件名到 JList