I want to get 5 components from BorderLayout (and prefer to get them with knowledge of there placement in the layout, e.g : CENER,NORTH,SOUTH,EAST,WEST
Well, only the BorderLayout knows what constraints you have used. So that information would be found in the BorderLayout
API. Look at the various getter methods.
You would use:
BorderLayout layout = (BorderLayout)panel.getLayout();
to access the layout manager.
Then you would request the component for each of the constraints using the appropriate method from the API.
i'm trying to build application with gui that demonstrate computer keyboard. and since in each row there are different numbers of keys, and some of them are in different size i decided to use FlowLayout for each "row" in keyboard
A better solution is to let a layout manager do the resizing.
Take a look at: Why does this GridBagLayout not appear as planned? for an example.
You would just need to add:
gbc.weightx = 1.0f;
to the example to allow for dynamic horizontal resizing.
Edit:
Attempting to answer questions related to the other link.
Look at the picture in the other link. You will see each row contains 5 to 8 buttons, which means the GridBagLayout only know about the size of 8 individual columns.
Add up the numbers in the (..) which indicate the gridwidth for each row. The total is 22.
The GridBagLayout doesn't know how to turn 8 columns into 22 columns. It doesn't know what widths to use for each column.
So the suggested solution is to add 22 dummy components in a single row. Now each column will have a width so specifying"gridwidth" will now mean something to the GridBagLayout.
In the code you will see I tried to assign the dummy buttons to gridy=4, but the code was wrong:
ui.add(new JButton()); //wrong
//ui.add(new JButton(), gbc); // what I should have had
Now the default behaviour is you don't specify a GridBagConstraint, is to just display each component on the first row (ie. gridy=0).
So the problem is now you have two rows of buttons that are trying to be displayed at gridy=0, which is obviously wrong.
At the time I didn't realize I forget to specify the "gbc" for the dummy buttons so I just adjusted the gridy for each row of buttons which is why you see code like:
//gbc.gridy = 0;
gbc.gridy = 1;
I just shifted each row of the keyboard buttons down 1, so the dummy buttons can display at gridy=0.
If you want to see the buttons at the bottom change the code as follows:
//gbc.gridy = 4;
//ui.add(new JButton());
gbc.gridy = 5;
ui.add(new JButton(), gbc);
Now components will exist for gridy=1, 2, 3, 4, 5. There are no components in gridy=0.
Somehow GridBagLayout "lerans" cell size from the "for" loop
Yes, because each button has its own size. Since 22 buttons were added on a single row the GridBagLayout knows the minimum width of all 22 columns.
So now if you add a button at gridx=1, with a gridwidth=2, the width of the button is simply the width of column 1 and column2, which in this case is simply the width of the dummy button in each column.
However, the "edited" solution is even better because you don't need the dummy button to determine the width of each of the 22 columns.
The size of the array defines the number of columns.
The value assigned to each entry in the array is the "minimum width" of the column (it can be different for each column).
So now, even though you don't have the dummy components, the GridBagLayout knows how to calculate a width for each column and using "gridwidth" will work as expected.