Question

I have a main panel using a null layout and I want to add three more panel in it but I want those three panels to automatically resize upon loading of the main panel. I cant use layout managers since I will be dragging those three panels.

I tried this code but it doesnt work

pnlGrid.setBounds(514,11, pnlMain.this.getWidth()/3, pnlMain.this.getHeight());

I also tried

pnlGrid.setPreferredSize(new Dimension(pnlMain.this.getWidth()/3, pnlMain.this.getHeight()));

but still not good. Please help. Thanks!

Was it helpful?

Solution

Use MigLayout and update the ComponentRestraints when your dragging each panel. That way you can control exactly where they are and what their bounds are.

For example:

MigLayout layout;

public Constructor(){
   layout = new MigLayout();
   ...
   container.setLayout(layout);
}

public void onMouseDrag(JPanel panel, int newX, int newY){
   layout.setComponentConstraints(panel, "pos " + newX + " " + newY + ", w 100%/3, h 100%");
   container.validate(); // probably not necessary
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top