2013-12-17 44 views

回答

3

您需要覆盖getColumnClass()方法,以便表格可以选择合适的渲染器。简单的例子:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.table.*; 

public class TableIcon extends JFrame 
{ 
    public TableIcon() 
    { 
     ImageIcon aboutIcon = new ImageIcon("about16.gif"); 
     ImageIcon addIcon = new ImageIcon("add16.gif"); 
     ImageIcon copyIcon = new ImageIcon("copy16.gif"); 

     String[] columnNames = {"Picture", "Description"}; 
     Object[][] data = 
     { 
      {aboutIcon, "About"}, 
      {addIcon, "Add"}, 
      {copyIcon, "Copy"}, 
     }; 

     DefaultTableModel model = new DefaultTableModel(data, columnNames); 
     JTable table = new JTable(model) 
     { 
      // Returning the Class of each column will allow different 
      // renderers to be used based on Class 
      public Class getColumnClass(int column) 
      { 
       return (column == 0) ? Icon.class : Object.class; 
      } 
     }; 
     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 
    } 

    public static void main(String[] args) 
    { 
     TableIcon frame = new TableIcon(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

} 
+0

Mr.camickr在我的页面上面看,数组的列和行没有名字,所以DefaultTableModel的参数是什么? –

+0

定制我的示例以满足您的要求。只有你知道确切的要求。 – camickr

相关问题