如何让我的TreeCellRenderer填充我的JTree?

时间:2013-02-25 20:30:48

标签: java swing jpanel jtree renderer

如何让我的树形细胞填充我的JTree宽度:

JTree tree = new JTree();
tree.setCellRenderer(new TreeCellRenderer() { 
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean selected, boolean expanded, boolean leaf, int row, 
        boolean hasFocus) {
            // TODO: How do I make this panel fill the width 
            // of the JTree?
            JPanel panel = new JPanel();
            panel.add(new JLabel("ITEM");
            return panel;
        }
});
add(tree);

1 个答案:

答案 0 :(得分:4)

  • JPanel panel = new JPanel();JPanel是容器,

  • 默认情况下,
  • JPanel会返回zero Dimension

  • 只有空JPanel(仅限AFAIK)才会在10px中返回BorderLayotu,如果JPanels放置在NORTH,SOUTH,WEST,SOUTH区域,则会创建非常好的10pxs EmptyBorders

  • 必须覆盖getPreferredSize的{​​{1}},方法与在Swing中绘画所需的方式相同

  • 不知道为什么你将JPanel JPanel作为Renderers JComponent添加到JTree中,也许还有其他方式,确保更快地发布{{} {1}}