Festivejelly
Festivejelly

Reputation: 690

Calling A JFrame method from Another JFrame

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.

Upvotes: 1

Views: 5375

Answers (1)

Michael Freake
Michael Freake

Reputation: 1207

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.

Upvotes: 2

Related Questions