File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Get a JPanel to fill all available width Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Get a JPanel to fill all available width" Watch "Get a JPanel to fill all available width" New topic
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: 4522
    
    5

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: 1934
    
  28
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: 1934
    
  28
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: 1934
    
  28
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: 1934
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get a JPanel to fill all available width
 
Similar Threads
Simple side-by-side layout manager?
JscrollPane with JTabbedPane for NullLayout
Entering data in many tabbed JPanels and saving once
Hierarchical GUI advice
Turning JTree into a layout manager, resizing nodes