1

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.

vidit
  • 6,293
  • 3
  • 32
  • 50
Festivejelly
  • 670
  • 2
  • 12
  • 30

1 Answers1

2

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.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35
  • I'll give that a shot as soon as I get home. Im not sure how that works though? Would that go into the MainForm code? – Festivejelly Jun 06 '13 at 18:29
  • That is correct. Your constructor for 'ConfigEditor' would then look something like this: 'ConfigEditor(MainFrame mainFrame)' You can then call 'mainFrame.initMyComponents()' – Michael Freake Jun 06 '13 at 19:14
  • Fantastic Michael thanks very much for your suggestion you're helping me understand it a bit more now. Sorry I cant upvote your answer I dont have enough rep. – Festivejelly Jun 06 '13 at 19:17
  • No problem. This is what stackoverflow is all about! Good luck. – Michael Freake Jun 06 '13 at 19:46