2013-12-17 90 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

相關問題