【发布时间】:2014-05-14 00:54:44
【问题描述】:
在多次尝试找到@Override 类 Worker 扩展 SwingWorker 类的问题后,我希望有人帮我找到答案,我的旧代码是:
这段代码负责读取文件并显示JTable中的信息,但是速度很慢所以我需要使用swing worker,所以我更新了代码(看下一段代码)
private DefaultTableModel createModel(String srtPath) {
//int maxLine = ReadFile.maxLine(srtPath); // debug
//Object[][] data = new Object[maxLine][];
//System.out.println(maxLine); // debug
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
//System.out.println(lins.get(i)+" "+starts.get(i)+" "+ ends.get(i)+" "+ subs.get(i));
model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
}
return model;
现在为了使用 SwingWorker,我应用了这些更改
@Override
class Worker extends SwingWorker<DefaultTableModel, Void> {
private final String srtPath;
private final JTable table;
DefaultTableModel model;
public Worker(String srtPath, JTable table) {
this.srtPath = srtPath;
this.table = table;
}
protected DefaultTableModel doInBackground(String srtPath) {
model = new DefaultTableModel(columnNames, 0);
ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
}
return model;
}
@Override
protected void done() {
table.setModel(model);
table.setFillsViewportHeight(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
TableColumn columnA = table.getColumn("#");
columnA.setMinWidth(10);
columnA.setMaxWidth(40);
TableColumn columnB= table.getColumn("Start");
columnB.setMinWidth(80);
columnB.setMaxWidth(90);
TableColumn columnC= table.getColumn("End");
columnC.setMinWidth(80);
columnC.setMaxWidth(90);
}
}
这段代码是我的操作方法
Action openAction = new AbstractAction("Open Subtitle", openIcon) {
@Override
public void actionPerformed(ActionEvent e) {
ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
ourFileSelector.showSaveDialog(null);
ourSrtFile = ourFileSelector.getSelectedFile();
srtPath = ourSrtFile.getAbsolutePath();
Worker p = new Worker(srtPath, table);
p.execute();
}
};
我得到的错误
C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java:241: error: annotation type not applicable to this kind of declaration
@Override
C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java:242: error: GuiInterface.Worker is not abstract and does not override abstract method doInBackground() in SwingWorker
class Worker extends SwingWorker<DefaultTableModel, Void> {
Note: C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
【问题讨论】:
标签: java swing jtable jfilechooser swingworker