Ive got an Application that should display tiles of different "sizes" (better different spans) in different positions on a 8x6 Grid. These positions change and new tiles are added at runtime. I tried different Layouts to accomplish this but without success. BoxLayout: No Chance to span Components over multiple Cells GridLayout: No Chance to span Components over multiple Cells GridBagLayout: I used this Layout until I realized that when I try to merge two cells the layout just expands the cell where the component is in but not merges the cells. This is also described in the documentation
GridBagLayout does not allow components to span multiple rows
.
Example:
How it looks when i add dummy panels to my GridBagLayout:
How it actually look when I add my tiles:
How it should look like:
Is there a Layout that can accomplish this task or is there another possibility to get this thing to work (JTable?!?)? Would be great if you could provide me with some new ideas (when possible with some tutorials). I use Netbeans 7.3 without any GUI Builder extensions.
Code EDIT:
Layout (created by Netbeans)
java.awt.GridBagLayout contentPaneLayout = new java.awt.GridBagLayout();
contentPaneLayout.columnWidths = new int[] {240};
contentPaneLayout.rowHeights = new int[] {160};
contentPane.setLayout(contentPaneLayout);
Adding a Panel (called for every Panel)
GridBagConstraints c = new GridBagConstraints();
c.gridx = positionx;
c.gridy = positiony;
c.insets = new Insets(2, 2, 2, 2);
c.weightx = width == 0 ? 2 : width;
c.weighty = height == 0 ? 2 : height;
c.anchor = GridBagConstraints.FIRST_LINE_START;
contentPane.add(myPanel, c);
With the help of the answer of Oliver I managed to get the layout to work. Oliver is right, in my orginal code I mixed up weightX and gridWidth (due to various tries). But when I just exchange weigthX/Y with gridWidth/Height then no tiles are shown. So i needed to define a weight of 1 as well.
So the code to add a panel looks like this:
GridBagConstraints c = new GridBagConstraints();
c.gridx = positionx;
c.gridy = positiony;
c.insets = new Insets(2, 2, 2, 2);
c.gridwidth = width == 0 ? 2 : width;
c.gridheight = height == 0 ? 2 : height;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.FIRST_LINE_START;
contentPane.add(myPanel, c);
But GridBagLayout still merged my rows and columns I had to add some dummy panels as described in the question GridBagLayout in Java Column Issue as well (before adding my tiles)
for (int i = 0; i < columnCount; i++) {
for (int j = 0; j < rowCount; j++) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = i;
c.gridy = j;
c.insets = new Insets(2, 2, 2, 2);
c.weightx = 1;
c.weighty = 1;
c.gridheight = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.FIRST_LINE_START;
NewJPanel p = new NewJPanel(); //A JPanel I created with the same background, width and height of a cell. May work with a normal JPanel as well.
//p.setVisible(false);
p.setOpaque(false);
contentPanel.add(p, c);
}
}
My tiles extend JPanel and are created like this (don't know if needed):
Size size = new Dimension(cellWidth * gridbagconstraint.gridwidth, cellHeight * gridbagconstraint.gridheight);
setSize(size);
setMaximumSize(size);
setMinimumSize(size);
setPreferredSize(size);
Thank you guys for your support!