Jerome
Jerome

Reputation: 1215

Java Swing LayoutProblem

I'm designing an application with Java and the Swing library (I can also use fragments of AWT) and I need some help with a "custom" layout.

I have a big JScrollPanel, where I'll dynamically insert JPanels of a variable height inside. Here is a more or less realistic draw :

|-------------------------------- JScrollPane ------------------------------|
| |------------------------------- JPanel --------------------------------| |
| |                                                                       | |
| |-----------------------------------------------------------------------| |
|                                                                           |
| |------------------------------- JPanel --------------------------------| |
| |                                                                       | |
| |                                                                       | |
| |-----------------------------------------------------------------------| |
|                                                                           |
|---------------------------------------------------------------------------|

Then I don't know which Layouts use.... I'd like the mainLayout to not resize the height of my internal JPanels but to resize their width. So when I expand the window for example, I want my internal Layouts to stay close to the left and right border but not to expand vertically.

Any suggestion?

I hope my english and explainations weren't too bad. Thanks for reading!

Upvotes: 0

Views: 89

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

The layout I might use in this case is either GridBagLayout or VerticalLayout (from SwingLabs, SwingX library) (but when all you have is hammer, everything looks like a nail ;)).

To use GridBagLayout, you will need to provide appropriate constraints that will instruct the layout manager to fill the panel out horizontally, but to honour the height.

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;

The only problem with this, is GridBagLayout likes to center it's components around the center position of the parent container. You may need to add a "filler" component to the last position that takes the remaining vertical space

gbc.weighty = 1;

Should suffice

Take a look at How to use GridBagLayout for more details

Update with very basic example...

// Basic constraints...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;

// Add your components...
add(panel, gbc);
//...

// Then add a fill component to push the layout to the top...
gbc.weighty = 1;
add(new JPanel(), gbc);
// You can use a constant value if you want, as it will be easier to remove
// so you can add new components to the end and re-add it when you're done

Upvotes: 1

Related Questions