*
The moose likes Swing / AWT / SWT and the fly likes Main Method problem in netbeans7.1 J Frame form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Main Method problem in netbeans7.1 J Frame form" Watch "Main Method problem in netbeans7.1 J Frame form" New topic
Author

Main Method problem in netbeans7.1 J Frame form

sinead connolly
Greenhorn

Joined: May 23, 2012
Posts: 2
Hi!
I am having an issue with my java form. It won't run because it doesn't have a main method. I know that in the main method I have need has some hidden components. I was wondering if there is some magic button you can press that will insert the main method, or should I start all over again. It's not my first form and I know that most of the time I don't create the main method its automatically there for me. If there is no other choice that to start all over again could you please point me in the right direction so I won't run into this problem again.

This is not the first time I've had this problem, but there must be an easier way to solve this problem than deleting the page and starting again.

Any information would be greatly appreciated.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hello sinead and welcome to the Ranch!

Your question reveals that you're fairly new to Java. Maybe nobody has pointed out to you that the NetBeans visual designer, regardless of what Oracle would have you believe, is categorically not a beginners' tool. Learning to code a Java GUI is a few orders of magnitude easier that learning the quirks of a code generator. You can get back to the visual designer after you feel confident that you know most of what you need to know about the Swing API and Java coding in general.

For learning to create a Swing application, you can go through the Tutorial Trail: Creating a GUI With JFC/Swing.

As for your problem as stated, nothing prevents you from writing a public static void main(String[] args) method in your existing class, or from creating a separate launcher class whose only role is to launch the GUI from its main(...) method. Don't ignore Swing's single threaded rule: make sure to construct and address all Swing components only on the EDT.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Welcome again
You will find more about the single-threading rule if you look at the bottom of this page.
sinead connolly
Greenhorn

Joined: May 23, 2012
Posts: 2
Hi!

Thanks for the information.

I will go through that in more detail when I have more time. I did just end up deleting it and starting again. It works fine the second time I tried it, the main method was created for me.

I guess on reflection, a better question would be why did NetBeans create it the second time and not the first time?
Is it as simple as - the j frame form was the only class (or whatever the technical term is) in that package.

I know I'm only a beginner(and I don't have all the technical jargon ), I also know that I can't ignore all the swing components but I had - import javax.swing.*; to compensate for that since I don't know them in greater detail.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Don’t you have to tell NetBeans to use a particular class to contain the main method? It sometimes creates a class called Main for that purpose.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

sinead connolly wrote:I know I'm only a beginner

Then why are you mucking around with the visual designer?

Would you recommend that anyone learn driving in a F1 race car?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
You can probably use NB as a text editor, writing the code by hand, and add a main method by hand. Or get that code half‑and‑half handwritten and machine generated.
That would give you the advantages of an IDE (speed, error checking, etc) without the incomprehensible code which comes from a GUI builder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main Method problem in netbeans7.1 J Frame form
 
Similar Threads
Java EE timer service and periodical jobs
i need massive help
prepoputlate form
First program by myself. Need help?
regarding synchronization