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.
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);
}
Related
I have four coordinates saved in the main class as global Variables. How do I access getters or give parameters to the new Applet? It isn't possible to create an instance because the values of the main class are getting set before the code of the second Applet is run.
When the mouse is pressed and when its released the positions are saved in my main Applet. By releasing the mouse button a second Applet in another Java Class gets opened.
The goal is second Applet being the size of the rectangle the mouse just dragged.
public void mousePressed() {
setStartDetectionAreaX(mouseX);
setStartDetectionAreaY(mouseY);
}
public void mouseReleased(){
setEndDetectionAreaX(mouseX);
setEndDetectionAreaY(mouseY);
PApplet.main("SecondWindow");
}
Okay I found a solution. It it possible to give a String Array as a parameter while calling the other Applet.
Like this:
PApplet.main("SecondWindow", new String[]{pm1, pm2, pm3, pm4};
In the other class its know possible to get the array like this:
args[0]
Another possibility would be setting a static variable and change it in the other class.
I have two classes one that I have in my program. One I have drawn my Graphical user interface and another that has methods to be executed by buttons in GUI class. Basically it's a program to add records to a database. When I click button register it calls the functions class to execute add to database method. In the add to database method I need to add a jOption pane to display successful or not.. How do I ho about it .
I have tried
JOptionPane.showMessageDialog(this,"successful"); the code is not working since it is not in the GUI class
the code is not working since it is not in the GUI class
You can pass the "frame" as a parameter to your "functions" class.
Then the "functions" class can use that value as the "frame" for the JOptionPane.
I have three classes. One is a worker class that does all the hard work but doesn't display anything. The other two are GUI classes one of which calls the other. The one that calls the second GUI class has the worker class open.
The first GUI calls the second with this method:
protected void openAdd() {
AddPet add = new AddPet(ADD_PROMPT, FIELDS, DATE_PROMPT);
add.setVisible(true);
}
The second GUI class is used to get information from the user that is used in the worker class but, as I already have the worker class open in the first GUI I don't want to open it again and I want to use some of the information in the first GUI.
What I need to do is pass that information in the second GUI back to the first GUI so that it can work with it and pass it to the open worker class.
How do I do this?
EDIT:
I think the best option would be to call a method in the first GUI from the second GUI but I don't know if this is even possible.
On second thought, it looks like your second window is being used essentially as a dialog off of the first window, and that you're using it for user data entry and little else. If so, then make sure that the second window is not a JFrame but is rather a modal JDialog. If so, then it will block user interaction with the first window when it's open, and extracting information out of it will be easy, since you're know exactly when the user is done with it, since program flow will resume in the first GUI immediately following your code that sets the second window visibile.
e.g.,
// in this example, AddPet is a modal JDialog, not a JFrame
protected void openAdd() {
// I'm passing *this* into the constructor, so it can be used ...
// ... in the JDialog super constructor
AddPet add = new AddPet(this, ADD_PROMPT, FIELDS, DATE_PROMPT);
add.setVisible(true);
// code starts here immediately after the AddPet modal dialog is no longer visible
// so you can extract information from the class easy:
String petName = add.getPetName(); // I know you don't use these exact methods
String petBreed = add.getPetBreed(); // but they're just a "for instance" type of code
// ... etc
}
....
I'm looking to find a solution too my issue. Currently I have 2 JFrames and 1 utilities class in my netbeans project. I'm no expert on java so please bear with me. I've tried looking through the java docs and on this site but cannot seem to find a solution to my problem.
Here is the scenario:
My launcher class launches the JFrame called MainForm.java the form then initializes components onto the screen. On this form I have a button that launches a new form called ConfigEditor.java. This form is used to edit a config file. I have a Save button on this form, and what I basically want to do is once I click save get the MainForm.java to call a method to fill in the right components with the new values.
Heres an example, heres some of the code from my Save button on the ConfigEditor.java:
if(reply == JOptionPane.YES_OPTION){
try {
Utilities.writeConfigFileBasic(ExecutionLists.getText(),DefaultResultsFile.getText(),
DefaultTestDir.getText(), Environments.getText(), ResultsViewerFile.getText());
ConfigTextArea.append(Utilities.readConfigFile());
JOptionPane.showMessageDialog(rootPane, "Saved");
Now just after the last line I want to call somthing like MainForm.initMyComponents(); as this method exists in the MainForm JFrame but it wont let me call this. The purpose of that method is to populate some of the fields with data extracted from a config file.
I'm sorry if I haven't explained it very well, I'm fairly new to Java if you need any clarification please let me know and I'll do my best to clarify it.
Can you simply pass a reference of MainForm to ConfigEditor when it is constructed? For instance:
... //Code fired by clicking the button you mentioned which is in class MainFrame
ConfigEditor configEditor = new ConfigEditor(this); //This would be a reference to your MainFrame
With this reference you can then call your desired method in the MainFrame class.
So I have two classes.
One is a class called Main. The Main class is supposed to process some data.
Then I have another class called MainApplet, an Applet of course.
How can I display MainApplet from the Main class? This is what I have so far, but the applet does not show:
public class Main {
public static void main(String args[]) {
System.out.println("Starting application.");
MainApplet Main = new MainApplet();
Main.setVisible(true);
Main.show();
} }
How can I display MainApplet from the Main class?
You don't. You display an applet from HTML code.
Are you sure your GUI is in fact an applet? Does it extend JApplet or Applet? If so and you want to show it on the desktop through code, then don't make it an applet but instead display a JFrame. The Java Swing tutorials will show you how to do this: How to use Swing Components
Edit
you state:
Basically I have a Main class that is not an applet. It doesn't extend anything. Then I have another class named MainApplet that is an applet (extends JApplet). I want to run Main first, then display MainApplet after... but I can do it the other way around if needed.
You don't sound like you're running your code from a web page (for some reason you're still keeping this information from us), so the solution is not to use applets for this. Instead create a JFrame. Please check out the tutorials that I've linked to above since an applet is not appropriate for your needs.
You will have a main that creates your GUI, that passes any information into the GUI via constructor or method parameters, and then that tells the GUI to show itself (by calling setVisible(true) if it's a JFrame).
Your mainApplet class must extend Applet. You would then replace your main(...) method with init(...) as this is the method that gets called when you open an Applet. You can test this in most environments such as Eclipse. It order to actually put the Applet on a webpage, you must tell the applet to display using HTML in the page.