George Avgoustis
George Avgoustis

Reputation: 31

GUI Java Netbeans multiple classes

This is pretty elementary but I do not even know how to search anything for what I need. I have created a system that has a few classes and two projects that communicate with each other via sockets.

Now I am aiming to create a GUI for the system using a designer Netbeans has but I am kind of stuck in limbo when it comes to communicating the GUI with the rest of the classes.

  1. Should the GUI be my main class?
  2. If not, how do I send messages to my GUI class.
  3. When I try to do the following e.g JTextField.setText("PleaseHelp"); in the run method of the GUI I get an error saying : non-static variable JTextField cannot be referenced from a static context.

I know I'm not asking for something specific but I have no clue on how to search for what I need. All I get is tutorials on how to make simple calculator GUIs which is easy to be done because there is only one class, the JFrame.

Upvotes: 3

Views: 1978

Answers (1)

RedGreasel
RedGreasel

Reputation: 481

  1. It would be better if the GUI were a separate class, since a modularized application is more maintainable.
  2. As with all object oriented code, you need a reference to an instance of the GUI class.
  3. JTextField is a class and setText is not static. You need to reference the JTextField that you want to change.

Basically, it should look something like this:

GUI gui = new GUI(/*Parameters*/);
gui.getTextField().setText("PleaseHelp");

Where getTextField is a method of the GUI class (add this to the class created by the Netbeans GUI designer):

public JTextField getTextField(){
  return /*TODO:  Enter text field name here*/;
}

Upvotes: 3

Related Questions