2017-04-12 95 views
-3

所以我有這個代碼,因爲我需要添加照片到我的JTable,所以我需要做一個抽象的表模型。然而,看到這段代碼後,我真的不知道它做了什麼,但我真的很想理解它。如果你可以請給我解釋一下,那真是太棒了! (我也不知道Icon.class在代碼中做了什麼)。代碼如下:需要解釋什麼代碼

public class TheModel extends AbstractTableModel { 

    private String[] columns; 
    private Object[][] rows; 

    public TheModel(){} 

    public TheModel(Object[][] data, String[] columnName){ 

     this.rows = data; 
     this.columns = columnName; 
    } 


    public Class getColumnClass(int column){ 
// 4 is the index of the column image 
     if(column == 4){ 
      return Icon.class; 
     } 
     else{ 
      return getValueAt(0,column).getClass(); 
     } 
    } 


    public int getRowCount() { 
    return this.rows.length; 
    } 

    public int getColumnCount() { 
    return this.columns.length; 
    } 


    public Object getValueAt(int rowIndex, int columnIndex) { 

    return this.rows[rowIndex][columnIndex]; 
    } 
    public String getColumnName(int col){ 
     return this.columns[col]; 
    } 


} 

每個解釋都非常感謝!我知道這個網站主要是爲了幫助編碼問題而不是解釋,但我真的希望你們能夠例外。

+4

*「我真的希望你們可以例外「* ...不。 –

+0

@AndrewThompson該死的。 –

+0

我建議你在問題導向的基礎上提問。您擁有的標題並未真正顯示您遇到問題,但會發送一個錯誤想法標題的消息。 – Pablo

回答

1

所以我需要做一個抽象的表模型。

不,你不知道。沒有理由創建新的表格模型。

您只能擴展DefaultTableModel並覆蓋getColumnClass(...)方法。

(我也有不知道Icon.class做代碼

,會告訴什麼樣的數據類型存儲在該列的表,這樣的表可以使用適當的渲染器的內顯示的數據。這就是你想看到的圖片而非文字。

閱讀從How to Use Tables了Swing教程中的一節有關表如何工作的基礎知識的更多信息。

+0

非常感謝您花時間來解釋! :) –