wait until a file is opened using JFileChooser

Ads

 
 

Share on Google+Share on Google+

vidya
wait until a file is opened using JFileChooser
2 Answer(s)      6 years and 4 months ago
Posted in : Java Beginners
  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

Ads
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
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
How to set the cursor to wait?
How to set the cursor to wait?  How to set the cursor to wait
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
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
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
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
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 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 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
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
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
Version of com.n0ize>wait dependency
List of Version of com.n0ize>wait dependency
Version of com.n0ize>wait dependency
List of Version of com.n0ize>wait dependency
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
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
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 Example Many
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
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
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
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