File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes JPanel Visible Answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

JPanel Visible Answer

Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

code:


package BouncingBalls;

import java.awt.*;
import javax.swing.*;

public class Main {

public static void main(String[] args) {

JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400, 400));
p.setVisible(true);

}

}




Simply, why the JPanel is not visible when running this. Nothing appears and there are no errors.

Thanks



ANSWER:

import java.awt.*;
import javax.swing.*;

public class Main {

public static void main(String[] args) {

JFrame f = new JFrame("My Frame");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400, 400));

f.getContentPane().add(p);
f.pack();
f.setVisible(true);

}

}
Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

Sorry, the other thread was closed so I started another...

Short answer is that you have to add the JPanel to a top level container.

http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
No, your other thread wasn't closed. It was moved.

This one is closed however.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
I must apologise; I thought at first you were the original poster.

You must have been trying to post at the same time as I moved the original thread, so it would have refused the reply. Although your reply was helpful, I can't drag the thread back from the Swing forum.

So this thread will have to stay closed, but I am sorry for messing you about, and would like to make you welcome here at JavaRanch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel Visible Answer