wait until a file is opened using JFileChooser

Ads
 

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.

Ads









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
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
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
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 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
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
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
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
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
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
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
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.
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
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> <
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
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
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
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
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
how to upload file using FTP in java
how to upload file using FTP in java  hai, i want to upload file using FTP in java Webservices.. Tel me some Articles Regards Asha
how to create pdf file using java and itextjar
how to create pdf file using java and itextjar  How to create pdf file having paragraphs and alignments done using java and itextjar 5.10 version.? hope i get quick response
how to create pdf file using java and itextjar
how to create pdf file using java and itextjar  How to create pdf file having paragraphs and alignments done using java and itextjar 5.10 version.? hope i get quick response
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     

Ads