Reputation: 1465
I have two classes in the same Package. The package is called main. Main contains two classes a Main class and a Display class. The display class was created to display a GUI with text boxes and buttons. I gave the buttons a listener that fires when the user clicks one of the button. In the main class is a vector of object that i am storing and need to display one a time in the display class' text boxes.
My Question is: can the mouse clicked action call a method in the main class that gathers the needed info an passes that back to a method in the display class to modify those text boxes> Do i need to combine my two classes into one? How would i do within the classes?
From testing i have made the main class extend the display class. I am able to start the display class from here but when i try to call a method in display from main it does nothing. If i try to call a main method from the display class it also seems to do nothing.
Upvotes: 0
Views: 491
Reputation: 691645
If you want your mechanic to fix your car (by starting it, diagnosing the problem, opening the hood, etc.), you give your car to the mechanic, don't you?
It's the same in Java. If you want the Display object (the mechanic) to access to information available in the Main object (the car), you need to give the Main object to the Display:
Main main = new Main(); // main contains data
Display display = new Display(main) // Display is constructed, and is given the main object
in Display:
public void someButtonClicked() {
String someInformation = this.main.getSomeInformation();
this.someTextField.setText(someInformation);
}
Upvotes: 2