java JScrollPane with JPanel with BoxLayout resizes elements - java

I have a collection of JPanels for different elements: JPanel showPane, seasonsPane, episodesPane, airingsPane
all of which have setLayout set to null and are of fixed size 304x416. I added those JPanels to a JPanel called showViewPanel of size 1280x416 and set up a BoxLayout like so (below is the code in the JFrame):
showViewPanel.setLayout(new BoxLayout(showViewPanel, BoxLayout.X_AXIS));
showViewPanel.setSize(1280, 416);
showViewScroll = new JScrollPane(showViewPanel);
add(showViewScroll);
setSize(304, 416);
setVisible(true);
What I can't figure out is why it does not produce a scrollable view of all the components and instead resizes showViewPanel to match the size of the window. What am I doing wrong?

all of which have setLayout set to null and are of fixed size 304x416.
Don't use a null layout!!! Don't manually set the size of a panel!!! Your panels should be using a layout manager so the preferred size will be calculated automatically.
If you want all your panels to be the same size then maybe use a GridLayout for your main panel (instead of the BoxLayout) and then add your child panels to this panel.
Scrollbars will automatically appear when needed if you let the layout managers do their job.

Override the preferredSize of the subpanels to 304x416 instead. The scrollpane relies on the preferred size of the content (which depends on the preferred size of its contents).
The layout manager will set the size of the subpanels depending on their preferred sizes, so your custom ones get overridden. Generally, you should get rid of null layouts and learn to use the layout managers. Absolute placement leads to trouble all the time, and is not worth the hassle even when it does not.

Related

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.

JPanel inside JScrollPane expanding on pack

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.

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.

Java: Difference between the setPreferredSize() and setSize() methods in components

What is the main difference between setSize() and setPreferredSize(). Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other.
What call should I use for JFrames and JPanels?
Usage depends on whether the component's parent has a layout manager or not.
setSize() -- use when a parent layout manager does not exist;
setPreferredSize() (also its related setMinimumSize and setMaximumSize) -- use when a parent layout manager exists.
The setSize() method probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames and JWindows) and things that are inside of scrolled panes. You also must call setSize() if you've got components inside a parent without a layout manager.
Generally, setPreferredSize() will lay out the components as expected if a layout manager is present; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, then using setSize() and setLocation() to position those components according to the layout's rules.
For example, a BorderLayout tries to make the bounds of its "north" region equal to the preferred size of its north component---they may end up larger or smaller than that, depending on the size of the JFrame, the size of the other components in the layout, and so on.
setSize() or setBounds() can be used when no layout manager is being used.
However, if you are using a layout manager you can provide hints to the layout manager using the setXXXSize() methods like setPreferredSize() and setMinimumSize() etc.
And be sure that the component's container uses a layout manager that respects the requested size. The FlowLayout, GridBagLayout, and SpringLayout managers use the component's preferred size (the latter two depending on the constraints you set), but BorderLayout and GridLayout usually don't.If you specify new size hints for a component that's already visible, you need to invoke the revalidate method on it to make sure that its containment hierarchy is laid out again. Then invoke the repaint method.
setSize will resize the component to the specified size.
setPreferredSize sets the preferred size. The component may not actually be this size depending on the size of the container it's in, or if the user re-sized the component manually.
IIRC ...
setSize sets the size of the component.
setPreferredSize sets the preferred size.
The Layoutmanager will try to arrange that much space for your component.
It depends on whether you're using a layout manager or not ...
In addition to the fact that setSize() should be used in the absence of a layout manager, and setPreferredSize() - when there is no layout manager, there is another difference. 1 takes two numbers in the parameters, and setPreferredSize() takes an object of class java.awt.Dimension:
JFrame frame = new JFrame("Test");
frame.setSize(200,300); //numbers
frame.setPreferredSize(new java.awt.Dimension(200,300)); //an object of java.awt.Dimension

Categories

Resources