I am getting an exception: java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint when I attempt to execute this code:
//creating the right splitpane
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
GridBagLayout paneLayout = new GridBagLayout();
sp.setLayout(paneLayout);
sp.setContinuousLayout(true);
sp.setDividerLocation(100);
//setting constraints
c = this.setConstraints(GridBagConstraints.ABOVE_BASELINE_TRAILING, GridBagConstraints.NORTH, 1, 1, 2, 2, .5, .5, new Insets(1,1,1,1), 5, 5);
paneLayout.setConstraints(treeView, c);
c = this.setConstraints(GridBagConstraints.BELOW_BASELINE_TRAILING, GridBagConstraints.SOUTH, 0, 0, 2, 2, .5, .5, new Insets(1,1,1,1), 5, 5);
paneLayout.setConstraints(info, c);
//adding components
sp.setTopComponent(treeView); // Line with the error
sp.setBottomComponent(info);
Where setConstraints does this:
private GridBagConstraints setConstraints(int fill, int anchor, int gheight, int gwidth, int x, int y, double d, double e, Insets insets, int padx, int pady){
GridBagConstraints c = new GridBagConstraints();
c.fill = fill;
c.anchor = anchor;
c.gridheight = gheight;
c.gridwidth = gwidth;
c.gridx = x;
c.gridy = y;
c.weightx = d;
c.weighty = e;
c.insets = insets;
c.ipadx = padx;
c.ipady = pady;
return c;
}
I figure I am either missing something simple, or there is a much larger bug that I can’t do anything about. What say you?
MirroredFate
JSplitPane has its own layout manager– you shouldn’t change it to GridBagLayout. If you want to use GridBagLayout in the panes, create a JPanel to put in the JSplitPane, and set the layout of that panel to GridBagLayout. Then you put the panel in the JSplitPane, and the controls in the panel.