@Override在SwingWorker方法中不起作用

时间:2014-04-02 15:44:10

标签: java swing jtable jfilechooser swingworker

经过几次尝试用@Override找到问题与类工作者扩展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

2 个答案:

答案 0 :(得分:1)

不要@Override这堂课。仅对方法使用@Override。

答案 1 :(得分:1)

@Override的目标是METHOD。 第二个问题是你不能覆盖超类(SwingWorker)的所有抽象方法。验证doInBackground是否具有与超类中相同的签名。