What is the difference between these two constraints?
From documentation:
PUSH - makes the row and/or column that the component is residing in grow with "weight"
GROW - Sets how keen the component should be to grow in relation to other component in the same cell.
So, the main idea is to shrink size inside and outside of component?
It is important to understand that fill
, (column, row) grow
, push
work in conjunction
with the (component) grow
to define the layout. (There are two different grow
constraints that do different things.)
MigLayout
is a grid-based manager. (More precisely, its most important mode is.)
There are two steps that need to be done:
- define how much space the columns and rows of the grid take
- define how much space components occupy in their alotted cell(s)
This is what fill
, (column, row) grow
, push
and (component) grow
constraints
help achieve. The first three define the growth of the grid's columns and rows,
the last one defines how the component will spread over its
alotted area, e.g. cell(s) that it is placed in. Note that filling or growing is the tendency or eagerness to occupy
empty space in the layout. The window area that is not taken by columns, rows, or
components is filled with empty space.
The push
and the (column, row) grow
take optional weight
parameter. It
defines how keen the column or row should grow in relation to other columns
and rows. The fill
constraint distributes the weight
evenly.
(The push
constraint may be used to make gaps greedy in different context.)
I provide three examples that clarify usage of these constraints.
Fill
The fill
constraint affects all cells of the grid. They occupy all available space.
The (component) grow
constraint specifies how the components spread in their cells.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutFillEx extends JFrame {
public MigLayoutFillEx() {
initUI();
setSize(300, 250);
setTitle("Fill");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("fill"));
pnl.add(getLabel("Area 1"), "cell 0 0, growx");
pnl.add(getLabel("Area 2"), "cell 0 1, grow");
pnl.add(getLabel("Area 3"), "cell 1 0 1 2, grow");
add(pnl);
}
private JLabel getLabel(String text) {
JLabel label = new JLabel(text, JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder());
return label;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutFillEx ex = new MigLayoutFillEx();
ex.setVisible(true);
}
});
}
}
In the example we have three labels.
JPanel pnl = new JPanel(new MigLayout("fill"));
The fill
is a layout constraint; it affects all cells.
pnl.add(getLabel("Area 1"), "cell 0 0, growx");
pnl.add(getLabel("Area 2"), "cell 0 1, grow");
pnl.add(getLabel("Area 3"), "cell 1 0 1 2, grow");
Now we define how the components fill their areas alotted by the
layout manager.
The Area 1 label fills its alotted area horizontally, the other
two labels fill the alotted area in both dimensions.
Column, row grow
The (column, row) grow
constraint affects all cells in particular column
or row.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGrowEx extends JFrame {
public MigLayoutGrowEx() {
initUI();
setSize(300, 250);
setTitle("Grow constraint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("wrap", "[grow]", "[][grow]"));
JTextField field = new JTextField(10);
JTextArea area = new JTextArea(10, 10);
pnl.add(field, "growx");
pnl.add(new JScrollPane(area), "grow");
add(pnl);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutGrowEx ex = new MigLayoutGrowEx();
ex.setVisible(true);
}
});
}
}
In the example we have two components. The text field is expected to grow horizontally,
the text area both horizontally and vertically.
JPanel pnl = new JPanel(new MigLayout("wrap", "[grow]", "[][grow]"));
In this line we specify that the first colum and the second row of the
grid grow.
pnl.add(field, "growx");
pnl.add(new JScrollPane(area), "grow");
Now we define that the text field fills its alotted area horizontally while
the text area fills the whole alotted area.
Push
The push
constraint is esentially the same as the (column, row) grow
. The
difference is that push
is specified at the add()
method.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPushEx extends JFrame {
public MigLayoutPushEx() {
initUI();
setSize(300, 250);
setTitle("Push constraint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("wrap"));
JTextField field = new JTextField(10);
JTextArea area = new JTextArea(10, 10);
pnl.add(field, "pushx, growx");
pnl.add(new JScrollPane(area), "push, grow");
add(pnl);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutPushEx ex = new MigLayoutPushEx();
ex.setVisible(true);
}
});
}
}
The example is the same as the previous one.
JPanel pnl = new JPanel(new MigLayout("wrap"));
No growing is specified here.
pnl.add(field, "pushx, growx");
pnl.add(new JScrollPane(area), "push, grow");
Everything is specified using components' constraints within the add()
methods.