This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
There are no new questions, but there may be new answers.
You will find more about the single-threading rule if you look at the bottom of this page.
Joined: May 23, 2012
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.
Joined: Oct 13, 2005
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.
Then why are you mucking around with the visual designer?
Would you recommend that anyone learn driving in a F1 race car?
Joined: Oct 13, 2005
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.