• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main Method problem in netbeans7.1 J Frame form

 
sinead connolly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
You will find more about the single-threading rule if you look at the bottom of this page.
 
sinead connolly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic