本帖最后由 tinalucy67 于 2009-11-17 17:36:36 编辑

解决方案 »

  1.   

    http://topic.csdn.net/u/20091104/19/f77ee27a-ff7f-417c-8a73-f8ba1cf7a2e5.html
    看看这个。
      

  2.   

    我的意思是在一个单元格里既有图片又有文字,文字在图片的后面,而且文字是从一个javabean得到的。
      

  3.   

    俺自己发的贴自己回复。
    import java.awt.BorderLayout;
    import java.awt.Component;import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;public class SampleTable extends JPanel
    {
    JTable table; public SampleTable() 
    {
    setLayout(new BorderLayout());
    table = new JTable(new SampleTableModel());
    table.setCellSelectionEnabled(true); table.getColumn("Button").setCellRenderer(new MyTableCellRenderer()); JScrollPane pane = new JScrollPane(table);
    add(pane, BorderLayout.CENTER);
    } public static void main(String[] args) 
    {
    JFrame f = new JFrame("Sample   Table");
    SampleTable sample = new SampleTable();
    f.getContentPane().add(sample, BorderLayout.CENTER);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 200);
    f.setVisible(true);
    }
    }class MyTableCellRenderer extends JButton implements TableCellRenderer
    {
    public MyTableCellRenderer()
    {
    super();
    } public String SetImgpath(int row)
    {
    if(row % 2 == 0 )
    {
    return "D:\\pause.png";
    }
    else
    {
    return"D:\\play.png";
    }
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) 
    {


    // this.setText((value == null) ? "" : ((JButton) value).getText());
    //
    // if (isSelected)
    // {
    // int choose = JOptionPane.showConfirmDialog(null, "第" + row + "行"
    // + "第" + column + "列" + "   JButton   Clicked", "Test",
    // JOptionPane.OK_CANCEL_OPTION);
    // if (choose == JOptionPane.OK_OPTION) 
    // {
    // super.setForeground(Color.red);
    // super.setBackground(table.getSelectionBackground());
    // } 
    // else 
    // {
    // super.setForeground(Color.blue);
    // super.setBackground(table.getSelectionBackground());
    // }
    // }// JLabel jlbl = new JLabel("String text", new ImageIcon("D:\\pause.png"), 0);


    JLabel jlbl = new JLabel("Name " + String.valueOf(row) , new ImageIcon(this.SetImgpath(row)), 0);
    return jlbl;
    }
    }class SampleTableModel extends AbstractTableModel 
    {
    Object data[][] = { 
    { "Jon", "Apple", null },
    }; Object names[] = { "NAME", "FURUTS", "Button" }; public int getRowCount()
    {
    return data.length;
    } public int getColumnCount() 
    {
    return names.length;
    } public Object getValueAt(int row, int col)
    {
    return data[row][col];
    } public String getColumnName(int colIndex) 
    {
    return names[colIndex].toString();
    } public Class getColumnClass(int c) 
    {
    return getValueAt(0, c).getClass();
    } public boolean isCellEditable(int row, int col)
    {
    return getColumnClass(col) == String.class;
    } public void setValueAt(Object aValue, int row, int col) 
    {
    data[row][col] = aValue;
    }
    }
      

  4.   

    import   java.awt.*;  
      import   javax.swing.*;  
      import   javax.swing.event.*;  
      import   javax.swing.table.*;  
       
      public   class   SampleTable   extends   JPanel{  
                      JTable   table;  
         
                      public   SampleTable(){  
                                      setLayout(new   BorderLayout());  
                                      table   =   new   JTable(new   SampleTableModel());  
                                      table.setCellSelectionEnabled(true);  
         
                                      table.getColumn("Button").setCellRenderer(new   MyTableCellRenderer());  
         
                                      JScrollPane   pane   =   new   JScrollPane(table);  
                                      add(pane,   BorderLayout.CENTER);  
                      }  
         
                      public   static   void   main(String[]   args){  
                                      JFrame   f=   new   JFrame("Sample   Table");  
                                      SampleTable   sample   =   new   SampleTable();  
                                      f.getContentPane().add(sample,   BorderLayout.CENTER);  
                                      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                                      f.setSize(400,   200);  
                                      f.setVisible(true);  
                      }  
      }  
         
      class   MyTableCellRenderer   extends   JButton   implements   TableCellRenderer{  
         
              public   MyTableCellRenderer()   {  
                                      super();  
              }  
         
              public   Component   getTableCellRendererComponent(JTable   table,   Object   value,  
                                                          boolean   isSelected,   boolean   hasFocus,   int   row,   int   column)   {  
         
                                      this.setText((value   ==   null)?   "":   ((JButton)value).getText());  
         
                              if(isSelected){  
         
                                      int   choose   =     JOptionPane.showConfirmDialog(null,"第"   +row   +"行"   +"第"   +   column+"列"   +"   JButton   Clicked","Test",JOptionPane.OK_CANCEL_OPTION);  
                                          if(choose   ==   JOptionPane.OK_OPTION){  
                                              super.setForeground(Color.red);  
                                              super.setBackground(table.getSelectionBackground());  
                                          }else{  
                                              super.setForeground(Color.blue);  
                                              super.setBackground(table.getSelectionBackground());  
                                          }  
                                            super.setForeground(table.getSelectionForeground());  
                                            super.setBackground(table.getSelectionBackground());  
                            }else{  
                                            super.setForeground(table.getForeground());  
                                            super.setBackground(table.getBackground());  
                                      }  
         
                                      if(hasFocus){  
                                            super.setForeground(table.getSelectionForeground());  
                                            super.setBackground(table.getSelectionBackground());  
                                      }else{  
                                            super.setForeground(table.getForeground());  
                                            super.setBackground(table.getBackground());  
                                      }  
         
                                      return   this;  
              }  
      }  
         
         
         
      class   SampleTableModel   extends   AbstractTableModel{  
         
                      Object   data[][]   =   {  
                                      {   "Jon","Apple",   new   JButton("Apple")},  
                                      {   "Marry","Pine",   new   JButton("Pine")},  
                                      {   "Ben","Peach",   new   JButton("Peach")},  
                                      {   "Mike","Orange",   new   JButton("Orange")},  
                                      {   "Patty","Apple",   new   JButton("Apple")},  
                                      {   "Jimmy","Lemon",   new   JButton("Lemon")},  
                                      {   "Jon","Apple",   new   JButton("Apple")},  
                                      {   "Marry","Pine",   new   JButton("Pine")},  
                                      {   "Ben","Peach",   new   JButton("Peach")},  
                                      {   "Mike","Orange",   new   JButton("Orange")},  
                                      {   "Patty","Apple",   new   JButton("Apple")},  
                                      {   "Jimmy","Lemon",   new   JButton("Lemon")},  
                      };  
         
              Object   names[]   =   {"NAME",   "FURUTS",   "Button"};  
         
                      public   int   getRowCount(){  
                                      return   data.length;  
                      }  
         
                      public   int   getColumnCount(){  
                                      return   names.length;  
                      }  
         
                      public   Object   getValueAt(int   row,   int   col){  
                                      return   data[row][col];  
                      }  
         
                      public   String   getColumnName(int   colIndex){  
                                      return   names[colIndex].toString();  
                      }  
         
                      public   Class   getColumnClass(int   c){  
                                      return   getValueAt(0,   c).getClass();  
                      }  
         
                      public   boolean   isCellEditable(int   row,   int   col){  
                                      return   getColumnClass(col)   ==   String.class;  
                      }  
         
                      public   void   setValueAt(Object   aValue,   int   row,   int   col){  
                                      data[row][col]   =   aValue;  
                      }  
      }