简体   繁体   中英

Java JFrame and another class interaction

I've found the similar question , but it's still be unclear for me.

So, I have a main class ProcessorCalculations(), from which I call MainFrame() class. In MainFrame class user should choose the folder. How I can transmit the JFileChooser() object from MainFrame() to ProcessorCalculations()?

I've tried to implement the hint from the link above:

   ProcessorCalculation processor = new ProcessorCalculation();
   MainFrame mainFrame = new MainFrame(processor);

But I don't know how to call processor methods from mainFrame without creating new objects. Even I dont't know the correct question I should ask Google. Help please.

If you're using the code written above, then you're passing the current processor instance into your MainFrame constructor. What are you doing with the reference from within this constructor? Are you settinga a ProcessorCalculation instance to this reference? Please show us your constructor.

Your MainFrame class should look something like...

public class MainFrame extends JFrame {
   // your ProcessorCalculation field  
   private ProcessorCalculation processor;

   public MainFrame(ProcessorCalculation processor) {
      // set the field with ref passed in parameter
      this.processor = processor; 

      // of course other code goes here
   }

   public void someMainFrameMethod() {
      // use the reference
      processor.someProcessorMethod();
   }

}

Create an attribute say for example files in the mainframe by which the contents of JFileChooser() are referenced ( you may say contents are stored in this attribute ). If this attribute is private put getter setter methods in the Mainframe for this attribute ( to make it accessible from other classes) now coming back to your ProcessorCalculation class when you write mainFrame.getFiles() ( you have already created object mainFrame object there) it returns the data you wanted in this class.

In case you still face a problem please ask for a coded solution I will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM