2014-04-25 22 views
-1

你好我有從jtable中取值並將其添加到ArrayList的方法,我將使用該方法將文本緩衝寫入srt文件,但現在我現在的儲蓄ArrayList的我想從jtable中獲取所有值並將其添加到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被推薦給create方法getValueAt(),但這個又是什麼內容方法 一些側面信息表被聲明爲全球和該方法是在

更多的代碼相同的類從類jtable.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

什麼問題?可能與增加2個地方的'i'有關? – csmckelvey

+0

不,他是說創建方法getVlaueAt() – user3571995

+0

getValueAt()'? – BitNinja

回答

0

更改它放在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

+0

你能讀懂edite嗎 – user3571995

+0

現在工作嗎? – Braj

+0

我將它更改爲cModel.getValueAt它沒有錯誤,但我沒有嘗試 – user3571995

相關問題