views:

25

answers:

2

Hi,

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.

A: 

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

  validate();
  repaint();
Prine
Yes, when I call these methods after the GUI changes it still does not show anything. It's as if it only removed the panel value_fields from the borderlayout as it's just empty at that place still. Any idea?
Skyfe
A: 

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.

camickr
Hi, I tried it and it worked for removing components from the panel. However when I try to add components to the panel dynamicly this way, it does not work.Works: values_fields.remove(vfields[blocks-1]); values_fields.validate(); values_fields.repaint();Does not work: values_fields.add(vfields[blocks]); values_fields.validate(); values_fields.repaint();
Skyfe
Update: I seem to get a Java NullPointerException on the part where I use the add method: values_fields.add(vfields[blocks]). What's wrong?
Skyfe
If it doesn't work when you add components, then its probably because you are using a layout manager that requires you to specify constraints. This will happen for example when you use an IDE to create your form. Most IDE's use the GroupLayout which require you to specify several constraints. My advice is to dump the IDE and code the forms yourself so you know how to add components and how to specify the required constraint.
camickr
Thanks for your reply, I've got it working now; using your methods. I made some silly mistake: I refered to the object I created ( vfields[blocks] ) the same way as I created it (using as well the value of blocks as index) but inbetween creating the object and using it I increased the value of blocks which made it refer to a yet non-existing object, so I moved the increasing of the int blocks to after it and it's working now. Thanks a lot!
Skyfe