代码如下
package com.nfs;
import java.awt.*; 
import java.util.Vector;import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.table.*; public class TestSimpleJTable { 
private  static DefaultTableModel   tableModel1   =   new   DefaultTableModel()   { 
        public   boolean   isCellEditable(int   row,   int   col)   { 
            if   (col   ==   1)   { 
                return   true; 
            } 
            else   { 
                return   false; 
            } 
        }//使得第二列的单元格子可编辑,其他列的单元格子不可编辑 //        public   Class   getColumnClass(int   col)   { 
//            Vector   v=(Vector)dataVector.elementAt(0); 
//            return   v.element(col).getClass(); 
//        }         public   Class   getColumnClass(int   c)   { 
            return   getValueAt(0,   c).getClass(); 
        }//可以使单元格子实现Image,CheckBox等 
    };       private   JTable   jTable1   =   new   JTable(tableModel1);   


     public static void main(String[] args) { 
         String[][] data = {{"A", "B"}, {"C", "D"}}; 
         String[] colName = {"First", "Last"};          JFrame frame = new JFrame(); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          final JTextField field = new JTextField(); 
         JTable table = new JTable(data,colName){//(tableModel1){ 
         
             /**
 * long
 */
private static final long serialVersionUID = 1L; // JTable的RowSelectionChanged事件在这里。 
             public void valueChanged(ListSelectionEvent e) { 
                 super.valueChanged(e);                  if (!this.isVisible()) { 
                     return; 
                 }                  ListSelectionModel sm = 
                     (ListSelectionModel)e.getSource(); 
                 int row = sm.getLeadSelectionIndex();                  int colCount = this.getColumnCount(); 
                 String[] sa = new String[colCount];                  for (int col=0; col<colCount; col++) { 
                     sa[col] = (String)this.getValueAt(row, col); 
                 }                  String s = ""; 
                 for (int i=0; i<sa.length; i++) { 
                     s += sa[i] + "\t"; 
                 } 
                 System.out.println("--->>>"+s);
                 field.setText(s); 
             } 
         };          JTable table1 = new JTable(tableModel1);
         tableModel1.addRow(new   Object[]   {new   ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "), new   Boolean(false),   "John ", "male ", "23 "}); 
         tableModel1.addRow(new Object[]{"111","222","233","444","555"});
         
         Vector<String> columnNames = new Vector();
  // String[] m_colNames = { "子服务ID", "主服务ID",
  // "子服务名称","开始路径","日志路径","备注","是否启用","是否可视","创建时间"};
  columnNames.addElement("子服务I");// {"日期","类别","分类","多少钱","卡/现金","说明"};
  columnNames.addElement("主服务ID");
  columnNames.addElement("子服务名称");
  columnNames.addElement("开始路径");
  columnNames.addElement("日志路径");
  columnNames.addElement("备注");
  columnNames.addElement("是否启用");
  columnNames.addElement("是否可视");
  columnNames.addElement("创建时间");
  // dummyMacData.addElement(new Data("105", "1",
  // "子服务2","","","","","","2012-09-08"));
  Vector<Vector> rowData = new Vector<Vector>();
  Vector<String> oneRow = new Vector<String>();
  oneRow.addElement("105");
  oneRow.addElement("1");
  oneRow.addElement("子服务1");
  oneRow.addElement("");
  oneRow.addElement("");
  oneRow.addElement("");
  oneRow.addElement("");
  oneRow.addElement("");
  oneRow.addElement("2012-09-09");  rowData.addElement(oneRow);
  Vector<String> oneRow1 = new Vector<String>();
  // oneRow.clear();
  oneRow1.addElement("100");
  oneRow1.addElement("1");
  oneRow1.addElement("子服务2");
  oneRow1.addElement("");
  oneRow1.addElement("");
  oneRow1.addElement("");
  oneRow1.addElement("");
  oneRow1.addElement("");
  oneRow1.addElement("2012-04-09");
  rowData.addElement(oneRow1);
  Vector oneRow2 = new Vector();
  oneRow2.addElement(new   ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
  oneRow2.addElement(new   ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
  oneRow2.addElement("");
  oneRow2.addElement("");
  oneRow2.addElement("");
  oneRow2.addElement("");
  oneRow2.addElement("");
  oneRow2.addElement("");
  oneRow2.addElement("");
  rowData.addElement(oneRow2);
         
         
         
         tableModel1.setDataVector(rowData, columnNames);
         table1.setPreferredSize( 
                 new Dimension(200, 200));          table.setPreferredSize( 
             new Dimension(200, 200));          Container container = frame.getContentPane(); 
         container.add(table, BorderLayout.CENTER);
         container.add(table1, BorderLayout.EAST);
         container.add(field, BorderLayout.PAGE_END); 
 
         frame.pack(); 
         frame.setVisible(true); 
     } 
}
 

解决方案 »

  1.   

    JTable中负责显示的是TableCellRenderer.而JTable默认是DefaultTableCellRenderer来渲染,其实就是个JLable。你可以看一下DefaultTableCellRenderer的API, 
    public Component getTableCellRendererComponent(JTable table, Object value,
                              boolean isSelected, boolean hasFocus, int row, int column)
    这个方法决定了显示的效果。
    而默认只是把value toString()了。如果你想显示为图片,则需要重写此方法:import java.awt.Component;import javax.swing.Icon;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;public class SupportIconTableCellRenderer extends DefaultTableCellRenderer { @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
    row, column); if (value instanceof Icon) {
    this.setIcon((Icon) value);
    // remove lable's text
    this.setText("");
    } return this;
    }}
    简单实现了下,自己可以根据需要扩展。