I'm sorry, I still don't get it. This is a continuation of the question, not an attempt at an answer.
I have never been able to figure out GridBagLayout; it is, to me, a sterling example of a component that is not explained. I've never seen an explanation of many of its constraints, etc., just examples. But what I want to do never looks like the examples, and I hate futzing with it by trial and error, hoping to happen on a combination that does what I want. What a way to program.
EDIT: Ok, now it's an answer, at least to the continuation question. Through typical trial-and-error I believe I have discovered the missing principle, and provide it here so it can get lost while someone looks in the official documentation for it.
The Oracle "How to use GridBagLayout" contains the following: "...GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be"
It is easy to miss the implication of this: gridbag determines the width of a column and the height of a row by using the preferred sizes of all the components in that column/row. Think of GBL, at time of realization, as going through all the components that are going to appear in a column, determining the largest preferred-size width, and making that the width of the first column, etc.
The code below attempts to treat the GridBagLayout panel as a grid with columns and rows determined by the 'x','y','width', and 'height' attributes of the components within the panel, and GBL evidently ignores that.
So that's why my program below doesn't do what I expected; I could fiddle with setting the preferred sizes of the components, raising the ire of those who claim programs should not do that. If I did have this problem to solve, I wouldn't, in fact, do that.
I can't suggest anything definitive to the OP; after this exercise, I still prefer GroupLayout (with some additions of my own) for laying out components. To choose a good way to solve the original problem, we would need to know more about how the sizes of the components are chosen and what flexibility the OP needed.
Here's what I've gotdoesn't work:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
/**
* trying to do this:
* <pre>
* +----------------+----------------+--------+
* | | | |
* | tile 1 | tile 3 | |
* | | | |
* +----------------+----------------+ tile 5 |
* | | | |
* | | | |
* | | | |
* | tile 2 +----------------+--------+-------+
* | | | |
* | | tile 4 | tile 6 |
* | | | |
* |----------------+----------------+----------------+
*
* </pre>
*/
public class Tiles extends JFrame
{
private static final long serialVersionUID = 1L;
private GridBagConstraints constraints = new GridBagConstraints();
private Border lineBorder = BorderFactory.createLineBorder(Color.black);
JPanel gblPanel = new JPanel(new GridBagLayout());
public static void main(String[] args)
{
Tiles tiles = new Tiles();
tiles.go();
}
public void go()
{
createUI();
setVisible(true);
}
private void createUI()
{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addLabelToGblPanel("1", 2, 1, 0, 0); // label, width, height, x position, y position
addLabelToGblPanel("3", 2, 1, 2, 0);
addLabelToGblPanel("5", 1, 2, 4, 0);
addLabelToGblPanel("2", 2, 2, 0, 1);
addLabelToGblPanel("4", 2, 1, 2, 2);
addLabelToGblPanel("6", 2, 1, 4, 2);
getContentPane().add(gblPanel);
pack();
}
private void addLabelToGblPanel(String num, int width, int height, int x, int y)
{
String numString = "Tile " + num;
JLabel tile = new JLabel(numString, SwingConstants.CENTER);
tile.setBorder(lineBorder);
constraints.ipadx = 25;
constraints.ipady = 25;
constraints.gridwidth = width;
constraints.gridheight = height;
constraints.gridx = x;
constraints.gridy = y;
// constraints.fill = GridBagConstraints.BOTH;
// constraints.weightx = 0.5;
// constraints.weighty = 0.5;
gblPanel.add(tile, constraints);
}
}
Obviously I've got something wrong; what I've got shows up as a 3x2 grid. The sizes and positions I've put onto them make no difference whatsoever. Is there anyone who can explain how the constraints work to make it do this?
This is what drove me to learn how to do GroupLayout by hand...