5

如何使我的树单元格填充我的 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);
4

1 回答 1

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

  • JPanel默认为空返回zero Dimension,

  • 只有空的JPanel(仅限AFAIK)返回10pxBorderLayotu如果JPanels被放置到NORTH,SOUTH,WEST,SOUTH区域,然后创建非常漂亮的10pxsEmptyBorders

  • 必须覆盖getPreferredSizefor JPanel,与在 Swing 中绘画所需的方式相同

  • 不知道为什么你把JPanelasRenderers JComponent放进去JTree,也许有/还有另一种方式,肯定会尽快发布更好的帮助SSCCE

于 2013-02-25T22:12:02.340 回答