wait until a file is opened using JFileChooser

wait until a file is opened using JFileChooser

  1. I created a java class "MyMainClass" and created a UI panel inside this class.
  2. The panel contains a JButton "Open"
  3. I added an action listner for this button to an object of class called "OpenFileButtonClicked"
  4. 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

View Answers

June 28, 2011 at 3:25 PM

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);

    }
}

June 30, 2011 at 3:33 PM

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.









Related Tutorials/Questions & Answers:
wait until a file is opened using JFileChooser
wait until a file is opened using JFileChooser   I created a java... JFileChooser to open a file. b. call a funtion that takes in the filename as argument... its execution and always the filename is 'null'. How do I wait until 4.a get
How to open a file in java using JFileChooser
How to open a file in java using JFileChooser  How to open a file in java using JFileChooser
Advertisements
Using a JFileChooser - Java Beginners
Using a JFileChooser  Dear Editor, How to select an image from JFileChooser & add the image in mySQL5 database? Thanks in advanced. Regards... void main(String[] args) { JFileChooser chooser = new JFileChooser(); int
JFileChooser - Java Beginners
to select a text file (using the JFileChooser). The program will then count the number of words in the selected file and display the result containing the name of the file, its size (in bytes) and the number of word it contains. (Hint: You
JFileChooser - Swing AWT
JFileChooser  Hi,iam using jfilechooser to select a directory....now... ActionListener { JButton button; JFileChooser chooser; String... JFileChooser(); chooser.setCurrentDirectory(new java.io.File
generate columns in file using tab delimeter - Swing AWT
generate columns in file using tab delimeter   Hi, I have problem ........i have imported all the files in a directory using JFIleChooser... be opened in note pad.So i have read the file as normal text file,so can any
Uploading a file using UploadBean
Uploading a file using UploadBean  Dear sir, In my project i have to upload the file and use the same file for getting a values from... upload the file and use the file for getting values it will throws
file upload using JSP
file upload using JSP  I have created a form to upload a file in a html page, now i want to get the path of the file in a jsp page so what code...="java" %> <HTML> <HEAD><TITLE>Display file upload form
file uploading using jsp
file uploading using jsp  below file uploading code has one error... = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead
How to set the cursor to wait?
How to set the cursor to wait?  How to set the cursor to wait
need to open a file that is in server using jsp
need to open a file that is in server using jsp   im doing a web... open link it should be opened in jsp.downlaoding part was done by me. wt i need is just want to open a pdf in jsp.is it possible to open sever file from jsp.Plz
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
file upload using spring - Spring
file upload using spring  How to upload a file in server using spring framework
How to zip a file using javascrip?
How to zip a file using javascrip?  hi.. can anyone tell me How to zip a file using javascrip
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
scp using file transfer - Security
scp using file transfer  scp(secure copy protocol) using to download a file from linux server to windows xp . using scp, username, password authentication
How to write to file using FileWriter
How to write to file using FileWriter  hi, How to write to file using FileWriter thanks,   Hi, To writing in a file in Java program we... of the FileWriter class can be created using the following of its constructor i.e. FileWriter
How to write to file using FileOutputStream
How to write to file using FileOutputStream  Hi friends, Please help me in java program. How to write to file using FileOutputStream? thanks,   Hi, To write a file using FileOutputStream, we have to use
Excel file wriring using java
Excel file wriring using java  i have one predefined bill format in excel i want to append values in it using java apche POI or Jexcel i tried it but it overwrites file and lost previous contents what can i do
excel file using JDBC java.?
excel file using JDBC java.?  hey, i am a telecomm. engineer , and i am try to develop a tool which reads from a excel file and then appends the same... used JDBC ODBC to read from excel file. i am not able to append it using
Using the escape percent in the batch file
Using the escape percent in the batch file   If you use the one % in the batch file it will consider it as the parameter input and will try to find the variable and replace it. But how to use the percentage in batch file
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
example of sleep and wait method
example of sleep and wait method  write a program to use the sleep and wait method of thread class
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored...;b>Choose the file To Upload:</b></td> <td><INPUT NAME
How to upload file using JSP?
How to upload file using JSP?   Hi all, I m the beginner in JSP, I want to upload file on server in specific folder.   1)page.jsp... file upload form to the user</TITLE></HEAD> <
validation in struts2 using .xml file
validation in struts2 using .xml file  how to do xml validation in struts2 on dynamic fields   Hello Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/struts/struts2/struts2validation
ModuleNotFoundError: No module named 'wait'
ModuleNotFoundError: No module named 'wait'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'wait' How to remove the ModuleNotFoundError: No module named 'wait' error
ModuleNotFoundError: No module named 'wait-for'
ModuleNotFoundError: No module named 'wait-for'  Hi, My Python... 'wait-for' How to remove the ModuleNotFoundError: No module named 'wait... to install padas library. You can install wait-for python with following
ModuleNotFoundError: No module named 'wait-for-it'
ModuleNotFoundError: No module named 'wait-for-it'  Hi, My Python... 'wait-for-it' How to remove the ModuleNotFoundError: No module named 'wait-for-it' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'wait-for-it-to'
ModuleNotFoundError: No module named 'wait-for-it-to'  Hi, My... named 'wait-for-it-to' How to remove the ModuleNotFoundError: No module named 'wait-for-it-to' error? Thanks   Hi, In your python
How to select default FileFilter when creating a JFileChooser dialog?
How to select default FileFilter when creating a JFileChooser dialog? ... default FileFilter when creating a JFileChooser dialog? Thanks   Hi, Following code can be used: JFileChooser c = new JFileChooser
Unable to upload a file to mysql database using struts1
Unable to upload a file to mysql database using struts1  Hi, Below...="multipart/form-data"> Select a File :<html:file property="file"/> <... extends ActionForm{ private FormFile file; public FormFile getFile(){ return
Version of com.n0ize>wait dependency
List of Version of com.n0ize>wait dependency
Until Loop In a Procedure
Until Loop In a Procedure       Until Loop In a Procedure is used to execute a set of statements repeatedly until a certain condition is reached. Understand with ExampleADS
Unzip File Using Java
using java. The java.util.zip.ZipEntry class is used to represent a zip file... interface. Here we passing the zipped file name to be unzipped using command line... Unzipping a File     
Putting a wait notification in Swing
Putting a wait notification in Swing  Hi, I want to created... a frame "Please wait" which comes up whenever a user clicks a Open or Save document Step:- 1)User clicks on Save Document 2)Display "Please wait
Putting a wait notification in Swing
Putting a wait notification in Swing  Hi, I want to created... a frame "Please wait" which comes up whenever a user clicks a Open or Save document Step:- 1)User clicks on Save Document 2)Display "Please wait
Writing a file using servlets - JSP-Servlet
Writing a file using servlets  I'm using a servlet to read an input from a jsp file and write into a .txt file in my computer. The code is good until reading the data and creating a file in my space. but it is not writing
ModuleNotFoundError: No module named 'until-nonidle'
ModuleNotFoundError: No module named 'until-nonidle'  Hi, My... 'until-nonidle' How to remove the ModuleNotFoundError: No module named 'until-nonidle' error? Thanks   Hi, In your python
Thread'd wait() related >>>>>>> - Java Beginners
Thread wait in Java  Hello, Seeking for a thread wait example in java. Also What is thread wait method and when we should call it in the application.Thanks
Why the wait(),notify() should not avaialable in Thread class.
Why the wait(),notify() should not avaialable in Thread class.  wait(),notify(),notifyall() is available in Object class.We are using those method in Threading then why don't they put the 3 methods in thread class
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
How to link an excel file with the application software using java swing
How to link an excel file with the application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
Zip File Using Java
Zipping a File       This example shows how to create a zip file in java. In other words, we will learn how to compress a file just to take less space. We can compress any file
how to create an excel file using java
how to create an excel file using java  how to create an excel file using java
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
How to transfer a file in java using SSH & Telnet
How to transfer a file in java using SSH & Telnet  How to transfer a file in java using SSH & Telnet
Remote file transfer in java using SSH & Telnet
Remote file transfer in java using SSH & Telnet  How to upload & download a file in java using SSH & Telnet

Ads