0

I have a JPanel which has GridLayout as the layout. I have a plus button at the end of the Main Panel to add some panels to the grid. I need a minus button that subsequently removes the last row added to the grid.

I saw the docs related and it shows removeAll() method, which is not useful for me as I need to remove only the last row.

Also it shows some remove(int index). Again I am unaware of the index of the last row, though I tried doing this:

myPanel.remove(0);

but this removes the first row.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user2176576
  • 734
  • 3
  • 14
  • 39

1 Answers1

1

If you want to remove the component at index x:

myPanel.remove(x);

If you want to add a component instead of the one you've removed:

myPanel.add(component c , x);

Then you should repaint:

myPanel.validate();
myPanel.repaint();
mostar
  • 4,723
  • 2
  • 28
  • 45
  • okey i am sorry : myPanel.remove(x); //where x is the index of the component you want to remove myPanel.add(component c , x); //if you want to add a component instead of the one you've removed myPanel.validate(); myPanel.repaint(); – Osama Najjar Dec 13 '14 at 23:16