2015-02-05 68 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