user1060187
user1060187

Reputation: 977

JFileChooser class

When I try and use this code, I get the following error:

Error: Could not find or load main class jfilechooserexample.JFileChooserExample.

Any help would be appreciated

This is the code I put in the class.

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);

  }
}

Upvotes: 0

Views: 380

Answers (1)

Udo Held
Udo Held

Reputation: 12538

Your class declaration doesn't match the error message. The error message lists a package name jfilechooserexample. However, your class doesn't belong to your package.

You missed to post the start parameters. I guess they contain the package name.

Upvotes: 1

Related Questions