Anak1n
Anak1n

Reputation: 149

Placing components on Jpanel

Can i combine Java layouts in same JPanel. I'm stuck with with placing my components on JPanel. It shoudl be like this: JLabel, JButton, JButton , JLabel and new line and same. I used BorderLayout but it wont go to the next row, keep adding components to same row and I need a new row. Ideal situation will be that i can use BorderLayout combined with cardlayout or some other good solution.

EDIT: Solved with GridLayout (0,4) It will do the job till i learn to use GridBaglayout. Thank you for trying to help me.

enter image description here

Upvotes: 1

Views: 264

Answers (2)

Oliver Watkins
Oliver Watkins

Reputation: 13509

Yes you can combine java layouts.

A common pattern I use is BorderLayout first on a frame. The central component expands out, while the other components shrink in. Inside these panels I might have a Flowlayout to show buttons evenly spaced horizontally on top.

Another common approach for forms is using a Gridbaglayout, then adding all the form elements at gridX and gridY positions. I then later can stretch and teak these cells using other constraints in the Gridbaglayout repetoire.

Can you add a screenshot so that we can see what you want to do?

Upvotes: 1

StuPointerException
StuPointerException

Reputation: 7267

Yes you can combine layouts.

Using a JPanel you are able to embed other JPanels:

JPanel back = new JPanel(new BorderLayout());
JPanel rows = new JPabel(new GridLayout(3,3));

back.add(rows, BorderLayout.CENTER);

Without seeing your code though it's difficult to know exactly what you are trying to achieve!

Upvotes: 1

Related Questions