How to vertically center align a JScrollPane in a JPanel? - java

I have a JPanel with a GridLayout consisting of 1 row and 2 Columns.
Inside the first column, I have a JPanel consisting of a Jpanel and a Jtree.
Inside the second column, I have a JScrollPane consisting of a JTable.
How can I center-align the JscrollPAne to appear vertically center-aligned? currently, It is showing at the top and not at the center.
Here is an image:

You for your JScrollPane in the 2nd column, you will need to wrap it with a JPanel. Then you will need to set a layout manager for the new JPanel. I would probably use GridBagLayout as it gives you more precise control and you can achieve the vertical centering you desire with it.
You will have to add a fake JPanel into the wrapper JPanel to fill in the whitespace you are looking for.
So in short you will have:
JPanel with GridLayout (1 row, 2 columns)
Column 1:
JTree
Column 2:
JPanel with GridBagLayout which then holds:
JPanelwith GridBagConstraints (for whitspace)
JScrollPane with GridBagConstraints
You might need to add another JPanel below the JScrollPane if you want whitespace down there too. I'm not 100% sure what your looking for. With the GridBagConstraints you can precisely control the layout.

There is a Layout manager to save you :) Inside the second column, use a panel with GridBagLayout , and insert the JScrollPane into the panel, you can specify the position of components in panel using GridBagConstraints.
See this for more information : How to Use GridBagLayout

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.

Swing: how to add two JScrollPanes with TableLayout?

For my Java program I am actually using the simple library TableLayout as layout for my main JPanel body so that I can add any widget just by specifying its row and column index, for example"
body.add(new JLabel(
"Search by date"),
"1,8");
Now I would need to add two JScrollPane (one horizontal and one vertical) but they should include all the body and not just a single cell of the layout. Shall I add another JPanel? How can I do it?
Now I would need to add two JScrollPane (one horizontal and one
vertical) but they should include all the body and not just a single
cell of the layout. Shall I add another JPanel?
IMO, yes you should. Nesting Layouts is a common approach that could be applied in this way:
Create a new JScrollPane and set your panel as its viewport view.
Give the scroll pane a reasonable preferred size to enable the scroll bars if your panel's size exceeds this preferred size.
Have a wrapper panel with BorderLayout and add the scroll pane to its CENTER location.
In a nutshell:
JScrollPane scrollPane = new JScrollPane(yourPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
See also:
How to Use Scroll Panes

How to put two java swing JPanels next to each other and have a table in it?

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).

How can I split a panel into subpanels?

I already have a panel made (its a row of buttons), and have it located at the bottom of a frame (SOUTH), but I would like to add two rows (panels/ subpanels) beneath it (a text input line and output line if it matters). Right now the only thing I know to do is declare and add more panels, which would be fine, but when I specify .SOUTH they go over top of the previous panel.
EDIT: The solution I used
As suggested by Ted Hopp, I added my original panel (now called subPanel1), as well as the two new panels which were going on top of the original (subPanel2 & subPanel3), to a fourth "container panel" (bottomCotainerPanel). Since I only had three subPanels, this allowed me to specify where in the containerPanel each subPanel would go (using NORTH, CENTER, SOUTH, might have to do something slightly different if you had more than 3...), and then specify where the contianerPanel would go in the frame (SOUTH).
subPanel1.setLayout(new GridLayout(1,6)); //Layout of subPanel1
subPanel1.add(clearButton);
subPanel1.add(customerNameLabel);
subPanel1.add(customerNameTextField);
subPanel1.add(showByNameButton);
subPanel1.add(openNewSavingsButton);
subPanel1.add(openNewCheckingButton);
subPanel2.add(sendChatTextField);
subPanel2.add(sendButton);
subPanel2.add(clearButton2);
subPanel3.add(receiveChatTextField);
subPanel3.add(nextButton);
subPanel3.add(previousButton);
bottomContainerPanel.setLayout(new GridLayout(3,1)); //Layout of Container Panel
bottomContainerPanel.add(subPanel1, BorderLayout.NORTH);
bottomContainerPanel.add(subPanel2, BorderLayout.CENTER);
bottomContainerPanel.add(subPanel3, BorderLayout.SOUTH);
tellerWindow.getContentPane().add(bottomContainerPanel, BorderLayout.SOUTH);
You need to add a single container panel as the SOUTH panel of the frame. The container itself should have the layout that you want for everything that goes at the bottom.
If you just want to split panel onto 2 equal parts at south and north use GridLayout. If you want something in the middle you can use BorderLayout.
If you want to give your user ability to change the sub-panels size use JSplitPane.
I had a similar problem trying to put several rows of buttons into a panel borrowed from the ListDemo example. Well, the first thing to do is to read about BorderLayout: How to Use BorderLayout, or at least see the image shown there:
You cannot have multiple bottom rows in a BorderLayout. But you can use a nested layout. What we need is a BoxLayout, see How to Use BoxLayout:
.
We just have to replace the buttons shown on the above image by rows of buttons.
public class MyStuff extends JPanel {
...
public MyStuff() {
super(new BorderLayout());
...
JPanel buttonArea = new JPanel();
buttonArea.setLayout(new BoxLayout(buttonArea, BoxLayout.PAGE_AXIS));
add(buttonArea, BorderLayout.PAGE_END);
...
//if you dislike the default center alignment:
//panelWithButtons1.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonArea.add(...);// add the 1st panel with buttons
buttonArea.add(...);// add the 2nd panel with buttons
buttonArea.add(...);// add the 3rd panel with buttons

Recommendation for Layout Manager

I am creating a GUI for an arcade game. It consist of a single JFrame with some JPanels and one JMenu as shown in the figure:
I've been trying using BorderLayout but it doesn't show the panels properly. What I get is that the JMenu shows properly. [1] adjusts its width to content the buttons inside it. The JPanel [2] fulfill almost the rest of the screen. And [3] shows just as a thin line at the end.
Here's the fragment of code that I use to put them in position:
add(new TopMenu(), BorderLayout.PAGE_START); // JMenu
add(new LeftPanel(), BorderLayout.WEST); // [1]
add(new StatusPanel(), BorderLayout.CENTER); // [2]
add(new GameUI(), BorderLayout.LINE_END); // [3]
Any suggestions of what could be provoking this behavior are welcome.
You can always nest JPanels/containers, each using its own layout. So the overall layout could be a BorderLayout with the menu at the BorderLayout.NORTH and the JPanel [1] on the BorderLayout.EAST side, then nest a JPanel into the BorderLayout.CENTER position using either another BorderLayout or a BoxLayout and put your other two JPanels into this JPanel. For instance this CENTER JPanel could use BorderLayout and it could hold JPanel [2] in its BorderLayout.NORTH position and JPanel [3] in its BorderLayout.CENTER position.
Your JPanel's have to have Swing components inside, or else they will shrink to the minimum size when you call pack() on the JFrame.
Since you're creating a game, you'll need to set the preferred size on your JPanel's, and set the preferred size on the JFrame. I'm assuming you'll want to paint at least one of the JPanel components directly using the paintComponent method.
You can nest JPanel-2 and JPanel-3 in a rightPanel. You can use FlowLayout for the rightPanel itself, as well as putting JPanel-1 and rightPanel into the JFrame.
If you insist on using a layout manager that will lay out the 3 JPanel's without nesting, you will have to use the GridBagLayout. JPanel-1 will be 1 column wide and 2 rows deep. JPanel-2 will be 1 column wide and 1 column deep. JPanel-3 will be 1 column wide and 1 column deep.
You'll still have to set the preferred size of the 3 JPanel's.

Categories

Resources