在JTree的节点内启用滚动

时间:2014-02-17 15:39:00

标签: java swing jtable jscrollpane jtree

我正在尝试构建一个列出错误的调试GUI,同时对它们进行分类以获得更好的概述。因此,我正在构建一个JTree JTables作为树叶。由于表需要一些空间,因此需要JScrollPane。现在显示此功能或多或少,但我无法在表格内滚动。

CellRenderer

private static class MyTableCellRenderer implements TreeCellRenderer {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        Object v = ((DefaultMutableTreeNode) value).getUserObject();
        if (v instanceof JTable) {
            JPanel panel = new JPanel();
            panel.add(((JTable) v).getTableHeader());
            panel.add(new JScrollPane((JTable) v));
            return panel;
        }
        return new JLabel(v.toString());
    }
}

Panel&树:

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);

DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("root");
JTree tree = new JTree(parentNode);
tree.setCellRenderer(new MyTableCellRenderer());
DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode(new JTable(new Object[0][], new Object[] {"col1"}));
//add some content
parentNode.add(tableNode);

有关如何在表格内滚动的任何建议吗?

0 个答案:

没有答案