Click here to Skip to main content
15,902,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want code brows files of computer then choose file
the result of following code is exeption
Java
package reading;
//import java.io.File;
//import java.awt.Desktop;
import javax.swing.JFileChooser;
import  java.lang.String;
import java.io.IOException;
import javax.swing.filechooser.FileNameExtensionFilter;
 import javax.swing.*;

public class Reading {

    
     
    public static void main(String[] args) {
try {
  JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(  "doc");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }
                }          
catch (Exception e) 
                 { System.out.println("Exeption");}                
    }
Posted

oh lord - where to start?

let's try list it:

- you can not launch a JFileChooser alone.
- You can not have a member "parent" that is not initialized
- You can not set a filter "doc" - the object FileNameExtensionFilter needs at least 2 arguments
- it's really helpful to let the exception just print out the word "Exception". Please let it print the StackTrace. Read that Trace - it's there for a reason.

This all leads me to a question: are you using a decent IDE? Eclipse and Netbeans are free. Use them. They support you in the process of coding. This does not mean that they provide code or anything, but they can see your errors and help you. Simple.

Here is your code in a tuned version:
Java
package reading;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
 
public class Reading {

	public static void main(String[] args) {
		try {
			final JFrame oFrame = new JFrame();
			oFrame.setSize(200,300);
			JButton oButton = new JButton();
			oButton.setText("Click me really hard!");
			oButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser chooser = new JFileChooser();
				    FileNameExtensionFilter filter = new FileNameExtensionFilter("*.doc", "doc");
				    chooser.setFileFilter(filter);
				    int returnVal = chooser.showOpenDialog(oFrame);
				    if(returnVal == JFileChooser.APPROVE_OPTION) {
				       System.out.println("You chose to open this file: " +
				            chooser.getSelectedFile().getName());
				    }
				}
			});
			oFrame.add(oButton);
			oFrame.setVisible(true);
		}          
		catch (Exception e){ 
			e.printStackTrace();
		}
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900