posted 8 years ago
I was used to create 90% of the UI dynamically, removing and rebuilding everything inside the frame as needed on the fly, but recently people have advised me to not do it like that. So, on my current project I am trying to have everything already created as the program launches.
The following method is called inside initcomponents() to create one of the panels inside my frame. As you can see, the panel has a JList referencing custom objects; an area with CardLayout storing a card with components for each item from the JList; and a JButton.
I tried to make the code short, but probably failed, sorry.
Next is the JButton actionlistener.
As you can see, an optionPane gets input from the user and puts it inside an object, then I immediatly update the UI to show the respective card with the new components for the recently acquired input.
At first I tried to call just revalidate and repaint on the parent panel hoping that it would work, but nothing changed. What did work was also calling removeAll and the entire method to dynamically recreate the whole thing, which is exactly what I was advised against.
Was the advice wrong? Is it just this particular case? Was there another way of solving my problem?