Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

UI ceation discussion with a personal example  RSS feed

 
Ekhard Aldric
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really sure I understand the question.

But generally what I think people are suggestion is that you create a panel to display the detail information of a client. For example maybe you have data like firstName, lastName, address etc. Then when you prompt for a client you get the client number and then just refresh the data on the panel. So you do something like:



So there is no need to recreate a panel just because you change the client you want to display information for.

Maybe there are times when you have data in a JList or JTable and you need to change the data. Again, there is no need to recreate the panel of the component. Instead you change the model of the component:



So there is no need to remove or create new components.

The other part of your question deals with the CardLayout. A CardLayout is used to display different panels in any order. So again the panels are created with components at design time. Then at run time you just populate the data as required. The Swing tutorial on How to Use Card Layout shows how to initially create panels and then swap tem as required.

There is no need for methods like removeAll().

Of course there are times when you need to dynamically create components so you have to look at each situation and decide which is the best approach to use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!