Results 1 to 13 of 13
  1. #1
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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

  2. #2
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Main class and GUI class

    How can I access methods in my main class from my GUI class?
    The same way you access methods in any class. You need a reference to the class with the methods:
    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.

  4. #4
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Main class and GUI class

    access the methods in Main by Main.method()
    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.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Main class and GUI class

    Quote Originally Posted by Norm View Post
    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.
    Good to know--thanks!

  7. #7
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Main class and GUI class

    How do I pass this to dgui?
    The main() method does not have a "this". Its a static method and does not have an instance of the class it is in.

    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.

  9. #9
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Main class and GUI class

    main() makes an instance of dGUI, and then makes it showable.
    Yes, that is all the main() method does. What more are you expecting to happen?
    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.

  11. #11
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default 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

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default 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.

  13. #13
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Main class and GUI class

    Quote Originally Posted by Norm View Post
    Have you solved the problem now?
    Yes--thanks.

Similar Threads

  1. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  2. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  3. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •