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:
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();
}
}
}