This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

JPanel not showing up dynamically inside JLayeredPane  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JLayeredPane and it contains a JPanel to start with. Now I want another smaller JPanel to display on top of the previous JPanel inside the JLayeredPane dynamically. By dynamically I mean when a certain event occurs, so I put its code inside the event handler for the event as well.

The problem is that sometimes the JPanel shows up as expected while other times it doesn't. And I cannot even predict what makes it show up when it does, because it is very random.

Following is the code for the JPanel that I want to display dynamically...

Dont worry about the class and the constructors being private, because this is an inner class for the Board class. It is accessed from a method inside that class. Here is how I am calling the promotion options to display dynamically...

Here layeredPaneContainer is the reference to the JLayeredPane that I created inside another class and passed to this Board class through the constructor. Here is the code for where I created the JLayeredPane object...

It would be really helpful if someone can point out the bug.
Thank you for your time...
Posts: 63843
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please read this to see what RC means.
Chandrachur Mukherjee
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After hours of frustrated debugging, I finally somehow made it work correctly. What I did was I added SwingUtilities.invokeLater() method and passed the code inside it like this..

Although I made it to work somehow, I am not completely satisfied yet. I dont understand when I need to use the invokeLater() method and when I donot need to use it. I have made a few GUI applications before and there also I had to add components dynamically, and I could do it without this passing my code inside this method.

Can someone please clarify when and when not to use this invokeLater(). Also for the record, I already have read the Java docs about this method, I just need a little non technical explanation for this.

Thanks in advance...
Crusading Chameleon likes the size of this ad:
global solutions you can do at home or in your backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!