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.
Related
I have a pretty straight forward question. Can some please please explain to me why the following JFrame is not showing Hello (100,100 pixels on the left side of the screen and World (100,100 pixels) on the right side of the screen since I am using border layout.
I created a JFrame
Assigned it a layout of borderlayout
Created 2 panels with 2 labels and assigned the panels to be aligned left and right.
added the panels to the JFrame
Displayed the JFrame
What am I missing?
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(100,100));
panel1.setBackground(Color.BLUE);
JLabel label1 = new JLabel("Hello");
label1.setBackground(Color.YELLOW);
label1.setForeground(Color.WHITE);
panel1.add(label1,BorderLayout.LINE_START);
frame.add(panel1);
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(100,100));
panel2.setBackground(Color.RED);
JLabel label2 = new JLabel("World");
label2.setBackground(Color.CYAN);
label2.setForeground(Color.WHITE);
panel2.add(label2,BorderLayout.LINE_END);
frame.add(panel2);
You are setting the Layout-Constraints on the wrong panel.
Instead of panel2.add(label2,BorderLayout.LINE_END); it should be panel2.add(label2) and instead of frame.add(panel2); it should be frame.add(panel2, BorderLayout.LINE_END);.
Same for panel1.
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.
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);
}
}
My code is essentially as follows
JPanel x = new JPanel();
JPanel y = new JPanel();
JPanel rowPanel = new JPanel(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(y);
Container c = frame.getContentPane();
rowPanel.add(x, BorderLayout.LINE_START);
frame.add(rowPanel);
c.add(scrollPane);
frame.setVisible(true);
except that its in a for loop to create a lot of those in a gridlayout on the frame. The thing I want to do is put that scroll pane in the rowPanel as center but I'm pretty sure I have to add the scroll pane with the container and I don't know how to specify to the container to add the scroll pane there
So as it turns out I don't have to add the scroll pane with a container, I can add it directly to the panel by just puttting
rowPanel.add(scrollPane, BorderLayout.CENTER);
I recently started working with Java and I am not too sure how to put my BoxedLayout Panel in the middle of my `JFrame. At the moment, I have the following:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel quizLabel = new JLabel("Java Quiz",SwingConstants.CENTER);
quizLabel.setForeground(Color.BLUE);
quizLabel.setFont(new Font("Arial", Font.BOLD, 20));
quizLabel.setOpaque(true);
panel.add(quizLabel);
JLabel newLineLabel = new JLabel(" ",SwingConstants.CENTER);
newLineLabel.setOpaque(true);
panel.add(newLineLabel);
JLabel createdByLabel = new JLabel("Created By",SwingConstants.CENTER);
createdByLabel.setOpaque(true);
panel.add(createdByLabel);
JLabel nameLabel = new JLabel("XXX",SwingConstants.CENTER);
nameLabel.setOpaque(true);
panel.add(nameLabel);
contentPane.add(panel, BorderLayout.CENTER);
contentPane is taken from my frame. This gives me the following output:
I want the three labels inside the panel to appear in the middle of the Frame.
Because it is the only panel on the screen, the BoxLayout will fill the entire frame and thus depending on how your JComponents are created in the panel, it will show it like that on the frame too.
What I would do if I were you, is created a BorderLayout as a container for your BoxLayout.
This way, you can set your BoxLayout as the center of the Borderlayout.
See if this code works:
//This will fill your frame
JPanel containerPanel = new JPanel(new BorderLayout());
contentPane.add(containerPanel);
//this is the BoxPanel you wnat your components to be organized in
JPanel boxPanel = new JPanel(new BoxLayout());
//Add all your components to the boxPanel
//add your panel with all the components to the container panel
containerPanel.add(boxPanel, BorderLayout.CENTER);
The easiest way is to use a GridBagLayout. Using the default constraints a single component will be centered in the panel:
//contentPane.add(panel, BorderLayout.CENTER);
contentPane.setLayout( new GridBagLayout() );
contentPane.add(panel, new GridBagConstraints());