John
John

Reputation: 838

How to set the size of a panel inside a panel with null layout?

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!

Upvotes: 1

Views: 171

Answers (1)

rtheunissen
rtheunissen

Reputation: 7435

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
}

Upvotes: 2

Related Questions