java - 如何使用 Filter 使我的 JFileChooser 工作?另外,我的 .getText() 和 .getSelectedIndex 有错误
问题描述
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)
解决方案
推荐阅读
- python - Mypy:设置更新期间的不兼容类型错误
- c++ - 没有得到以下代码的任何输出。任何人都可以看看这个
- python - 将语音训练为文本时不知道参数
- typescript - Vue3 Typescript VSCode Intellisense Props 扩展 Attrs
- docker - 从Jenkins主机的内部图像运行文件
- java - 如何有效地测试 JPA 存储库?
- database - Postgresql - 在同一会话中查找不同用户的重叠时间范围并将它们作为对呈现
- c# - 如何在选择 DataGridCell 时更改模板项的背景
- angular - 如何使用 (selectionChange) 方法更新基于另一个垫选择字段的输入字段
- jinja2 - Jinja2 未定义错误 jinja2.exceptions.UndefinedError:列表对象没有元素 0