JBTreeTable - how to set column width

Answered

Hi,

I'm trying to use JBTreeTable in the plugin that i'm trying to develop for intellij platform. I think i'm nearly there, but I cant figure out why the column is not rendered according to the preferred width that i set in the code.

Here is my snippet:

public class Sample {
    private JPanel contentPane;
    private JButton processButton;
    private JButton clearButton;
    private JTextArea fixMessageTextArea;
    private JPanel fixTreeContainer;


    private static class CustomNode extends DefaultMutableTreeNode {
        String name;
        Object value;

        CustomNode(String name, Object value) {
            this.name = name;
            this.value = value;
        }

        public Object getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    public Sample() {
        ColumnInfo name = new ColumnInfo("Name") {
            @Nullable
            @Override
            public Object valueOf(Object o) {
                if (o instanceof CustomNode) {
                    return ((CustomNode) o).getName();
                } else {
                    return o;
                }
            }
        };

        ColumnInfo value = new ColumnInfo("Value") {
            @Nullable
            @Override
            public Object valueOf(Object o) {
                if (o instanceof CustomNode) {
                    return ((CustomNode) o).getValue();
                } else {
                    return o;
                }
            }
        };

        final ColumnInfo[] columns = {name, value};
        final ListTreeTableModel model = createTableModel(columns);
        final JBTreeTable treeTable = new JBTreeTable(model);

        treeTable.getTable().setAutoResizeMode(JBTable.AUTO_RESIZE_OFF);
        treeTable.getTable().getColumnModel().getColumn(0).setPreferredWidth(200); // width in pixels for column 1
        treeTable.getTable().getColumnModel().getColumn(1).setPreferredWidth(400); // width in pixels for column 1
        treeTable.getTree().setCellRenderer(new DefaultTreeCellRenderer());

        fixTreeContainer.add(treeTable,
                new GridConstraints(0, 0, 1, 1,
                        GridConstraints.ANCHOR_CENTER,
                        GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50),
                        null, 0, false));
    }

    @NotNull
    private static ListTreeTableModel createTableModel(ColumnInfo[] columns) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        ListTreeTableModel model = new ListTreeTableModel(root, columns);
        DefaultMutableTreeNode first = new CustomNode("I am first name", "I am first value");
        DefaultMutableTreeNode second = new CustomNode("I am second name", "I am second value");
        root.add(first);
        root.add(second);
        DefaultMutableTreeNode child1 = new CustomNode("I am child namel", "I am child value");
        DefaultMutableTreeNode child2 = new CustomNode("I am child name2", "I am child value");
        DefaultMutableTreeNode child3 = new CustomNode("I am child name3", "I am child value");
        second.add(child1);
        second.add(child2);
        second.add(child3);
        return model;
    }

    public JPanel getContentPane() {
        return contentPane;
    }

and then in my code for the plugin:

public class FixParserToolWindowFactory implements ToolWindowFactory, DumbAware {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        final FixParserWindowContent windowContent = new FixParserWindowContent(toolWindow);
        final Content content = ContentFactory.getInstance().createContent(windowContent.getContentPanel(), "", false);
        toolWindow.getContentManager().addContent(content);
    }

        public FixParserWindowContent(ToolWindow toolWindow) {
            contentPanel.setLayout(new BorderLayout(0, 20));
            contentPanel.setBorder(BorderFactory.createEmptyBorder(40, 0, 0, 0));
            contentPanel.add(new Sample().getContentPane(), BorderLayout.CENTER);
   
        }
  ...
  }      

with the code above, it looks like this:

 

Have i missed anything obvious? Also, is there a documentation on these widgets ? E.g. when i first started writing the plugin, I was using TreeTable (as opposed to JBTreeTable), and I couldnt figure out why the expand/collapse icon wasnt showing! Maybe a sample demo code in the jetbrains repo that shows how these components can be used?

Also, what do people usually use to develop the UI? for the above, i use the UI designer plugin in intellij, but it doesnt recognise all these widgets from Jetbrains. 

 

Thank you!

0
3 comments

Hi Alex,

Please try overriding com.intellij.util.ui.ColumnInfo#getWidth.

0

Hi Karol,

Thank you for getting back. I’m quite sure I’ve also tried that approach, and it didn’t work (not shown in the snippet above). But can I ask, from the screenshot above, it looks like there are three columns on the table ? So the first column is essentially used as a container for the tree nodes (while the other columns are from my column definition )? In that case, how do I set the width on the column that contains the tree nodes ? Because what I’ve done (and the approach that didn’t work), is to set the width on the other two columns to be something like 800.

0

Hi Alex,

Sorry for delayed response.

There is no documentation of this component except what's in the class.

The JBTreeTable component is split into two parts: tree and table (accessible via getTable() and getTree()). When changing the size of a column at index zero, the table part of the component is changed.

Anyway, looking at the class, I suggest trying to use treeTable.setColumnProportion() (setting it to, e.g., 0.33). It is 0.1 by default, which seems to be reflected on your screenshot. Tree and table are split with OnePixelSplitter, which will receive this value and adjust tree and table sizes according to the value.

0

Please sign in to leave a comment.