Put JTable in the JTree

Get rid of the scrollPane, it’s dysfunctional anyway (so far I agree with Russell 🙂 and add the table and its header to the panel, using an appropriate LayoutManager:

public MyTableInTreeCellRenderer() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    table = new JTable();
    add(table.getTableHeader());
    add(table);
}

you’ll probably need to tweak the visuals a bit, the left and top border lines are missing – not entirely sure which component paints them normally, could be the scrollPane

Edit

Forgot: the reason querying the scrollPane’s prefSize in calculating the required size (done in the ui delegate, namely the VariableHeightLayoutCache) of the rendering component is that the scrollPane not yet configured with the header. The query happens before the panel is added to the rendererPane, the complete configuration is done in the table’s addNotify which happens only after adding the panel to the hierarchy

Leave a Comment