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

Recycle/Re-use  RSS feed

 
Carol Murphy
village idiot
Bartender
Posts: 1211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to figure out what was causing a null pointer exception in the application I have been modifying.
I was trying to add a delete function to an app coded by someone else, so I decided to try and re-use as much as I could of his code, and just make the additions I needed.
Am I correct in saying that if a JPanel ( say as a buttonPanel component) has been added to one card in a JPanel using the cardLayout manager, that variable referencing that same component panel cannot be used in a second card at the same time?
It seems that I had to create both a labelPanel and a buttonPanel for each of my cards, even though they were identical items for each card.
When I re-used the variable, the panel wouldn't appear in the first card it was added to, but would appear in the last card to which it had been added.
Am I correct, or is something more sinister and insidious going on in the background?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add a component to a container and then add the same component to another container (or different section of the same container) the component is removed from the first container and added to the second container. So it makes sense that the same component will only show up in the last container to which you added it.

And it is okay to use the same reference repeatedly as long as you do not intend to use it to look up any of the components. So

works fine for layouts in which you don't care to use the reference ('panel') to get back to one of the instances you added to a container. You can always use Container methods like 'getComponents' to get a reference when needed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!