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