简体   繁体   中英

@Override are not working in SwingWorker method

After several attempts to find the issue with @Override with class Worker extends SwingWorker class i would like some one to help me to find answer my old code was:

this code are responsible about reading file and show the information inside JTable, but that is very slow so i need to use swing worker, so i update the code (look at the next code)

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;

now in order to use SwingWorker i applied this changes

  @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);
}
}

and this code is my action method

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();


        }
    };

error i am getting

    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

Don't @Override the class. Only use @Override for methods.

The target of @Override is METHOD . The second problem is that you don't override all the abstract methods of the superclass ( SwingWorker ). Verify that doInBackground has the signature same as in the superclass.

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