Netbeans problem with JPanel

N

NickPick

I created a new JPanel with NetBeans and I'm instantiating the class
with

NewJPanel nj = new NewJPanel();

in the main method. Shouldn't the JPanel window appear automatically
now when I execute the main class? It doesn't. Can anybody tell me
why?

many thanks
 
K

Knute Johnson

NickPick said:
I created a new JPanel with NetBeans and I'm instantiating the class
with

NewJPanel nj = new NewJPanel();

in the main method. Shouldn't the JPanel window appear automatically
now when I execute the main class? It doesn't. Can anybody tell me
why?

many thanks

A JPanel is not a window. You have to put it in a window if you want to
see it.
 
L

Lew

Mark said:
Two things here: first you need a top level window to display something.

Actually, first go here:
That's JFrame, JDialog, JWindow ... one other I think, but mostly
you'll use JFrame.

NewJPanel nj = new NewJPanel();
JFrame jf = new JFrame();
jf.add( nj );

Next, you need to call setVisible to show the window.

jf.setVisible( true );

As an aside, these actions must be done on the EDT or you risk
catastrophe. See here:

<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html>

Part of
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html>
which is a chapter in the very useful
<http://java.sun.com/docs/books/tutorial/uiswing/index.html>

The documentation makes an excellent starting place for this kind of stuff,
doesn't it?
 
N

NickPick

Two things here: first you need a top level window to display something.
  That's JFrame, JDialog, JWindow ... one other I think, but mostly
you'll use JFrame.

   NewJPanel nj = new NewJPanel();
   JFrame jf = new JFrame();
   jf.add( nj );

Next, you need to call setVisible to show the window.

   jf.setVisible( true );

As an aside, these actions must be done on the EDT or you risk
catastrophe.  See here:

<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html>

Many thanks

In fact I manage to create a JFrame when I do it manually but the
problem is when I create a JFrame with NetBeans the class contains
automatically a new public static void main(String args[]) although in
my opinion it should simply be a class that I can instantiate from my
own main class. Why does it automatically create another public static
void main?

thanks
 
L

Lew

NickPick said:
the problem is when I create a JFrame with NetBeans the class contains
automatically a new public static void main(String args[]) although in
my opinion it should simply be a class that I can instantiate from my
own main class. Why does it automatically create another public static
void main?

When you create a new artifact with NetBeans, it gives you a choice of what
kind of artifact to create. One choice is "Java Main Class", another is
simply "Java Class". The latter does not include a 'main()' method by
default. You can edit the templates for these artifacts to your taste. My
"Java Main Class" template includes a log4j 'Logger' instance.
 
M

Mark Space

NickPick said:
own main class. Why does it automatically create another public static
void main?

I don't know why, it just does. Just ignore it. Or as Lew says you can
edit the template for JFrame Forms and remove it. I leave it in because
it's handy for testing. I can pick "Run File" from the menu and
NetBeans will run that one JFrame independently of the rest of the
application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NetBeans NewPanel question 4
netbeans 3
Problems with JPanel 4
Display jpg in JPanel problem 5
Adding Graphics to Jpanel 2
JPanel inside JPanel inside JFrame 1
NetBeans development 4
Painting in JPanel 2

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top