
Inside OpenFileButtonClicked class's actionPerformed method,
a. I create object of a differnt class that essentially uses JFileChooser to open a file. b. call a funtion that takes in the filename as argument.
Now my questions is the statement 4.b gets executed before 4.a completes its execution and always the filename is 'null'. How do I wait until 4.a get completed before 4.b gets executed? I am very new to java, so please bear my ignorance if this question is trivial. Also I have stripped down lot of code and requirements to make the question simple.
Thanks

Try this:
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JFileChooserExample{
public static void getFileName(File f){
System.out.println("File is: "+f.getName());
}
public static void main(String[] args) {
JPanel panel=new JPanel();
panel.setLayout(null);
JButton b=new JButton("Open File");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser();
int ret = chooser.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
getFileName(file);
}
}
});
b.setBounds(10,10,120,20);
panel.add(b);
JFrame f=new JFrame();
f.add(panel);
f.setSize(400,200);
f.setVisible(true);
}
}

Hi, Thank you, this works fine without any problem. Could you please suggest any good website for java tutorials and also some article/books for good design practices.
Thank you.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.