Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


Advertisement
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
How to set the cursor to wait?
How to set the cursor to wait?  How to set the cursor to wait
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
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
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
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
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 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
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
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 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
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
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
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
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
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
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
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
file upload using spring - Spring
file upload using spring  How to upload a file in server using spring framework
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
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
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
 

 

 

DMCA.com