ev00l
ev00l

Reputation: 23

Java GridLayout

Is there a way to add elements to a gridlayout that adds them horizontally instead of vertically? Basically i want to fill up an entire row with elements before it begins with the next.

Hope i made my question clear.

Upvotes: 0

Views: 7976

Answers (5)

jonescb
jonescb

Reputation: 22811

I would suggest GridBagLayout and GridBagConstraints

It's a lot like a GridLayout, but with GridBagConstraints you can specify the x and y coordinates of the component, and you can do column and row spans.

It would look something like this:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

JPanel panel = new JPanel();
JLabel label = new JLabel("foo");

panel.setLayout(layout);

c.gridx = 0;
c.gridy = 0;
panel.add(label, c);

Upvotes: 2

camickr
camickr

Reputation: 324197

I assume you meant to say that you want to fill up all the rows in a column before moving to the next column. If so, then use a custom layout manager

VerticalGridLayout

Upvotes: 0

Chad Okere
Chad Okere

Reputation: 4578

Actually you should use GroupLayout It's new (since jdk 1.6) and pretty awesome. It gives you a ton of flexibility in layout.

Upvotes: 0

Jared Russell
Jared Russell

Reputation: 11372

The GridLayout, as the name suggests lays out components according to the number of columns and rows you specified in the constructor, and will move to the next row as soon as you have added the specified number of components.

From your question, it seems that a FlowLayout is more along the lines of what you're looking for.

Edit: I'm not sure why, but if you specify the number of rows as 0 (e.g. new GridLayout(0, 9), it seems to work properly.

Upvotes: 0

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45364

That is how GridLayout works.

Upvotes: 6

Related Questions