문제

I'd like my JTree to have variable row heights. From what I've read, all I have to do is setRowHeight(0) and implement a custom TreeCellRenderer. 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 EmptyBorder(0,0,8,0) 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:

  EXPECTED:          RESULT:
     _                 _
  |-|_| Item 1      |-|_| Item 1
  |  _              | |_| Item 2  
  |-|_| Item 2      |-
  |                 |  _
  |  _              |-|_| Item 3
  |-|_| Item 3      

This is essentially my 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; 
    }

}
도움이 되었습니까?

해결책

You assumption is wrong, the row height did not stay the same. The row height is the distance between the top and the bottom of the row, in other words the occupied space between the rows above and beneath it. Your sketch clearly shows a raised distance, and that’s what really happens.

The problem with your expectation is a completely different: you expect the handle to the left of the cell to move upwards staying at the center of the text but the handle remains at the center of the entire cell, including the empty border you have added.

As of Java 7 and BasicLookAndFeel and its descendants (like Windows and Metal) the vertical centering of the handle line is hardcoded in the implementation.

So if you want the handle to stay at the center of the text the only practicable solution is to keep the text centered by adding the empty space equally to the top and the bottom.

다른 팁

You can try to use html inside rendered component for get that effect without Border or setRowHeight(), or setPreferredSize(). Here is your renderer:

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);
            if (c instanceof JLabel) {
                ((JLabel) c).setText("<html><p style=\"padding-top: 8px;\">" + value + "<p/>");
            }
            return c;
        }

    }

enter image description here

padding-bottom instead padding-top for that effect under name.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top