我试图从jtable获取所有值并将其添加到ArrayList

时间:2014-04-25 23:11:12

标签: java swing arraylist jtable

你好我有从jtable获取值并将其添加到ArrayList的方法,我将使用该方法缓冲区将文本写入srt文件,但现在我将信息保存在arrayList中

public static ArrayList<String> getTableSTimeData(){

ArrayList<String> data = new ArrayList<String>();

    for (int i=0; i < 5; i++){      
      data.addAll(getValueAt(i, 2));
      i++;
    }

return data;

}

错误信息是:

     error: cannot find symbol
          data.addAll(getValueAt(i, 2));
  symbol:   method getValueAt(int,int)
  location: class GuiInterface
1 error

netBeans是建议创建方法getValueAt()但该方法的内容是什么 一些辅助信息表被声明为全局,并且此方法与

在同一个类中

来自班级的更多代码

public class GuiInterface extends JFrame {
String[] columnNames = {"#", "Start", "End", "Translation column"};
public final JTable table;
DefaultTableModel cModel ;
public GuiInterface(String title){
cModel =new DefaultTableModel(columnNames, 0);
table = new JTable(cModel);
    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);

}//end of the constructor

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


     @Override
      protected DefaultTableModel doInBackground() {
     model = new DefaultTableModel(columnNames, 0);
    ArrayList<String> ends = ReadingFile.getFileEndingTime(srtPath);
    ArrayList<String> starts = ReadingFile.getFileStartingTime(srtPath);
    ArrayList<String> subs = ReadingFile.readSubtitles(srtPath);
    ArrayList<String> lins = ReadingFile.ArraylineLengths(srtPath);
    for (int i = 0; i < ReadingFile.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);
}
}
/*
public Object getValueAt(int row,int column){


    return null;

}
  */  
public static ArrayList<String> getTableSTimeData(){

    ArrayList<String> data = new ArrayList<String>();


        //for (int i=0; i < 5; i++){      
          Object val = table.getValueAt(2, 2);
            data.add((String)val);
        //}

    return data;
} 

1 个答案:

答案 0 :(得分:0)

将其更改为jtable.getValueAt(),其中jtableJTable的实例。

示例代码:

    public static ArrayList<String> getTableSTimeData(){

        ArrayList<String> data = new ArrayList<String>();

            for (int i=0; i < jtable.getRowCount(); i++){      
               Object val = jtable.getValueAt(i, 2); // value of ith row and 3rd column
               data.add((String)val);
            }

        return data;
    }

请查看JTable#getValueAt()JTable#getRowCount()


请注意,根据您的代码,您将获取每行第3列的所有值。

你为什么要在循环中增加i两次?