其实响应鼠标事件更方便吧.
至于如果你想要Button的外观, 可以重写Renderer

解决方案 »

  1.   

    本人写的代码你可以参照一下:
    import java.sql.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class ResultsTable implements ActionListener{
    public ResultsTable(){
    JFrame f=new JFrame();
    MyTable mt=new MyTable();
    JTable table=new JTable(mt);
    JButton jb=new JButton("ddd");table.getColumnModel().getColumn(1).setCellEditor(new JButtonCellEditor(jb));
    jb.addActionListener(this);JScrollPane scrollPane=new JScrollPane(table);
    f.getContentPane().add(scrollPane);
    f.setSize(800,600);
    f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
    System.out.println("ddd");}
    public static void main(String args[]){
    ResultsTable rst=new ResultsTable();
    }
    }
    -----------
    import java.lang.Boolean;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.EventObject;
    import javax.swing.tree.*;
    import java.io.Serializable;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
     * The default editor for table and tree cells.
     * <p>
     * <strong>Warning:</strong>
     * Serialized objects of this class will not be compatible with 
     * future Swing releases.  The current serialization support is appropriate
     * for short term storage or RMI between applications running the same
     * version of Swing.  A future release of Swing will provide support for
     * long term persistence.
     *
     * @version 1.38 02/02/00
     * @author Alan Chung
     * @author Philip Milne
     */public class JButtonCellEditor extends AbstractCellEditor 
        implements TableCellEditor, TreeCellEditor { //
    //  Instance Variables
    //    protected JComponent editorComponent;
        protected EditorDelegate delegate;
        protected int clickCountToStart = 1;//
    //  Constructors
    //    /**
         * Constructs a DefaultCellEditor that uses a JButton
         *
         * @param x  a JButton object ...
         */
        public JButtonCellEditor(final JButton button) {
            editorComponent = button;
    this.clickCountToStart = 2;
            delegate = new EditorDelegate() {
                public void setValue(Object value) {
    button.setText((value != null) ? value.toString() : "");
                }     public Object getCellEditorValue() {
    return button.getText();
        }
            };
    button.addActionListener(delegate);
        }    /**
         * Specifies the number of clicks needed to start editing.
         *
         * @param count  an int specifying the number of clicks needed to start editing
         * @see #getClickCountToStart
         */
        public void setClickCountToStart(int count) {
    clickCountToStart = count;
        }    /**
         *  ClickCountToStart controls the number of clicks required to start
         *  editing.
         */
        public int getClickCountToStart() {
    return clickCountToStart;
        }//
    //  Override the implementations of the superclass, forwarding all methods 
    //  from the CellEditor interface to our delegate. 
    //    public Object getCellEditorValue() {
            return delegate.getCellEditorValue();
        }    public boolean isCellEditable(EventObject anEvent) { 
    return delegate.isCellEditable(anEvent); 
        }
        
        public boolean shouldSelectCell(EventObject anEvent) { 
    return delegate.shouldSelectCell(anEvent); 
        }    public boolean stopCellEditing() {
    return delegate.stopCellEditing();
        }    public void cancelCellEditing() {
    delegate.cancelCellEditing();
        }//
    //  Implementing the TreeCellEditor Interface
    //    public Component getTreeCellEditorComponent(JTree tree, Object value,
    boolean isSelected,
    boolean expanded,
    boolean leaf, int row) {
    String         stringValue = tree.convertValueToText(value, isSelected,
        expanded, leaf, row, false); delegate.setValue(stringValue);
    return editorComponent;
        }//
    //  Implementing the CellEditor Interface
    //    public Component getTableCellEditorComponent(JTable table, Object value,
     boolean isSelected,
     int row, int column) {
            delegate.setValue(value);
    return editorComponent;
        }
    //
    //  Protected EditorDelegate class
    //    protected class EditorDelegate implements ActionListener, ItemListener, Serializable {        protected Object value;        public Object getCellEditorValue() {
                return value;
            }     public void setValue(Object value) { 
        this.value = value; 
    }        public boolean isCellEditable(EventObject anEvent) {
        if (anEvent instanceof MouseEvent) { 
    return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
        }
        return true;
    }
        
            public boolean shouldSelectCell(EventObject anEvent) { 
                return true; 
            }        public boolean startCellEditing(EventObject anEvent) {
        return true;
    }        public boolean stopCellEditing() { 
        fireEditingStopped(); 
        return true;
    }       public void cancelCellEditing() { 
       fireEditingCanceled(); 
           }        public void actionPerformed(ActionEvent e) {
                JButtonCellEditor.this.stopCellEditing();
    }        public void itemStateChanged(ItemEvent e) {
        JButtonCellEditor.this.stopCellEditing();
    }
    }
    }
    -------------------------------------------------
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class MyTable extends AbstractTableModel{
    Object[][] pls={{"","",""},{"","",""}};
    String[] names={"name","address","hell"};
    public int getColumnCount(){
    return names.length;}
    public int getRowCount(){
    return pls.length;}
    public String getColumnName(int col){
    return names[col];}
    public Object getValueAt(int row,int col){
    return pls[row][col];}
    public Class getColumnClass(int c){
    return getValueAt(0,c).getClass();}public boolean isCellEditable(int row,int col){
    return true;}
    }
    你可以参照一下,最后我把按钮加了进去