Readding panel to layout after editing panel?

2019-04-17 09:39发布

问题:

What I'm trying to do is dynamicly edit a panel and readd it to the (Border)layout. The panel contains textfields and I want the user to be able to add or remove textfields to the panel. What I tried is the following: remove the panel from the layout, add another textfield to the panel, readd the panel to the layout. However this doesn't work ( nothing happened; only the panel was removed but not readded with the new textfield in it , so the area was just empty ). Even when I removed the panel from the layout and then added another component to the layout at that position (BorderLayout.EAST) instead; nothing happened ( just empty ).

This is the part of the code which is not working:

vfields[blocks] = new TextField("0"); //add new textfield to the array of textfields
blocks    += 1;
dp.blocks = blocks;
this.remove(values_fields); //remove the values_fields panel from the borderlayout
values_fields.add(vfields[blocks]); //add new component,textfield to the panel
this.add(values_fields, BorderLayout.EAST); //readd the panel to the border layout<-- doesn't work

I hope anyone can help me out. I'm relative new to Java so I might just be doing something totally wrong or something, but I don't see it myself.

Thanks in advance!

Skyfe.

回答1:

There is no need to remove the entire panel. You can just remove/add components to the existing panel. On a visible GUI the order of the code would be:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

If you need more help post your SSCCE.



回答2:

It looks like it doesnt get repaint again. Have you tried to call the following two methods after the gui changes:

  validate();
  repaint();