在JTable中加Button

  import   java.awt.*;  
  import   javax.swing.*;  
  import   javax.swing.event.*;  
  import   javax.swing.table.*;  
   
  public   class   Test   extends   JPanel{  
                  JTable   table;  
     
                  public   Test(){  
                                  setLayout(new   BorderLayout());  
                                  table   =   new   JTable(new   TestModel());  
                                  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");  
                                  Test   sample   =   new   Test();  
                                  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   TestModel   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;  
                  }  
  } 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值