-1

I have this jFrame class:

    public class Frame1 extends javax.swing.JFrame {

    ........

    String name;
    File file;
    JFileChooser FileChooser = new JFileChooser();

    if (FileChooser.getSelectedFile().isFile()) {
    try {    
           file = FileChooser.getSelectedFile();
           name = FileChooser.getSelectedFile().getName();   
           System.out.println( name ); 

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ........

    private void Button1 (java.awt.event.ActionEvent evt) {                                            

                java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Frame2 obj = new Frame2 ();
                }
            });    
        }                                           
}

Then I created the class "Frame2":

public class Frame2 extends javax.swing.JFrame {

.......

}

As you can image, when my program starts I use a JFileChooser to choose a file; after that I click a button that opens another jFrame; in this jFrame (Frame2)

What I would need is to use the file that I have chosen in the previous jFrame (Frame1). So I need to use "file" variable from "Frame1" in "Frame2".

I tried to do this in Frame2:

Frame1 obj1 = new Frame1();
File file2 = obj1.file;
System.out.println( file2  ); 

So when I run the program and choose a file and then I click "Button1" to run "Frame2", it first prints the file name ("name") from "Frame1" and after that it prints "null" so I can't get correct "file" value from "Frame1" and use it in "Frame2".

How can I do that? Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frank
  • 2,083
  • 8
  • 34
  • 52

1 Answers1

5

This won't work:

Frame1 obj1 = new Frame1();
File file2 = obj1.file;
System.out.println( file2  ); 

because it falls into a common newbie trap: thinking that a new instance of a class (here Frame1) holds the same information as another previously used instance of the class (the previous Frame1 instance that was displayed), and this simply isn't true unless you use static variables -- something I strongly urge you not to do.

Instead why not:

  1. Change your first JFrame into a modal JDialog or JOptionPane
  2. Give it a public method getSelectedFile() that returns the selected file. This is what your question is really about -- sharing the state of one object with another object -- and one way to do this is via your basic getter and setter methods.
  3. Then show your dialog, and when it returns, call the above method on the above object.
  4. Or why not instead simply show a JFileChooser dialog since it does all of this for you?

For example:

import java.io.File;
import javax.swing.*;

public class Foo {

   private static void createAndShowGui() {
      JFileChooser fileChooser = new JFileChooser();
      int result = fileChooser.showOpenDialog(null);
      if (result == JFileChooser.APPROVE_OPTION) {
         File file = fileChooser.getSelectedFile();
         JTextField field = new JTextField(file.getAbsolutePath(), 30);
         JPanel panel = new JPanel();
         panel.add(new JLabel("Selected File:"));
         panel.add(field);

         // create and open a new JFrame with the selected file's path
         JFrame frame = new JFrame("Foo");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(panel);
         frame.pack();
         frame.setLocationByPlatform(true);
         frame.setVisible(true);
      }

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • thanks, but the problem is that I need to access file variable from another class (Frame2) in another .java file and I can't avoid that. Maybe a getSelectedFile() method would be better, but how can I use it to return the changed during running value of a public variable? Is there any example? – Frank Jul 12 '13 at 07:18