954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to? Java GUI interacting with another Java class

Hi guys,

I'm writing a Java programme, one with GUI for a project in college.

We separated the work into a few guys doing a few standalone Java classes. These standalone Java classes are run by doing the usual:
java -jar XXX.jar through the console. The functions of the class are issued through by typing commands into the class by way of the console. e.g. /chat mynick message

A few of the other guys designed the GUI.

The question now is.. how do we integrate these two together? I've been googling but I dont know a good sign post.

I'm thinking we need to write an interface between the GUI and the Java classes. Is there a term for this?

We're lost, please advise!

heysebas
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Java GUI is just another class. You can create instances of other classes inside and call their methods.
Small, silly example:

class Person {
public String name;
public int age;   

public Person() {
   }
}
class Print {
private Person person;

public Print(Person p) {
   person = p;
}

 public void print() {
   System.out.println(person.name+";"+person.age);
 }
}


And in another class (or in main)

Person pers = new Person();
pers.name = "Name";
pers.age = 10;

Print pr = new Print(pers);
pr.print();
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Add methods to the stand-alone classes so their functionality can be called from Java, eg public sendChat(String nick, String message).
Then, in the GUI have entry fields for the required data, when the user clicks OK, get the data from the fields and use it to call the functionality via these new methods.
ps It's called MVC. Model (ie the functionality) View (ie the user interface) and Controller (the logic that links the two). In this case, like many others, the controller logic is trivial so it gets combined with the view code.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You