如何让我的树形细胞填充我的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);
答案 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}}