JPanel inside JScrollPane expanding on pack - java

I have a JPanel inside of a JScrollPane which is nested inside some other containers. My JPanel uses a modified FlowLayout from SO user jxd in this question.
This may be information overkill but the full nesting of the panel in question is as follows:
JPanel (ModifiedFlowLayout) > JScrollPane > JPanel (GridBagLayout) > JTabbedPane > JPanel (GridBagLayout) > JSplitPane > JPanel (BorderLayout) > JFrame.
The problem is that when I call pack() on my JFrame the JScrollPane/JPanel expands horizontally to fill the entire remaining screen space (across multiple monitors). The space used is more than is needed to display all of the components in the JPanel. I tried using setMaximumSize() on my JPanel but it seems to be ignored in this scenario.
Ideally I would like the panel to have it's size dictated by space left after sizing the components that surround it. Can/how can this been done?

The preferred size of a FlowLayout will try to display all components on a single row. I don't know how the ModifiedFlowLayout works but you can check out Wrap Layout which does the same thing.
However, when using Wrap Layout you can use the setSize(...) method to make a suggestion as to what the initial width should be. Initial wrapping of components should then be based on that size.

Related

What layout should I use for chat history

![two muppets][1]
I want to make a chat history in java swing.
I have a JScrollPane. I append dinamic multiple JPanels in him.
I use BoxLayout but I have a problem:
- When appending the first JPanel it are height 100% of JScrollPane.
- When appending the second JPanel, both are height 50% of total JScrollPane.
I want to make each JPanel have fixed height (40px).
What layout should I use or what should I do?
First off, I'd consider using a JList and not a grid of JPanels.
Your JList cell may easily display what looks like a JPanel view by simply using the right renderer.
And you'd lesson GUI override doing this.
If you must use JPanels, I'd put them in a GridLayout or BoxLayout container (JPanel)
Add this container to a BorderLayout using container (JPanel), to its BorderLayout.PAGE_START position
And then add this final container to the JScrollPane's viewport.

How to make a JPanel dynamic?

I have a JFrame that has a JPanel inside. I call "setPreferredSize(new Dimension(500, 600));" but I want the JPanel and its contents to resize when someone resizes the JFrame.
BorderLayout is the way to go. Components start at their preferred size, but are expanded as needed to fill the region they are in.
Set your layout on your frame with BorderLayout
Add your JPanel by
frame.add(yourPanel, BorderLayout.CENTER);
This will allow it to stretch vertically & horizontally
As for the Contents inside a JPanel, give it a layout that will accommodate stretching as well.
Use a layout manager instead of setting the bounds for each component.
It is going to vary from program to program how you want your components to move.
Take a look at this and try to see which layout will work best for you.

Set constant size of JScrollPane

I have a JTabbedPane with a Border Layout.
Here's the code I'm using to add the components:
add(columnNames, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
add(useCtrl, BorderLayout.SOUTH);
setVisible(true);
Question:
Notice the excess whitespace to the right inside the JScrollPane. I don't want that there. I would like for the JScrollPane not to change size at all when changing the size of the JFrame. I have tried setSize() and setPreferredSize(), but the size of the JScrollPane always changes. I've tried using GridLayout, but I get the same result.
Place the JScrollPane in a JPanel with another layout. (e.g. BoxLayout or GridBagLayout). And add the JPanel to the center.
The size of a graphics object is controlled by the layout manager. The BorderLayout will always expand the CENTER object to take up all available space. GridLayout expands all it's children proportionally. If you try a GridBagLayout and set the weightx to 0, that will prevent expansion horizontally.
There are a lot of layout managers available, browse the API for more choices and experiment until you find the resizing behavior you want. Each has a fairly good explanation of how it works in the javadoc.

Java - Having trouble setting JFrame to a good size

I'm making Minesweeper as a school project. It's close to completion, but the only problem now is setting JFrame's size. I just can't figure out a way to set frames to the size I want.
The program looks almost like a Swing version of the original Minesweeper on Windows XP.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button. The top panel's using flow layout, and the bottom panel's using grid layout for the buttons.
I set the preferred size of the frame's content pane. Getting the width is easy (The numbers of fields in a row * my button size), but the problem is getting the height right. The frame always go down to the 2nd last row of the minefield.
I also tried pack() but it resizes it to the preferred size of the content pane, which isn't the right size to begin with. What can I do?
Don't have the JFrame (or better its contentPane) use FlowLayout since this won't give the JFrame the best size for its components. Instead why not have it use the default BorderLayout? Your mine cell's will probably have their getPreferredSize() method overridden and thus will direct the size of the enclosing containers. As always, call pack() on the JFrame after filling it with components and before calling setVisible(true) on it.
Set a preferred size for the buttons in the GridLayout and pack() the frame after adding them.
Don't try to manually set the size. You should let each component display at its preferred size and use the pack() method.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button
I would use a BorderLayout. Create a top panel and add it to the NORTH.
Then create a panel for the grid and add it to the CENTER. If you have problems with the buttons in the grid resizing then try creating a JPanel as a wrapper panel. Add the buttons to this panel and then add this panel to the CENTER of the frame. The panel will retain its preferred size.

How do I make JScrollPane work properly with nested JPanels?

I'm building a Swing application in Java using NetBeans and I have a problem with layout. My main frame contains a JScrollPane which contains a JPanel called contentPanel which in turn contains a JPanel called listPanel. The listPanel is empty when the program starts, but when the user interacts with the program an unpredictable number of smaller JPanels are added to it. I've used the NetBeans GUI-builder to snap the top edge of listPanel to the top of contentPanel, and the same with the bottom edges.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane. The verticalScrollBarPolicy of my scrollpane is set to AS_NEEDED and its viewportView is set to contentPanel. What I think I need to do is to make contentPanel grow when more items are added to listPanel.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane.
The scrollbar will appear when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. When you add components dynamically you need to tell the scrollpane something has changed. So you basic code should be:
panel.add( subPanel );
panel.revalidate();
Or, because you are adding a panel to the sub panel, you may need to revalidate the scrollpane (I don't remember):
panel.add( subPanel );
scrollPane.revalidate();
The key is the revalidate() which tell the layout manager to recalculate its size.
Use a different LayoutManager. One that will allow for vertical growth like BoxLayout. Also remember that you can use multiple layouts and nest them inside of each other for different effects.

Categories

Resources