I fail to see how to make a JPanel with SpringLayout properly calculate its preferred size, as currently it is always (0, 0). I tried calling panel.setPreferredSize(layout.preferredLayoutSize(panel));
, however it doesn`t change anything. I also tried putting constraints (commented out in the code below) on panel's south and east sides comparatively to components that are the most south / east, and while it starts giving me right preferred size, the GUI gets absolutely messed up when resizing JFrame (e.g. some components shift out of view, overlap each other etc.).
This is the class that is giving me issues:
public class TileView extends JPanel {
private TilesController controller;
private JRadioButton paint, select;
private JPanel prototype_panel, selection_panel;
public TileView (TilesController tiles_controller) {
controller = tiles_controller;
SpringLayout layout = new SpringLayout();
setLayout(layout);
prototype_panel = new TileBrushPanel(controller);
selection_panel = new TileSelectionPanel(controller);
paint = new JRadioButton("draw");
paint.addActionListener(l -> controller.setPaintTileMode());
select = new JRadioButton ("select");
select.addActionListener(l -> controller.setSelectMode());
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(paint);
bGroup.add(select);
layout.putConstraint(NORTH, paint, 4, NORTH, this);
layout.putConstraint(WEST, paint, 4, WEST, this);
add (paint);
layout.putConstraint(NORTH, select, 0, NORTH, paint);
layout.putConstraint(WEST, select, 4, EAST, paint);
add (select);
layout.putConstraint(NORTH, prototype_panel, 4, NORTH, paint);
layout.putConstraint(WEST, prototype_panel, 4, WEST, this);
add (prototype_panel);
layout.putConstraint(NORTH, selection_panel, 0, NORTH, prototype_panel);
layout.putConstraint(WEST, selection_panel, 4, EAST, prototype_panel);
add (selection_panel);
//layout.putConstraint(SOUTH, this, 4, SOUTH, prototype_panel);
//layout.putConstraint(EAST, this, 0, EAST, selection_panel);
setPreferredSize(layout.preferredLayoutSize(this));
}
}