I am trying to create a GUI in which has three main panels for a text-based game: the leftmost panel for your stats/inventory, the center panel will be for a text pane that displays the story and a text field to type your commands, and the rightmost pane contains quest and mission information. I set the main content pane to have a BorderLayout and added JPanels to the west, center, and east positions (no components added to the panels yet). The center panel takes up most of the space, so I set the east's and west's panels' preferredSize width to be 200, which looks great proportionally for the size of the JFrame (800x500). When maximising the window, however, the east and west panels remain at 200 rather than scaling up proportionally, making it seem small compared to the center panel. When resizing the width of the window to be smaller than 800, the east and west panels remain 200 while the center panel gets smaller, to the point of the east and west panels overlapping. I was wondering if it would be possible to use BorderLayout's west, center, and east positions to scale proportionally upon resizing rather than just the center resizing up and down.
Thank you much for your time!
Edit:
Here are a couple snapshots to show the GUI. It is saying I do not have enough reputation to include images on my posts, so I can only include the hyperlinks to them.
The first image shows the frame at 800x500, the default size (blank space at left and right of the center panel are empty JPanels). The second image shows the GUI maximised (left and right panels remaining at 200).
scale proportionally upon resizing rather than just the center resizing up and down.
I suggest you to use GridBagLayout where you can set the width and height of the components in percentage defining different constraints of GridBagConstraints
See Swing Tutorial on How to Use GridBagLayout
Related
As I understand it, the normal FlowLayout only uses the preferredSize to determine the width and heigt of a component and draws it exactly in these dimensions.
Now I'd like its components so stretch, when space is available and break to a new line, when all the components minimumSizes are reached.
Here is an example:
Red and Green are my components. These have a minimumSize of 100 (symbolized by the yellow line). They are placed in a Layout which is symbolized by the blue background. This is a big JFrame.
I then make my JFrame smaller until I reach the minimumSizes of the components. This JFrame is just big enough.
When I make my JFrame even smaller, the FlowLayout should break into a new line. The JFrame is now smaller than the components minimum sizes.
Is there a LayoutManager in swing who can do that?
Basically i would like to have BorderLayout that will expand north section vertically and shrink center section on resize. I think it's easier to explain with pictures.
So here is situation I have now:
But when dialog is resized i would like to have buttons expand vertically and to shrink down center pane (one with tabs). Currently when dialog is resized i get this (the rest of the buttons are "hidden":
My current layout is set like this:
contentPane - BorderLayout
panelTopButtons - FlowLayout (set in north section of conentPane)
panelContent - BorderLayout (set in center section of contentPane)
So how can i achieve when dialog is resized that buttons in north section take more vertical space (and thus all are visible), and content of panelContent is shrink to fit rest of dialog contentPane space?
Put the buttons in the CENTER and the tabbed pane in the PAGE_END of the BorderLayout.
I have a program in which I use BorderLayout and the EAST and WEST positions are not filled, though the CENTER one is. As a result, the center component extends to the space where the east and west components would be.
How can I prevent this? I know I could just use a blank JPanel in the east and west positions, but that seems hacky.
If what you want is empty space around your center panel, you can add a border to it:
panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10);
You said:
I know I could just use a blank JPanel in the east and west positions, but that seems hacky.
Why is that hacky? How else would you control the amount of size of the visible space? What if you only want it to be a narrow (say, 10 pixels) blank space? What if you want it to be 100 pixels? What if you want the border to grow and shrink based on the size of the corresponding frame?
Adding some sort of component there (it doesn't have to be a JPanel, though that's not a bad choice) allows you to specify the size of it more effectively, and there's nothing inherently wrong with doing so.
What you need is Box.Filler. It is used to create empty components to fill areas.
See https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler
I have a Java Swing GUI where the top level layout manager is BorderLayout. I'd like it so that when the user resizes my GUI below the preferred size the center region can't be resized any smaller (or at least takes precedence in resizing) and the north/south regions become smaller.
The way it's working right now is my north and south panels are taking precedence and blowing away my center region first. I've tried setting a minimum size on the center panel once it has all of its components added to it
centerPanel.setMinimumSize(centerPanel.getPreferredSize());
but that isn't working for me. It's still the first region to minimize on a resize.
Maybe a BoxLayout will work. Box layout will respect the minimum size of a component. See the section from the Swing tutorial on How to Use Box Layout for more information.
I don't think anything simpler than GridBagLayout will serve. BorderLayout is not good about honoring minimum sizes.
I am trying to build my own "Battleship" game and have problems with swing.
I now read endless docs on oracle tutorials on LayoutManagers, but not any of them works as I understand them. They only add a few buttons, but never two individual panels.
JPanel Background = new JPanel();
Background.setLayout(new BoxLayout(Background, BoxLayout.X_AXIS));
panelPlayer = new JPanel();
panelPlayer.setBorder(BorderFactory.createLineBorder(Color.black));
panelPlayer.setSize(700, 600);
// PC Field
panelPc = new JPanel();
panelPc.setBorder(BorderFactory.createLineBorder(Color.black));
panelPc.setSize(700, 600);
//adding to frame
getContentPane().add(Background);
Background.add(panelPlayer);
Background.add(panelPc);
After that I have a loop thats adds 16x16 buttons in a JButton[] once for every panel.
How to get the two panels to show a table layout?
I used GridLayout before, the grid works, but it always takes up the whole space of the frame, not of the Container or Panel or else. The panels are overlapping then.
GridBagLayout just puts the buttons in a row and beyond the screen.
Don't fix the size of the panel while using any layout. It works only when you use null layout
You can achieve your goal with GridBagLayout. While adding buttons specify gridx, gridy correctly, it will add buttons like table
just keep nesting the layouts.
in your case make a big one with two sides -
then in each side place another panel with your grid.
You can solve this by nesting panels. Each panel has its own layout manager, so it is a matter of breaking up your UI into pieces and choosing the layout manager for each piece.
If you want two panels side-by-side, then the panel that contains them should have a FlowLayout manager with horizontal orientation. Create a panel with FlowLayout and add the panels to it.
If each of the the side-by-side panels needs the grid of buttons, then set the panel layout to GridLayout and put the buttons in the panel. This fits what I remember of Battleship; in a grid layout, all the grid elements remain the same size no matter how the window is resized.
That should get you started. If, as I expect, you will want another panel with some game controls on it, look into BorderLayout; it has a section on each edge of a rectangle and another in the middle. Put the panel containing the two grids in the center of a panel using BorderLayout, and then your game controls can go in a panel to the north, south, east, or west of that.
Good luck. Let us know if you have a specific problem (in another question).