Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fill a GridLayout vertically instead of horizontally

 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, I have a question wich I can't figure out:

When I fill a Gridlayout with buttons through iteration, it starts filling the grid topleft, then fills untill topright and then goes to the second row and fills the grid horizontally again.

Is it possible to fill the grid starting in the topleft collumn and then going down until the bottom of the grid and then the second row topdown?

I hope you understand my question!

Thanks again for your time and effort,

Patrick
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check out the ComponentOrientation class and also applyComponentOrientation method of java.awt.Container class which is inherited by JComponent.

Please note that I haven't tried this though
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:You can check out the ComponentOrientation class and also applyComponentOrientation method of java.awt.Container class which is inherited by JComponent.

Please note that I haven't tried this though


Thanks for the quick reply and sorry my late reply.

I've studied your advise, but I can't use it for my problem. ComponentOrientation is used to check if text is entered left to right, or right to left. I can't use it to set things, it only comes with getter methods.


After reading a little bit more, my ComponentOrientation should be TL = Mongolian. see: http://www.science.uva.nl/ict/ossdocs/java/jdk1.3/docs/api/java/awt/ComponentOrientation.html

I just don't know how to specify TL as a parameter for the applyComponentOrientation();
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After looking at the API documentation I would have to say "No".
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's dissapointing. Because I have 4 arrays of buttons that I want to add to one gridlayout. If I could add the buttons vertically, they all would be in the exact right order.

Is it possible to assign the first column of the gridlayout to the first button[], column 3 and 4 to the second button[] and so on? That would work for me as well.

Thanks for your time!

Patrick
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then write your own LayoutManager which does that. It might be easier than you think to write a layout manager, I've written one myself.

Or use more than one layout. From your casual description let's try this: make a GridLayout which has 1 column and N rows (whatever N might be). Then create a JPanel using that layout and fill it with one column of your data. Repeat for each of the M columns you need (M = 4?) and then you have M JPanels. Create another JPanel with a GridLayout which has 1 row and M columns, and fill it with those M JPanels.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not simply change the sequence of your component array?
Instead of a,b,c,d have it as a,c,b,d and the grid will be populated vertically.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen a VerticalGridLayout floating around on the web.
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Why not simply change the sequence of your component array?
Instead of a,b,c,d have it as a,c,b,d and the grid will be populated vertically.


I don't think it works, because I have 3 different button arrays. Can you please explain more in detail what you mean?

Here is a simple sketch of my wishes:

I have 3 button arrays; x[4], $[12], +[8]
I have 1 GridLayout with 16 cells (1234 x abcdef)

If I populate the gridlayout with the three button arrays one after the other, by default the gridlayout will fillup like this:



But I want the gridlayout to fill itself like this:

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Then write your own LayoutManager which does that. It might be easier than you think to write a layout manager, I've written one myself.

Or use more than one layout. From your casual description let's try this: make a GridLayout which has 1 column and N rows (whatever N might be). Then create a JPanel using that layout and fill it with one column of your data. Repeat for each of the M columns you need (M = 4?) and then you have M JPanels. Create another JPanel with a GridLayout which has 1 row and M columns, and fill it with those M JPanels.

I think Paul can be onto something here. The main panel has a GridLayout with only 1 row. Each row has another panel with a GridLayout with only 1 column. The main GridLayout will make sure that all panels have the same size. Inside the panels, their GridLayouts ensure that all components have the same size. So if all your panels have the same number of components then these will have the same size. You can use empty JLabels as filler if needed.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Why not simply change the sequence of your component array?
Instead of a,b,c,d have it as a,c,b,d and the grid will be populated vertically.

Because that will also change the tab order.
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok if the size of the cells stays the same, even though there different panels, then I will give it a try. I'm so happy with all my buttons having the same size right now, that I first didn't want to split them up in several panels. I'm scared of messing up the whole design.

Thanks gentlemen, your all fantastic!
 
Lec Chen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I met the exact same problem, and below is my work around, in case some one is transported here by Google like I am :


 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not use a GridBagLayout and add buttons in your order? this can be done like: increasing the gridy in the inner loop and gridx in the outer loop. This way, your tab order remains the way you want it to...
with having same fill and weights, we can have GridBag behave like a GridLayout...
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:with having same fill and weights, we can have GridBag behave like a GridLayout...



Only if all components have the same preferredSize.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic