How to divide a JPanel into left and right segments? - java

I want to divide a JPanel into left and right segments. How do I do that ? After that, I will place panels in the left and right half.

If there is no need to resize them, you can simply use a BorderLayout and insert your panels in the BorderLayout.EAST and BorderLayout.WEST:
JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );
You could also consider using a JSplitPane which allows to resize the UI:
JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel );

It is very easy if you use a JSPlitPane.

there are two ways
use GridLayout
use JSplitPane (with hidden divider)

JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(c1);
panel.add(c2);

Use a JSplitPane or a GridLayout

You can use SplitPane as Costis Aivalis suggested.
Or
Use Border Layout Manager on JPanel.
Put your left side components in WEST side and put your right side components in EAST side of layout manager.
JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);

JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);

JPanel example = new JPanel(new GridLayout(1,2));
example.add(p1);
example.add(p2);

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class Display{
JFrame frame=new JFrame("Drawing");
North north;
South south;
East east;
West west;
Center center;
public int width=600,height=600;
public Display() {
// TODO Auto-generated constructor stub
frame.setSize(width,width);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
north=new North(frame);
south=new South(frame);
east=new East(frame);
west=new West(frame);
center=new Center(frame);
frame.setLayout(new BorderLayout());
JSplitPane pane2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,west,east);
frame.add(pane2);
frame.setVisible(true);
}
}

Related

How to change width size of Jpanels in BorderLayout?

I have main JPanel which is Borderlayout with added 4 JPANELS: NORTH(Green), WEST(Red), CENTER(Gray), SOUTH(Blue). I want to reduce width size of WEST(Red) Jpanel, or increase width size of Center(Grey) Jpanel.
Screenshot:
Here is my code:
frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Main paneel
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
frame.getContentPane().add(panel);
//West panel;
JPanel panelWest = new JPanel(new GridLayout(14,0,0,2));
panelWest.setPreferredSize(new Dimension(300, 300));
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
panel.add(panelWest, BorderLayout.WEST);
panelWest.setBackground(Color.red);
for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
panelWest.add(buttonsTables[i]);
panelWest.add(Box.createVerticalStrut(10));
}
//South panel;
JPanel southPanel = new JPanel(); // Donji layout za dugmice
southPanel.setBorder( BorderFactory.createEmptyBorder(20,0,0,0) );
panel.add(southPanel, BorderLayout.SOUTH);
southPanel.setBackground(Color.BLUE);
JButton buttonDodaj = new JButton("Dodaj");
southPanel.add(buttonDodaj);
JButton buttonIzmeni = new JButton("Izmeni");
southPanel.add(buttonIzmeni);
JButton butonObrisi = new JButton("Obrisi");
southPanel.add(butonObrisi);
//North panel;
JPanel northPanel = new JPanel(); // Donji layout za dugmice
northPanel.setBorder( BorderFactory.createEmptyBorder(0,10,0,0) );
panel.add(northPanel, BorderLayout.NORTH);
northPanel.setBackground(Color.green);
JButton buttonImport = new JButton("Importuj fajl");
buttonImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importActionPerformed(evt);
}
});
northPanel.add(buttonImport, BorderLayout.WEST);
JButton ButtonRecord = new JButton("Snimi fajl");
northPanel.add(ButtonRecord, BorderLayout.WEST);
// Central panel
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.GRAY);
panel.add(centerPanel, BorderLayout.CENTER);
I want to reduce width size of WEST(Red) Jpanel
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
So why is the width of your Border so large?
A Border is for "extra" space around the components.
So the width of your panel is the width of the buttons plus the width of the border.
Edit:
panelWest.setPreferredSize(new Dimension(300, 300));
Don't hardcode a preferred size. The layout manager will calculate the size based on the above logic. Get rid of that statement.
Edit 2:
// buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
Get rid of any logic that attempts to control the size of a component. The point of using layout managers is to let the layout manager do the size calcualtions.
So for your buttons panel you need to nest panels to prevent the buttons from taking all the space.
You can do something like:
JPanel wrapper = new JPanel();
wrapper.add(buttonsPanel);
...
//panel.add(panelWest, BorderLayout.WEST);
panel.add(wrapper, BorderLayout.WEST);
By default a JPanel uses a FlowLayout which will respect the preferred size of any component added to it.
Another option is to use a GridBagLayout with the wrapper panel. By default the panel will then be displayed in the "center" of the available space. So it will be vertically centered and you won't need the EmptyBorder.

MigLayout Panel inside a MigLayout panel - aligning it to the bottom

The panel to the right with all the buttons, I would like to align to the bottom.
JPanel easternDock = new JPanel(new MigLayout("", ""));
easternDock.add(button1, "wrap");
....
this.add(easternDock);
I'm thinking I could add a component above all the buttons and make it grow in the y dimension to fill the screen, but I'm not sure what component I'd use for that and I can't find any components designed to do such a thing.
The way I would do it is to have another panel within the "easternDock" panel which contains all the components and have "easternDock" push this other panel to the bottom using the push Column/Row constraint.
From the MiG Cheat sheet : http://www.miglayout.com/cheatsheet.html
":push" (or "push" if used with the default gap size) can be added to the gap size to make that gap greedy and try to take as much space as possible without making the layout bigger than the container.
Here is an example:
public class AlignToBottom {
public static void main(String[] args) {
JFrame frame = new JFrame();
// Settings for the Frame
frame.setSize(400, 400);
frame.setLayout(new MigLayout(""));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Parent panel which contains the panel to be docked east
JPanel parentPanel = new JPanel(new MigLayout("", "[grow]", "[grow]"));
// This is the panel which is docked east, it contains the panel (bottomPanel) with all the components
// debug outlines the component (blue) , the cell (red) and the components within it (blue)
JPanel easternDock = new JPanel(new MigLayout("debug, insets 0", "", "push[]"));
// Panel that contains all the components
JPanel bottomPanel = new JPanel(new MigLayout());
bottomPanel.add(new JButton("Button 1"), "wrap");
bottomPanel.add(new JButton("Button 2"), "wrap");
bottomPanel.add(new JButton("Button 3"), "wrap");
easternDock.add(bottomPanel, "");
parentPanel.add(easternDock, "east");
frame.add(parentPanel, "push, grow");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Having Multiple Panels with scroll panes

I created two panels and a main panel. Each panel contains a very large image, and I wanted both of them to be scroll-able to see the rest of the image. But when I add the two panels in the main panel and run it, the first panel is soo big that it covers the second panel. How would I implement ScrollPane for both panels?
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(jPanelSouth, BorderLayout.SOUTH);
add(mainPanel);
//where would I use this?
//scrollPane.setViewportView();
}
}
Each panel contains a very large image>
//JPanel mainPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
You may want to use a GridLayout so that each scroll pane takes up half the frame so as much of each image as possible is displayed.
//JScrollPane scrollPane = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane(jPanelNorth);
The easiest way to use the scroll pane is to create the scrollpane with the component you want displayed and the scrollpane will add the component to the viewport for you.
//mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(scrollPane); // don't need the constraint when using GridLayout.
Then you add the scrollPane to the main panel, since the scrollpane contains the panel with the image.
it seems to use grid layout is much better than using border layout , in this case :
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
//1. use GridLayout with 2 rows and 1 column .
JPanel mainPanel = new JPanel(new GridLayout(2,1));
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
//2.you should place .setViewportView() here :
scrollPane.setViewportView(jPanelNorth);
scrollPane2.setViewportView(jPanelSouth);
mainPanel.add(scrollPane);//is in the top ("North")
mainPanel.add(scrollPane2);//next ("South")
//3.use setContentPane instead of add()
setContentPane(mainPanel);
}
}

align left and right two JLabels in a "North" BorderLayout

I am using BorderLayout for my application.
setLayout(new BorderLayout());
I need to align two JLabels in left and right in the "NORTH" of the JPanel.
Here is my code:
JPanel top = new JPanel();
top.add(topTxtLabel);
top.add(logoutTxtLabel);
add(BorderLayout.PAGE_START, top);
So I need topTxtLabel in left and logoutTxtLabel in right.
I tried to implement Border Layout again to use "WEST" and "EAST", but it didn't worked. Ideas?
Assuming your application consists of a JFrame with BorderLayout you could try this: Set the layout mode of your JPanel again to BorderLayout. Add the panel in the north of the frame. Then add the 2 JLabels in the east and west. You can also replace the JFrame with another JPanel.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main
{
public static void main(String[] args)
{
new Main();
}
Main()
{
JFrame frame = new JFrame("MyFrame");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
JLabel left = new JLabel("LEFT");
JLabel right = new JLabel("RIGHT");
JPanel top = new JPanel(new BorderLayout());
top.add(left, BorderLayout.WEST);
top.add(right, BorderLayout.EAST);
panel.add(top, BorderLayout.NORTH);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JLabel("Another dummy Label"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Boxlayouts with JPanels incorrectly expanding with resize

I've searched a fair amount and I cannot locate a good, simple answer to this problem.
I want a box layout (superPanel), which contains an upper and lower JPanel (mainPanel and footerPanel). The upper will contain further JPanels (leftPanel and rightPanel).
Consider the code below, I find that when I resize the window, the mainPanel gets larger, and so does the footer. The footer should always stay the same size, below the mainPanel, at the bottom of the frame.
frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JPanel superPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel leftPanel = new JPanel();
JPanel rightPanel= new JPanel();
JPanel footerPanel = new JPanel();
footerPanel.setBackground(Color.GREEN);
mainPanel.setBackground(Color.RED);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
leftPanel.add(new JButton("left"));
rightPanel.add(new JButton("right"));
footerPanel.add(new JButton("footer"));
container.add(mainPanel);
container.add(footerPanel);
frame.add(container);
frame.pack();
frame.setVisible(true);
Anybody know why this is occurring? If you run this you'll see that red and green both grow in size as the window is resized. What I want to see is the red getting larger, while the green remains the same size.
Glue doesn't work, and I don't want to have to use GridBagLayout unless I have to (please explain why I should if need be)
Thanks
When you want a "main" section and side sections which don't change size, you usually want a BorderLayout:
container.setLayout(new BorderLayout());
container.add(mainPanel, BorderLayout.CENTER);
container.add(footerPanel, BorderLayout.PAGE_END);
Try to use GridBagLayout with fill.BOTH, weightx = 1 and weighty = 1 for the main panel and fill.NONE, weightx = 0 and weighty = 0 for the footer panel or use Miglayout which is really easy and with it you can do all what you want.
BorderLayout is what you want on your container, adding the mainPanel to the center and footerPanel to the south. Try the following changes on your code:
frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BorderLayout()); // Use BorderLayout instead of BoxLayout
JPanel superPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel footerPanel = new JPanel();
footerPanel.setBackground(Color.GREEN);
mainPanel.setBackground(Color.RED);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
leftPanel.add(new JButton("left"));
rightPanel.add(new JButton("right"));
footerPanel.add(new JButton("footer"));
container.add(mainPanel, BorderLayout.CENTER); // Add mainPanel to the central area
container.add(footerPanel, BorderLayout.SOUTH); // Add footePanel to the bottom
frame.add(container);
frame.pack();
frame.setVisible(true);
The BorderLayout determines that a component added to the CENTER area will expand both horizontally and vertically to follow the container. The SOUTH area can only expand horizontally while EAST and WEST can only expand vertically. Keep in mind that every layout manager class has its own rules on how to divide the container space among components and how they are resized.

Categories