| Author |
Get a JPanel to fill all available width
|
tom davies
Ranch Hand
Joined: Apr 27, 2012
Posts: 168
|
|
I am creating a layout for a new program i am working on. It consists of a main JFrame with a header and then a tabbed layout. Within my tabbed layout i have a main JPanel, within this JPanel i plan to have three more separate JPanels to handle the different layouts i need on this tab.
Currently For the inner JPanel i am using gridbag layout which lays the components out fine but it is still cantered in the middle of the panel. The width grows if i make the components wider but i would rather the JPanel take up all available width (slightly narrower to add a border) and the components to be aligned within it.
Any tis on how i can achieve this?
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
tom davies wrote:Currently For the inner JPanel i am using gridbag layout which lays the components out fine but it is still cantered in the middle of the panel.
Read the documentation for GridBagConstraints#weightx / wieghty
|
luck, db
There are no new questions, but there may be new answers.
|
 |
tom davies
Ranch Hand
Joined: Apr 27, 2012
Posts: 168
|
|
Darryl Burke wrote:
tom davies wrote:Currently For the inner JPanel i am using gridbag layout which lays the components out fine but it is still cantered in the middle of the panel.
Read the documentation for GridBagConstraints#weightx / wieghty
I have tried adding weightx = 1 but it seems to make no difference to the layout, the panel remains in the centre.
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
Have you read a tutorial on how to use GridBagLayout such as http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html.
You may want to look in particular at 'GridBagConstraints.fill'.
|
 |
tom davies
Ranch Hand
Joined: Apr 27, 2012
Posts: 168
|
|
I am also using fill horizontal. I have put the code below as it will probably make more sense.
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
|
In the logManagerPanel() method why are you adding the upload JPanel to another JPanel which you have set the size on (calling setSize() does nothing BTW unless the layout manger this panel is added to honours that setting)? Why not just return 'upload'.
|
 |
tom davies
Ranch Hand
Joined: Apr 27, 2012
Posts: 168
|
|
Tony Docherty wrote:In the logManagerPanel() method why are you adding the upload JPanel to another JPanel which you have set the size on (calling setSize() does nothing BTW unless the layout manger this panel is added to honours that setting)? Why not just return 'upload'.
Yeah i found out that set size does nothing, i should have already remove it.
The logManagerPanel has three different sections to it (well it will eventually currently i am only working on the first).
Each section has a different layout to it, so to avoid a very complicated GridBagLayout i decided to have three JPanels one after the other in a flowLayout and then tdo a GridBagLayout within these.
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
|
In that case set the LayoutManager of the logManagerPanel to BorderLayout and add your upload panel to the CENTER.
|
 |
tom davies
Ranch Hand
Joined: Apr 27, 2012
Posts: 168
|
|
Tony Docherty wrote:In that case set the LayoutManager of the logManagerPanel to BorderLayout and add your upload panel to the CENTER.
Thank you, that has solved the width issue. If i add multiple panels to the centre will this not overwrite the previous panels though?
EDIT: Actually i could probably get around this seeing as i have three sections. I could just use north, centre and south
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
If i add multiple panels to the centre will this not overwrite the previous panels though?
Yes it will. You can only put one component in a location. If you want to put more than one component in a location you need to add them to a container (eg JPanel) and add the container to the location.
|
 |
 |
|
|
subject: Get a JPanel to fill all available width
|
|
|