Results 1 to 13 of 13
Thread: Main class and GUI class
- 05-27-2012, 07:52 PM #1
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Main class and GUI class
I'm using NetBeans. My question is about how the main and GUI classes are supposed to communicate.
I got my program to do what I want it to through the command line, but now I'm trying to use a GUI. My main class has the main method and the code for my program. From here, I don't understand how things are supposed to work.
I think the main method is supposed to create an instance of the GUI and then run the GUIs init() on it. I think I can figure out the logic behind the action events. When buttons and such in the GUI are pressed, I think the action event code should run methods in main. How do I call the methods in main from the GUI? I wouldn't think that an instance of main would not need to be created since it's what made the instance of the GUI.
I found some tutorials, but they didn't exactly answer my question. It's fine to simply point me to the right documentation.
Thanks!
D
- 05-27-2012, 09:14 PM #2
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Re: Main class and GUI class
I think I can simplify my question: How can I access methods in my main class from my GUI class?
- 05-27-2012, 09:28 PM #3
Re: Main class and GUI class
The same way you access methods in any class. You need a reference to the class with the methods:How can I access methods in my main class from my GUI class?
refToClass.aMethodInThatClass();
If the main class creates the GUI class, have it pass a reference to itself ("this") in the constructor of the GUI class which should save it in a class variable so any methods in the GUI class can use it.If you don't understand my response, don't ignore it, ask a question.
- 05-27-2012, 10:36 PM #4
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Re: Main class and GUI class
I figured it out. With classes Main and GUI, GUI can access the methods in Main by Main.method() because there is already an instance of Main when the program runs.
- 05-27-2012, 10:44 PM #5
Re: Main class and GUI class
That is only true for static methods in the Main class. To call other methods will require a reference to an instance of the Main class.access the methods in Main by Main.method()If you don't understand my response, don't ignore it, ask a question.
- 05-28-2012, 01:05 AM #6
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
- 05-28-2012, 02:03 AM #7
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Re: Main class and GUI class
I think I finished my code, but I'm having a problem getting NetBeans to bring up the GUI. It builds with no problems. Here's my main method:
public static void main(String[] args) throws InterruptedException {
dGUI dgui = new dGUI();
dgui.setVisible(true);
}
Any clues? How do I pass this to dgui?
- 05-28-2012, 02:36 AM #8
Re: Main class and GUI class
The main() method does not have a "this". Its a static method and does not have an instance of the class it is in.How do I pass this to dgui?
What class do you want the dGUI class to have a reference to? There is only one class shown in the posted code.If you don't understand my response, don't ignore it, ask a question.
- 05-28-2012, 02:51 AM #9
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Re: Main class and GUI class
I have two classes: Main and dGUI. Main has the main() method. dGUI runs its own methods and static methods in Main. dGUI has an init() method.
As far as I understand, main() makes an instance of dGUI, and then makes it showable.
I'm probably missing something obvious.
- 05-28-2012, 03:05 AM #10
Re: Main class and GUI class
Yes, that is all the main() method does. What more are you expecting to happen?main() makes an instance of dGUI, and then makes it showable.
No instance of the Main class is created.
Why is the main() method in the Main class and not in the dGUI class since all it does is create an instance of dGUI and set it visible?If you don't understand my response, don't ignore it, ask a question.
- 05-28-2012, 11:28 PM #11
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Re: Main class and GUI class
The problem is that the gui does not show up when setVisable(true) is called. I figured out the problem. When you tell NetBeans to design a gui, it extends Applet. However, it has to rather extend JFrame. You can simply copy the components in the applet and paste them on a jframe. I learned it here:
Java - setVisible(true) has no effect on GUI - Stack Overflow
- 05-29-2012, 12:22 AM #12
Re: Main class and GUI class
Have you solved the problem now?
If you don't understand my response, don't ignore it, ask a question.
- 05-29-2012, 12:31 AM #13
Programming Again
- Join Date
- May 2012
- Location
- Eugene, OR
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
Can't Get variable calculated in another class to function in main class
By hkp30 in forum New To JavaReplies: 0Last Post: 10-23-2011, 10:49 PM -
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 07:54 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks