2015-02-05 40 views
-1

我需要更改我的jtable外觀。JTable:如何更改jtable形式

我有這些數據:

  1. 圖像的圖像。

  2. title字符串。

  3. date String。
  4. description字符串。
  5. auteur String。

img,title,date,description,auteur。 我的問題是:
是否有可能我可以在每行中顯示這些數據作爲twitter feed的外觀。 我想用樣本在同一個單元格中顯示所有這些數據。

感謝每一個。

+2

您應該創建自己的'TableCellRenderer'。在網上有很多教程... – 2015-02-05 15:57:48

+0

和TableCellRenderer具有行和列的參數,使用這兩個座標來定義getColumnClass – mKorbel 2015-02-05 18:23:42

回答

1

您應該使用自定義單元格渲染器,使用JList(或JTable,但每行只有一個單元格,JList似乎更合適)。

創建數據類

public class MyData { 
    // image, title, date, description and author 
} 

創建單元格渲染

class MyCellRenderer extends JLabel implements ListCellRenderer<MyData> { 

    public Component getListCellRendererComponent(
     JList<?> list,   // the list 
     MyData value,   // value to display 
     int index,    // cell index 
     boolean isSelected,  // is the cell selected 
     boolean cellHasFocus) // does the cell have focus 
    { 
     // tune your component in the way you want, for example 
     this.setText(value.getTitle()); 
     // return the component to draw for this cell 
     return this; 
    } 
} 

當然,你的渲染器可以像伸出另一JPanel組件。
最後,實例化一個JList和設置自定義渲染

JList<MyData> list = new JList<>(); 
list.setCellRenderer(new MyCellRenderer()); 
+0

我創建了類數據: public class Info { \t private int id_info; \t私人String titre; \t私有字符串描述; \t私人字符串留置權; \t private String auteur; \t private String pub_date; \t private String guid; \t private String thumbnail_url; private Image myImage; } 它應該如何用於你example – user3612862 2015-02-05 16:14:42

+0

@ user3612862在我的例子中,你可以用'Info'類替換MyData。有關單元格渲染器的更多信息,請參見[Oracle教程](http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer) – NiziL 2015-02-05 16:23:26