Can I add a component to a specific grid cell when a GridLayout is used?

83,148

Solution 1

No, you can't add components at a specific cell. What you can do is add empty JPanel objects and hold on to references to them in an array, then add components to them in any order you want.

Something like:

int i = 3;
int j = 4;
JPanel[][] panelHolder = new JPanel[i][j];    
setLayout(new GridLayout(i,j));

for(int m = 0; m < i; m++) {
   for(int n = 0; n < j; n++) {
      panelHolder[m][n] = new JPanel();
      add(panelHolder[m][n]);
   }
}

Then later, you can add directly to one of the JPanel objects:

panelHolder[2][3].add(new JButton("Foo"));

Solution 2

Yes

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2,2,1,1));
    JButton component= new JButton("Component");
    panel.add(component, 0,0 );

Create your panel and set its layout.
new GridLayout(numberOfRows,numberOfColums,HorizontalGap,VerticalGap)

(new GridLayout(2,2,1,1))  => here i want 2 rows, 2 columns,
- if any horizontal gaps (HGap), they should be 1px (1unit)
- I also want the same for vertical gaps so i do same as vertical gaps(VGap). i.e 1 unit
- In this case; gaps  => spacing/margins/padding --in that sense.

Create your components and add them to the panel
- (component, 0,0 )  => 0,0 is the row and column.. (like a 2d array). @row 0 & @column 0 or at intersection of row 0 and column 0
specify where your component goes by putting the row and column where it should go.
each cell has a location == [row][column]

Or you can do it without hgaps and vgaps:

    JPanel panel = new JPanel();        
    panel.setLayout(new GridLayout(2,2));
    JButton component= new JButton("Component");
    panel.add(component, 0,0 );
Share:
83,148
Roman
Author by

Roman

Updated on July 21, 2022

Comments

  • Roman
    Roman almost 2 years

    When I set the GridLayout to the JPanel and then add something, it is added subsequently in the "text order" (from left to right, from top to bottom). But I want to add an element to a specific cell (in the i-th row in the j-th column). Is it possible?