简体   繁体   中英

selecting srt file using JFileChooser and read it

i am trying to work in JFileChooser To get File selected from another class i don't care how the button look like but when some one click on it. it should show JFileChooser to select srt file (like text file but another type) and it should read it.

this is my first class

    package AnimeAid;
import java.io.*;
import javax.swing.*;

public class ReadFile {
    private File ourFile= null;
    private static final JFileChooser selectSrtFile = new JFileChooser();
    String filePath = "";

       public ReadFile(){

       }

    public File getSelectFile(){ 
            selectSrtFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
            selectSrtFile.showSaveDialog(null);
            ourFile = selectSrtFile.getSelectedFile();
            filePath = ourFile.getAbsolutePath();
            return ourFile;
    }

        public String readFileInput(){
            try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getSelectFile()), "UTF-8"));
              String line;
                while ((line = reader.readLine()) != null)
                {
                    System.out.println(line);
                }
            }catch(IOException ex){
            return "there is wrong";
            }
            return "file is added";

        }

}

the scanned class

    package AnimeAid;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *
 * @author isslam
 */
public class GuiInterface extends JFrame {

    JTable table;
    JButton is;
    ReadFile t;

    public GuiInterface(String title){
    setSize(900, 700);
    setTitle(title);
    setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
    is = new JButton();
    t = new ReadFile();
    Container cp = getContentPane();
    cp.add(is);

    is.addActionListener(new addButtonWatcher());

    }



    private class addButtonWatcher implements ActionListener{

         @Override
         public void actionPerformed(ActionEvent a){
             Object buttonPressed=a.getSource();
             if(buttonPressed.equals(is))
             {
              t.getSelectFile(); 
             }
}
    }
}

the error message

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: animeaid.GuiInterface
    at animeaid.main.main(main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

It looks like your two classes have different packages:

package animeFactor;
             ^

and

package animefactor;
             ^

I believe Java package names are case sensitive, so despite the fact that you have (probably) put the two files in the same folder, the GuiInterface class cannot use the ReadFile class without importing it.

Either change the package definitions to be the same, or add an import statement to GuiInterface :

import animeFactor.ReadFile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM