Note that there are some explanatory texts on larger screens.

plurals
  1. POJTree: variable row height
    primarykey
    data
    text
    <p>I'd like my JTree to have variable row heights. From what I've read, all I have to do is <code>setRowHeight(0)</code> and implement a custom <code>TreeCellRenderer</code>. Inside the TreeRenderer different borders are set, depending on the item. however, when a border is added, row height still remains the same. For example, when an <code>EmptyBorder(0,0,8,0)</code> is added, the label moves to the top by 8 pixels. So instead of the expected result on the left, the tree looks something like on the right: </p> <pre><code> EXPECTED: RESULT: _ _ |-|_| Item 1 |-|_| Item 1 | _ | |_| Item 2 |-|_| Item 2 |- | | _ | _ |-|_| Item 3 |-|_| Item 3 </code></pre> <p>This is essentially my code: </p> <pre><code>JTree tree = new MyTree(root); class MyTree extends JTree{ public MyTree(DefaultMutableTreeNode root){ super(root); setCellRenderer(new CustomCellRenderer()); setRowHeight(0); } class CustomCellRenderer extends DefaultTreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) { JComponent c = (JComponent) super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus); Data d = (Data)((DefaultMutableTreeNode) value).getUserData(); int bttm = 0; if(d.isTrue()){ bttm = 8; } c.setBorder(BorderFactory.createEmptyBorder(0, 0, bttm, 0)); //setting size didn't show any effect either: //c.setPreferredSize(new Dimension(150, 16+bttm)); return c; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload