| Author |
Fill a GridLayout vertically instead of horizontally
|
Patrick de Kruijf
Ranch Hand
Joined: Mar 02, 2010
Posts: 63
|
|
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
|
------------------8<------------------
please cut here
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2925
|
|
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
|
Mohamed Sanaulla | My Blog
|
 |
Patrick de Kruijf
Ranch Hand
Joined: Mar 02, 2010
Posts: 63
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
|
After looking at the API documentation I would have to say "No".
|
 |
Patrick de Kruijf
Ranch Hand
Joined: Mar 02, 2010
Posts: 63
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
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
Joined: Jul 26, 2007
Posts: 8430
|
|
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.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1787
|
|
|
I've seen a VerticalGridLayout floating around on the web.
|
 |
Patrick de Kruijf
Ranch Hand
Joined: Mar 02, 2010
Posts: 63
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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
Joined: Mar 02, 2010
Posts: 63
|
|
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
Joined: Oct 03, 2011
Posts: 1
|
|
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
Joined: Oct 16, 2003
Posts: 914
|
|
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...
|
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
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.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
 |
|
|
subject: Fill a GridLayout vertically instead of horizontally
|
|
|