Home Answers Viewqa Java-Beginners wait until a file is opened using JFileChooser

 
 


vidya
wait until a file is opened using JFileChooser
2 Answer(s)      a year and 11 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

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 Pages:
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
Wait Cursor - Java Tutorials
(if you're using JDK 1.4+). Happy waiting until next time :-) Herman...Wait Cursor 2003-03-06 The Java Specialists' Newsletter [Issue 065] - Wait, Cursor, Wait! Author: Herman Lintvelt (Polymorph Systems) If you
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
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
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
JFileChooser
Java: JFileChooser The javax.swing.JFileChooser class is used to create file choosers for selecting files or directories to open or save. To Create an Open File Chooser The sample code below uses JFileChooser in the Open button
how to focus on the new window opened from JSF ?
how to focus on the new window opened from JSF ?  Hi guys, I have commandLink with action which using javascript to open a new window(window.open()). Problem is, the new window opens at background behind the current window
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
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
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
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
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
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
Images - ImageIcon
formats are .gif, .jpg, and .png. Choice: Wait until loaded or overlap loading with other execution Wait until loaded (recommended where timing... until the ImageIcon is completely loaded. Warning: Just putting the file name
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
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
Java file dialog
of JFileChooser, there is one another way to select a file from the file system... until the user has chosen a file. You can see in the given code, we have... data into the selected file using write() method. Here is the code: import
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
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 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
java chat using java media
ActionListener, ControllerListener { JButton fileButton; JFileChooser file = new JFileChooser(); JLabel urlLabel; JTextField urlField; String mediaFile...java chat using java media   Remove the error of this code plz reply
How to run program in new opened cmd
How to run program in new opened cmd  Hello i have made a program in java to open command prompt, then how to run my left of the program in the new opened command prompt
File
File   How to find all files in our local computer using java
Scrollbar click in safari browser not Collapsing opened dropdowns
Scrollbar click in safari browser not Collapsing opened dropdowns  When im clicking on the scrollbar in safari browser the click not collapsing the opened dropdowns....i mean if iam scrolling the scrollbars after content is being
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
uploading a text file into a database
uploading a text file into a database  how to upload a text file into a database using jchooser   import java.io.*; import java.sql.... { static File file; public static void main(String[] args) throws Exception
File IO
are planning to learn file management using Java IO package.This class is available... are packaged into java.io. package. File Management with the help...; Constructing a File Name path Read the File
Java file scanner
Java file scanner In this section, you will learn how to read a file using... input until it reaches the end of the file. The nextLine() method returns a string on a separate line  until it reaches the end of the file. Here
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 = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:////////////////////////////////////" + file
Java Thread Wait,Notify
Wait and notify both are the methods of the Object class. Wait makes the object to wait for the given time or till it's notified. Wait method allows... in the synchronized context(methdod,block) Java Thread Wait & Notify Example public
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
JAVA Method Wait
JAVA Method Wait       The Wait method in Java hold the thread to release the lock till the thread hold the object. The most important point is to be remember that wait method
need code for file browser
need code for file browser  hi, i need to write a code to browse the file in my computer using Jsp or java.. what is the code for file browser... the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file">
Struts2.2.1 execAndWait Interceptor example.
will disscuss about the Execute and wait (execAndWait)  Interceptor using struts2.2.1.  The Execute and Wait Interceptor is used for the long running... or progress bar. The Execute and Wait Interceptor is very usefull to prevent the HTTP
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening... and save this amount until he changes it again. Thank You
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening... and save this amount until he changes it again. Thank You
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening... and save this amount until he changes it again. Thank You
File transfer to teh server - Java Beginners
File transfer to teh server  hi,, I've been trying to make an application where the user select several fiels using JFileChooser and then the program read all the files and then save the content of all in 1 file
How to copy a file
How to copy a file In this section, you will learn how to copy a file using... creates two File instances to specify source and destination file respectively...; reads the byte value from the stream until there is a data in the stream from
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 transfer from client to server - Java Beginners
File transfer from client to server  hi,, I've been trying to make an application where the user select a file using JFileChooser and then the program directly send it to the server using client/server sockets, I've tried
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

Ask Questions?

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.