Java Swing: Components don't show up

Posted on 2011-09-21
Last Modified: 2012-05-12
I am adding a JPanel to another JPanel. The problem is that the inner JPanel does not show up until I click at the location where it is supposed to appear.

Please let me know what I could be missing. Thanks!
Question by:dshrenik
  • 4
  • 2
  • 2
  • +2
LVL 41

Accepted Solution

HonorGod earned 125 total points
ID: 36576048
Are you adding the second JPanel after the container has been painted?

Consider calling the frame.repaint() method, int, int, int, int)
LVL 47

Expert Comment

ID: 36576054
Maybe you want to paste your code.
It is very common to add one JPanel to another JPanel - should not have any problems

Author Comment

ID: 36576086
Unfortunately, I'm not allowed to paste code. Sorry about that!
Can you tell me some possible things that could be going wrong?
LVL 47

Expert Comment

ID: 36576105

I think HonorGod has a good point - are you indedadding the second ;panel after you show the window?
LVL 47

Expert Comment

ID: 36576116
In general, unless there is some specvial need - just create all GUI first and then show
the window when all elements are already created and are in place, then say
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline


Author Comment

ID: 36576127
I am not sure. Sorry if question sounds silly - I am new to Swing:
What is the frame here, and how do I get a reference to it?
Do I just say this.repaint()? (where this is a jpanel)?
LVL 86

Assisted Solution

CEHJ earned 125 total points
ID: 36576218
If you're adding it dynamically, for parent JPanel 'p' do

Open in new window

LVL 47

Assisted Solution

for_yan earned 125 total points
ID: 36576259

If you say this.setVisioble(true) when you shoow it, then you should say this.repaint() after you add a new panel

If you say frame.setVisible(true); then you should asy frame.repaint();

Better don't setVisible() before you add everything to your GUI
LVL 10

Assisted Solution

gordon_vt02 earned 125 total points
ID: 36580582
What Layout are you using?  I've seen that happen before where multiple components are added to a Layout in the same location -- or when you don't specify a location properly for the Layout.
LVL 41

Expert Comment

ID: 36710057
Thanks for the grade & points.

Good luck & have a great day.

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now