如何使带有过滤器的JFileChooser工作?另外,我的.getText()和.getSelectedIndex出现错误

时间:2018-10-30 03:56:03

标签: java swing compiler-errors jfilechooser

 private void readContents() {

     // These are the variables for the creation of the file chooser
    int selection;
    FileReader fileReader;
    FileNameExtensionFilter filter;

      // Main folder, as per rubric
    this.fileChooser = new JFileChooser(".");
/**
* I was not sure how to implement a filter for file chooser
* So, I looked it up, and obtained idea from
* FileFilter for JFileChooser. (n.d.). Retrieved October 29, 2018, 
* from https://stackoverflow.com/questions/20411919/filefilter-for- 
* jfilechooser
*
* Another website used for help was
* Java Code Examples for javax.swing.filechooser.FileNameExtensionFilter. 
* (n.d.). Retrieved October 29, 2018, 
* from https://www.programcreek.com/java-api- 
* examples/javax.swing.filechooser.FileNameExtensionFilter
*
* Below is the JFileChooser Code which enables a user to select a file
* With the added research, I was able to figure out that the filter is used 
* to specify what kind
* of files you want the file chooser to use. You can add more and different 
* file types if needed. 
*/
    filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
    this.fileChooser.setFileFilter(filter); 
     selection = this.fileChooser.showOpenDialog(new JFrame());  
     if (selection == JFileChooser.APPROVE_OPTION) {
        try {
            fileReader = new FileReader(this.fileChooser.getSelectedFile()); 
            this.scannerContents = new Scanner(fileReader);
        } catch (FileNotFoundException ex) {               // remove catch
            this.displayErrorPopup("Error: No such file found. Please try again.");
        }
    }

//以下是我需要帮助的另一段代码。 // .getText()和.getSelectedIndex()出错

private void searchWorld() {

// Variables used for searching the "world"
    String resultingInformation, searchInformation;
    int item;
     // Getting text from JTextField1
    resultingInformation = "";
    searchInformation = this.searchjTextField1.getText();
// This code returns the first item in the list that is a match for any 
    //given item
    item = this.searchJComboBox.getSelectedIndex();
     // Code for if user adds no information in search field
    if (searchInformation.equals("")) {
 // Displays message to users for no information entered into search field  
        this.displayErrorPopup("Message: No informaiton in search field");
        return;
    }

下面是我在调试时遇到的错误。

Compiling 1 source file to C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\build\classes
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\src\SeaPortProgram.java:220: error: cannot find symbol
        this.fileChooser.setFileFilter(filter);
  symbol:   method setFileFilter(FileNameExtensionFilter)
  location: variable fileChooser of type Object
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\src\SeaPortProgram.java:221: error: cannot find symbol
         selection = this.fileChooser.showOpenDialog(new JFrame());
  symbol:   method showOpenDialog(JFrame)
  location: variable fileChooser of type Object
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\src\SeaPortProgram.java:224: error: cannot find symbol
                fileReader = new FileReader(this.fileChooser.getSelectedFile());
  symbol:   method getSelectedFile()
  location: variable fileChooser of type Object
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\src\SeaPortProgram.java:253: error: cannot find symbol
        searchInformation = this.searchjTextField1.getText();
  symbol:   method getText()
  location: variable searchjTextField1 of type Object
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\src\SeaPortProgram.java:255: error: cannot find symbol
        item = this.searchJComboBox.getSelectedIndex();
  symbol:   method getSelectedIndex()
  location: variable searchJComboBox of type Object
5 errors
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\nbproject\build-impl.xml:954: The following error occurred while executing this line:
C:\Users\Stephen Hamilton\Documents\NetBeansProjects\SeaPortProgram\nbproject\build-impl.xml:271: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

0 个答案:

没有答案