MigLayout Specific Layout - java

I have been trying to make a layout with MigLayout that consists of 3 panels, one on the right, one on top, and one that fills the rest of the space.
Like this:

There are several solutions possible. I provide two examples.
In both examples we also set the preferred width and height of
the panels. Normally, the panels would derive their preferred with from
their children.
The first layout is done with the dock constraint.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutThreePanels extends JFrame {
public MigLayoutThreePanels() {
initUI();
setTitle("Three panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout());
JPanel pnl1 = new JPanel();
pnl1.setBorder(BorderFactory.createEtchedBorder());
JPanel pnl2 = new JPanel();
pnl2.setBorder(BorderFactory.createEtchedBorder());
JPanel pnl3 = new JPanel();
pnl3.setBorder(BorderFactory.createEtchedBorder());
add(pnl2, "w 40, dock east");
add(pnl1, "h 40, dock north");
add(pnl3, "w 250, h 200, dock center");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutThreePanels ex = new MigLayoutThreePanels();
ex.setVisible(true);
}
});
}
}
The three panels are docked to east, north, and center. Additional spaces between
panels can be created with the gap constraints.
In the second example, we use utilize the grid structure.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutThreePanels2 extends JFrame {
public MigLayoutThreePanels2() {
initUI();
setTitle("Three panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("gap 3 3"));
JPanel pnl1 = new JPanel();
pnl1.setBorder(BorderFactory.createEtchedBorder());
JPanel pnl2 = new JPanel();
pnl2.setBorder(BorderFactory.createEtchedBorder());
JPanel pnl3 = new JPanel();
pnl3.setBorder(BorderFactory.createEtchedBorder());
add(pnl1, "h 40, growx");
add(pnl2, "w 40, spany 2, growy, wrap");
add(pnl3, "w 250, h 200, grow, push");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutThreePanels2 ex = new MigLayoutThreePanels2();
ex.setVisible(true);
}
});
}
}
In this example, we also set some gaps between the cells and container insets.

Related

Automatically Resizing with BorderLayout in Java

This is my jFrame Form with BorderLayout where buttons will be placed in Navigation Bar Panel and jDesktopPane will be placed in Content Panel. The DesktopPane with CardLayout will be displaying different sizes of jPanel Form. I want Content Panel(including whole form) resize based on the different sizes of jPanel Form displayed. Is it possible to do this? If not then I've tried resizing the panel and even the whole form with codes, but it's not working.
I've trying playing with these few codes, but it's not working.
Main_Menu form = new Main_Menu();
form.pack();
form.setSize(900, 548);
form.setPreferredSize(new Dimension(900, 548));
form.validate();
So, based on the limited information available, I wrote a quick test which seems to work just fine
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
CardLayout layout = new CardLayout();
JDesktopPane pane = new JDesktopPane();
Navigator navigator = new Navigator(pane, layout);
pane.setLayout(layout);
pane.setBackground(Color.GREEN);
frame.add(new TopPane(), BorderLayout.NORTH);
frame.add(new NavigationPane(navigator), BorderLayout.WEST);
for (int index = 0; index < 5; index++) {
pane.add(new ContentPane(index), Integer.toString(index));
}
JLabel initial = new JLabel("All your content belong to us");
initial.setBorder(new EmptyBorder(20, 20, 20, 20));
pane.add(initial, "initial");
layout.show(pane, "initial");
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Navigator {
private JComponent parent;
private CardLayout layout;
public Navigator(JComponent parent, CardLayout layout) {
this.parent = parent;
this.layout = layout;
}
public void show(String name) {
layout.show(parent, name);
}
}
public class TopPane extends JPanel {
public TopPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));
setBackground(Color.BLUE);
JLabel title = new JLabel("Top Panel");
title.setForeground(Color.WHITE);
add(title);
}
}
public class NavigationPane extends JPanel {
private Navigator navigator;
public NavigationPane(Navigator navigator) {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));
setBackground(Color.ORANGE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.BOTH;
for (int index = 0; index < 5; index++) {
JButton btn = new JButton("Test " + index);
btn.setActionCommand(Integer.toString(index));
add(btn, gbc);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
navigator.show(name);
}
});
}
}
}
public class ContentPane extends JPanel {
public ContentPane(int value) {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));
setBackground(Color.MAGENTA);
add(new JLabel("Hello from " + Integer.toString(value)));
}
}
}
Avoid setPreferred/Minimum/MaximumSize, you're overriding the work which the components and the layout managers do in order to provide dynamic sizing hints
If this fails to solve your issue, then consider providing a Minimal, Complete, and Verifiable example
Try to use pack(); not form.pack();. Or setLayout or use CardLayout

JPanel fill the whole JFrame

I want to place a JPanel with 300,200 size at 100,50 location in a JFrame, but the following code makes the whole JFrame to be filled with the JPanel, where am I wrong?
Code:
public class Class1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(700, 500);
panel.setSize(300, 200);
panel.setBackground(Color.green);
panel.setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
If I change BorderLayout.SOUTH or NORTH, the JPanel looks like a thin line at the bottom or top of the JFrame. Also I tried to use preferred size but its still the same result.
You need to understand the basics of Layout Managers before you can attempt anything with Swing.
Study the tutorial here https://docs.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html
If you are trying to construct a form with various Swing controls, never use absolute coordinates to position them. Resizing the window should dynamically reposition your controls according to the type of layout you want to use. If you are using your panel as a drawing canvas (which is what I think you are after), look at my example below.
Try to master a couple of fundamental layouts first such as BorderLayout and GridLayout. Once mastered you can use very few to achieve just about any kind of layout you desire by nesting them together. Also learn the basics of ScrollPane to make efficient use of screen real-estate
As for your original question, I created an example of how BorderLayout works and how to draw to a specific region on a panel. I also added some code so you can set your cursor differently depending on whether you are in the smaller region within a given panel.
Try this:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class FrameDemo extends JFrame {
private static final long serialVersionUID = 1L;
public FrameDemo() {
super("Frame Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(new CustomPanel(Color.RED), BorderLayout.NORTH);
getContentPane().add(new CustomPanel(Color.GREEN), BorderLayout.SOUTH);
getContentPane().add(new CustomPanel(Color.BLUE), BorderLayout.EAST);
getContentPane().add(new CustomPanel(Color.YELLOW), BorderLayout.WEST);
getContentPane().add(new JScrollPane(new CustomPanel(Color.BLACK)), BorderLayout.CENTER);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new FrameDemo();
frame.setVisible(true);
}
});
}
}
class CustomPanel extends JPanel implements MouseMotionListener {
private static final long serialVersionUID = 1L;
private static final Dimension PANEL_SIZE = new Dimension(200, 100);
private static final int HAND_CURSOR_INDEX = 1;
private static final int DEFAULT_CURSOR_INDEX = 0;
private static final Cursor[] _cursors = new Cursor[] {
Cursor.getDefaultCursor(),
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
};
// I want to place a JPanel with 300,200 size at 100,50 location in a JFrame
private static final Rectangle _rectangle = new Rectangle(50, 10, 40, 50);
public CustomPanel(Color color) {
setBackground(color);
addMouseMotionListener(this);
}
public Dimension getPreferredSize() {
return PANEL_SIZE;
}
public Dimension getMinimumSize() {
return PANEL_SIZE;
}
public Dimension getMaximumSize() {
return PANEL_SIZE;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(
(int)_rectangle.getX(),
(int)_rectangle.getY(),
(int)_rectangle.getWidth(),
(int)_rectangle.getHeight());
}
#Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent e) {
int cursorIndex = _rectangle.contains(e.getPoint()) ?
HAND_CURSOR_INDEX :
DEFAULT_CURSOR_INDEX;
setCursor(_cursors[cursorIndex]);
}
}
If you want the panel to occupy part of the JFrame (contentpane) you can use other layout managers. He is an example using GroupLayout:
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Class1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.getContentPane().setPreferredSize(new Dimension(700, 500) );
panel.setPreferredSize(new Dimension(300, 200));
panel.setBackground(Color.green);
panel.setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(104)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 493, GroupLayout.PREFERRED_SIZE)
.addContainerGap(103, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(100)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
output:
Another example using BorderLayout with 4 additional "place holder" or feeler panels :
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
public class Class1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel centerPanel = new JPanel();
centerPanel.setPreferredSize(new Dimension(300, 200));
centerPanel.setBackground(Color.green);
centerPanel.setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR));
frame.getContentPane().add(centerPanel);
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.RED);
northPanel.setForeground(Color.BLACK);
northPanel.setPreferredSize(new Dimension(0, 150));
frame.getContentPane().add(northPanel, BorderLayout.NORTH);
JPanel westPanel = new JPanel();
westPanel.setBackground(Color.MAGENTA);
westPanel.setPreferredSize(new Dimension(200, 0));
frame.getContentPane().add(westPanel, BorderLayout.WEST);
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.YELLOW);
southPanel.setPreferredSize(new Dimension(0, 150));
frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
JPanel eastPanel = new JPanel();
eastPanel.setBackground(Color.BLUE);
eastPanel.setPreferredSize(new Dimension(200, 0));
frame.getContentPane().add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
output :
import javax.swing.*;
import java.awt.*;
public class Class1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel another = new JPanel();
JPanel emptyPanel = new JPanel();
emptyPanel.setPreferredSize(new Dimension(700, 50));
frame.setSize(700, 500);
panel.setMaximumSize(new Dimension(300, 200));
panel.setMinimumSize(new Dimension(300, 200));
panel.setPreferredSize(new Dimension(300, 200));
panel.setBackground(Color.green);
panel.setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
another.add(emptyPanel, BorderLayout.NORTH);
another.add(panel, BorderLayout.CENTER);
frame.add(another);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
The created image looks like below
It looks to me that immediate child container of the JFrame is resized to the size of the JFrame, the top level container, itself.

Why is my JPanel hogging up the JFrame?

I am relatively new to programming, so I am sorry if this question is stupid. I am creating a Java program that involves one JButton inside a JPanel, and the JPanel is in a JFrame. Another button is outside the JPanel but still in the JFrame. I set the layout to a BoxLayout. My problem is that the the panel, which I made black, is taking up the whole JFrame except for where the second button is. How do I make the JPanel so it is only taking up the area right around the first button?
public class alt {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button1 = new JButton("button 1");
JButton button2 = new JButton("button 2");
public alt(){
frame.setVisible(true);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
panel.setBackground(Color.black);
frame.setTitle("test");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
panel.add(button1);
frame.add(panel);
frame.add(button2);
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
}
}
You could make use of a different layout manager, one which gives you more control over deciding how space is allocated and filling is handled, for example, GridBagLayout...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SampleLayout {
public static void main(String[] args) {
new SampleLayout();
}
public SampleLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel panel = new JPanel();
JButton button1 = new JButton("button 1");
JButton button2 = new JButton("button 2");
panel.setBackground(Color.black);
panel.add(button1);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(panel, gbc);
frame.add(button2, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Take a look at Laying Out Components Within a Container for more details
The reason why your panel takes up the bulk of the frame's content pane lies
in the way the BoxLayout manager works with the minimum, preferred,
and maximum values of components. It takes the maximum value of a component
into account. And since the maximum value of a JPanel is huge, it takes
all the space available. The solution is to change the maximum value
of a panel. However, this is bad practice. I do not recommend to use
the BoxLayout manager -- it is very weak and leads to poor code.
I recommend to use either the MigLayout manager or the GroupLayout manager.
I provide three solutions: a corrected BoxLayout solution, a MigLayout solution,
and a GroupLayout solution.
BoxLayout solution
We determine the maximum size of the button and change the panel's size
to be a bit larger than the button's.
package com.zetcode;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoxLayoutPanel extends JFrame {
public BoxLayoutPanel() {
initUI();
}
private void initUI() {
JPanel cpane = (JPanel) getContentPane();
cpane.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
cpane.setLayout(new BoxLayout(cpane,
BoxLayout.Y_AXIS));
JPanel pnl = new JPanel();
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
Dimension dm = btn1.getMaximumSize();
dm.height += 15;
dm.width += 15;
pnl.setMaximumSize(dm);
pnl.setBackground(Color.black);
add(pnl);
add(Box.createVerticalStrut(10));
pnl.add(btn1);
btn2.setAlignmentX(Component.CENTER_ALIGNMENT);
add(btn2);
setTitle("BoxLayout solution");
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
BoxLayoutPanel ex = new BoxLayoutPanel();
ex.setVisible(true);
}
});
}
}
This is not a clean solution. Generally, we should avoid calling the getMaximumSize() and
the setMaximumSize() in the application code -- this is the layout manager's job. Also in three occasions, we use fixed pixel widths: when we define an empty border, a vertical strut, and a maximum panel's size. This code is however not portable.
Pixel widths change when the resolution of the screen changes. This is a
shortcoming of the BoxLayout manager.
MigLayout solution
This solution is much cleaner and more portable. MigLayout is a third-party
manager, so we need to download additional libraries.
package com.zetcode;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPanel extends JFrame {
public MigLayoutPanel(){
initUI();
setTitle("MigLayout solution");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JPanel main = new JPanel(new MigLayout("center"));
JPanel pnl2 = new JPanel();
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
pnl2.setBackground(Color.black);
pnl2.add(btn1);
main.add(pnl2, "wrap");
main.add(btn2, "alignx center");
add(main);
pack();
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutPanel ex = new MigLayoutPanel();
ex.setVisible(true);
}
});
}
}
GroupLayout solution
GroupLayout is a built-in layout manager. With MigLayout, they are the most
portable and flexible layout managers.
package com.zetcode;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.DEFAULT_SIZE;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
public class GroupLayoutPanel extends JFrame {
public GroupLayoutPanel(){
initUI();
setTitle("GroupLayout solution");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
JPanel pnl = new JPanel();
JButton btn1 = new JButton("Button 1");
pnl.add(btn1);
JButton btn2 = new JButton("Button 2");
pnl.setBackground(Color.black);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addContainerGap(DEFAULT_SIZE, Integer.MAX_VALUE)
.addGroup(gl.createParallelGroup(CENTER)
.addComponent(pnl, DEFAULT_SIZE, DEFAULT_SIZE,
PREFERRED_SIZE)
.addComponent(btn2))
.addContainerGap(DEFAULT_SIZE, Integer.MAX_VALUE)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addContainerGap()
.addComponent(pnl, DEFAULT_SIZE, DEFAULT_SIZE,
PREFERRED_SIZE)
.addPreferredGap(RELATED)
.addComponent(btn2)
.addContainerGap()
);
pack();
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GroupLayoutPanel ex = new GroupLayoutPanel();
ex.setVisible(true);
}
});
}
}

How to make a gap grow in MigLayout

I want to create a vertical list of items with a bottom status line such that when it gets resized, the space between the items and the bottom line grows. UsingMigLayout this must be pretty trivial, but somehow it isn't.
The following code does exactly what I want, but I had to use a component to do the spacing:
final JFrame frame = new JFrame();
final JPanel panel = new JPanel(new MigLayout("wrap, debug", "[grow, fill]", ""));
for (int i=0; i<5; ++i) {
final JEditorPane line = new JEditorPane();
line.setText("a" + i);
panel.add(line);
}
panel.add(new JLabel(), "push"); // This should be a gap!
final JLabel status = new JLabel("status line");
panel.add(status, "");
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
I guess I could specify the growing gap in the third constructor argument, but the list length is variable. This could be all solved, too, but I doubt it's the best practice. What I was hoping for, was something like gaptop push, but it seems to do nothing at all.
What am I doing wrong? Is there an argument doing what I want?
MigLayout is a very powerful manager indeed. This could be accomplished
in multiple ways. I provide three solutions.
Solution 1
We put a greedy gap between row 5 and 6.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGapGrow extends JFrame {
public MigLayoutGapGrow() {
initUI();
setTitle("Gaps");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap", "[grow, fill]", "[][][][][]20:push[]"));
add(new JTextField("a"+1));
add(new JTextField("a"+2));
add(new JTextField("a"+3));
add(new JTextField("a"+4));
add(new JTextField("a"+5));
final JLabel status = new JLabel("status line");
add(status);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutGapGrow ex = new MigLayoutGapGrow();
ex.setVisible(true);
}
});
}
}
Solution 2
We enlarge the last cell in which the label is placed. The label is then
aligned to the bottom.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGapGrow2 extends JFrame {
public MigLayoutGapGrow2() {
initUI();
setTitle("Gaps");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap, debug", "[grow, fill]"));
add(new JTextField("a"+1));
add(new JTextField("a"+2));
add(new JTextField("a"+3));
add(new JTextField("a"+4));
add(new JTextField("a"+5));
final JLabel status = new JLabel("status line");
add(status, "pushy, bottom");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutGapGrow2 ex = new MigLayoutGapGrow2();
ex.setVisible(true);
}
});
}
}
Solution 3
The label is attached to the bottom using relative positioning. Relative
positioning does not seem to work with the pack() method and this might
be a problem.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGapGrow3 extends JFrame {
public MigLayoutGapGrow3() {
initUI();
setTitle("Gaps");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap", "[grow, fill]"));
add(new JTextField("a"+1), "id a1");
add(new JTextField("a"+2));
add(new JTextField("a"+3));
add(new JTextField("a"+4));
add(new JTextField("a"+5));
final JLabel status = new JLabel("status line");
add(status, "pos a1.x visual.y2-p");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutGapGrow3 ex = new MigLayoutGapGrow3();
ex.setVisible(true);
}
});
}
}
If you add push on the component it has a different semnatic. From the docs:
Makes the row and/or column that the component is residing in grow with "weight". This can be used instead of having a "grow" keyword in the column/row constraints.
So you have to add the push keyword as the row constraint.

Java Swing BorderLayout resize difficulties

I want to have my screen split in two so I used a BorderLayout with East and West sections. I had problems resizing and here I eventually found out that width is not changed in the East and West panels and height is not changed in the North and South panels and both are changed in the Center panel.
However, I want both width and height to be changed upon resize, and have two panels side by side. I have tried various levels of nesting to try getting it to work but I do not think it will work with BorderLayout.
It seems like this should be easy for the default layout manager but maybe I should try a different layout (e.g. BoxLayout) to achieve what I want.
Also here is some code which replicates the problem I am talking about (try resizing the window):
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
public static void main(String[] args) {
JFrame window = new Main();
window.setVisible(true);
}
public Main() {
JButton east = new JButton("East");
JButton west = new JButton("West");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(east, BorderLayout.EAST);
content.add(west, BorderLayout.WEST);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Edit: I do not want the two sides to be equal, roughly 2:1 is the ratio which I want.
What you can use in your case is GridLayout, here two JButtons will resize themselves as the JFrame resizes.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame window = new Main();
window.setVisible(true);
}
});
}
public Main() {
JButton east = new JButton("East");
JButton west = new JButton("West");
JPanel content = new JPanel();
content.setLayout(new GridLayout(1, 2));
content.add(east);
content.add(west);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Moreover, it's always best to run your GUI related code from the EDT - Event Dispatch Thread, and not from the Main Thread. Do read Concurrency in Swing, for more info on the topic.
LATEST EDIT : As per requested comment
Use GridBagLayout to specify the size that you want to give
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame window = new Main();
window.setVisible(true);
}
});
}
public Main() {
JPanel east = new JPanel();
east.setOpaque(true);
east.setBackground(Color.WHITE);
JPanel west = new JPanel();
west.setOpaque(true);
west.setBackground(Color.BLUE);
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.3;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
content.add(east, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
content.add(west, gbc);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Why don't you try with JSplitPane:
import javax.swing.*;
import java.awt.*;
public class AppDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, eastButton, westButton);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(splitPane, BorderLayout.CENTER);
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 400));
frame.pack();
frame.setVisible(true);
});
}
}
You will get this:
If you want to keep your BorderLayout you can use something like the following object:
public class ResizablePanel extends JPanel {
public ResizablePanel(JComponent body) {
setLayout(new BorderLayout());
JButton resize = new JButton();
resize.setPreferredSize(new Dimension(Integer.MAX_VALUE, 4));
resize.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
Dimension preferredSize = ResizablePanel.this.getPreferredSize();
ResizablePanel.this.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height-e.getY()));
ResizablePanel.this.revalidate();
}
});
add(resize, BorderLayout.PAGE_START);
add(body, BorderLayout.CENTER);
}
}
Now wrap the part you want to resize with an instance of ResizablePanel and you'll be able to resize it by dragging the thin button.
Note that this is code is for resizing the height of a panel that you put at the bottom (PAGE_END) part of a border layout, but it should be fairly straightforward to change it for resizing the width.
Sorry about replying to an old post.
My fix is to still use BorderLayout but to throw in the following line after the Component is resized
getLayout().layoutContainer(this);

Categories