Java layout center with two panels - java

In Java, when using the BorderLayout, is it possible to have two panels in the CENTER, but both be visible on the form.
Here is my code:
guiFrame.add(guiFieldsPanel, BorderLayout.CENTER);
guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);
guiFrame.setVisible(true);
In the above code, both panels are set to the center, yet I can only see the guiButtonsPanel as it is 'on top' of the guiFieldsPanel.
Can i group both panels together, and then set them to be displayed in the CENTER?

See the Nested Layout Example for ideas about how to combine layouts to create the required layout. E.G.
Perhaps use a single row GridLayout for the center.
guiFrame.add(guiFieldsPanel, BorderLayout.CENTER);
guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);
But that suggests a 2 column GroupLayout as seen in this answer. E.G.

You will need to create an intermediate panel that will contain both guiFieldsPanel and guiButtonsPanel, and then add that to the border layout.
final JPanel centre = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
centre.add(guiFieldsPanel);
centre.add(guiButtonsPanel);
guiFrame.add(centre, BorderLayout.CENTER);
guiFrame.setVisible(true);
You can adjust the layout of centre as appropriate for your needs with respect to the relative positioning of guiFieldsPanel and guiButtonsPanel.

Related

What layout should I use for my JPanels to look like this?

I would like to layout some JPanel containers to look something like this:
However, I am not quite sure what layout would be the best to choose, since I am still new to using the layout manager and do not want to only ever use null as layout manager.
So I thought that I could maybe split this into two panels. One for the left side and one for the right panels. My frame is 600x600px big and has the BorderLayout, so I thought I maybe can do something like this, which did not work:
JPanel right = new JPanel();
right.setLayout(new Border Layout());
right.setSize(400, 600);
JPanel green = new JPanel();
green.setPreferredSize(new Dimension(200,200));
green.setBackground(Color.green);
right.add(green, BorderLayout.PAGE_START);
//for orange same code but with center and for pink the same but with page_end
frame.add(left, BorderLayout.LINE_START);
frame.add(right, BorderLayout.LINE_END);
...
I would suggest using a gridbag layout where you can have components take up multiple cells. You can find more information using this java doc: https://docs.oracle.com/javase/7/docs/api/java/awt/GridBagLayout.html

Center Panel's Component Within a java swing BoxLayout

I'm trying to use a BoxLayout to display 2 panels vertically and I searched how to center the components in those panels. At the moment, my components are placed on the top center of each panel, and I want to get them at the center X and Y.
I added the components I want in the 2 panels, then I added the panels in my BoxLayout. This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
I tried to use methods such as setAlignementY and setLocation but any of them actually moves the components. I also saw that a BoxLayout will try to set the components as wide as the widest component, but as I have only 2 panels which have the same size I don't really understand it.
This is basically how I've added my components (without trying to center) :
private void initPanels ()
{
this.titlePanel.add(this.title);
this.bookInputPanel.add(bookTitle);
this.bookInputPanel.add(bookInput);
this.authorInputPanel.add(by);
this.authorInputPanel.add(authorInput);
this.authorInputPanel.add(this.authorsTable);
this.buttonsPanel.add(confirm);
this.contentPanel.setLayout(new BoxLayout(this.contentPanel, BoxLayout.Y_AXIS));
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.add(this.titlePanel, BorderLayout.NORTH);
this.add(this.contentPanel, BorderLayout.CENTER);
this.add(this.buttonsPanel, BorderLayout.SOUTH);
}
I made a picture to show you exactly what I want but it seems that I need 10 rep to do it, sorry about that.
This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
One way is to add "glue" to the top/bottom of the panel. This "glue" will expand to fill the extra space available to the panel:
this.contentPane.add(Box.createVerticalGlue());
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.contentPane.add(Box.createVerticalGlue());
Read the section from the Swing tutorial on How to Use BoxLayout for more information about the feature of a BoxLayout.
Another option might be to use a "wrapper" panel that uses a different layout manager. For example the GridBagLayout with the default constraints will automatically center the component horizontally/vertically:
//this.add(this.contentPanel, BorderLayout.CENTER);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add( contentPanel );
this.add(wrapper, BorderLayout.CENTER);

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.

Using BorderLayout for adding 4 in-line Components to a JPanel

In my application, there are 4 panels. And i need to insert them into the main panel, which uses BorderLayout. The 4 panels are...
A thin Image strip.
4 buttons just below above
A TextField covering the complete page.
An about at end.
This is my code...
add(imageLabel, BorderLayout.NORTH);
add(buttonPanel,BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
add(about, BorderLayout.PAGE_END);
When I do this, the buttonPanel disappears. How can I achieve what I need?
I usually try to keep a maximum of 3 components in any BorderLayout, so I would do it like this...
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel= new JPanel(new BorderLayout());
innerPanel.add(buttonPanel,BorderLayout.NORTH);
innerPanel.add(logScrollPane, BorderLayout.CENTER);
innerPanel.add(about, BorderLayout.SOUTH);
outerPanel.add(imageLabel, BorderLayout.NORTH);
outerPanel.add(innerPanel,BorderLayout.CENTER);
As long as you keep the 'maximum-stretched' component in the CENTER (in this case, your logScrollPane) then it'll always work. If you want to use the panel, such as setting it on a JFrame, just use add(outerPanel).
Don't be afraid of BorderLayout - the ability of this layout to auto-expand the CENTER component to fill the available space make it a very powerful and very important LayoutManager!

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

Categories

Resources