Posted on 2008-03-02 00:16 
kooyee 阅读(1065) 
评论(0)  编辑  收藏  所属分类: 
Swing/Applet 
			 
			
		 
		使用到自定义的
CellRenderer和 CellEditor. 它们以作为inner class加入到table所在的class中
定义一个cell的Jbutton渲染对象

 class ButtonRenderer extends JButton implements TableCellRenderer
class ButtonRenderer extends JButton implements TableCellRenderer  {
{


 public ButtonRenderer()
          public ButtonRenderer()  {
{
 setOpaque(true);
            setOpaque(true);
 }
          }

 public Component getTableCellRendererComponent(JTable table, Object value,
          public Component getTableCellRendererComponent(JTable table, Object value,

 boolean isSelected, boolean hasFocus, int row, int column)
              boolean isSelected, boolean hasFocus, int row, int column)  {
{

 if (isSelected)
            if (isSelected)  {
{
 setForeground(table.getSelectionForeground());
              setForeground(table.getSelectionForeground());
 setBackground(table.getSelectionBackground());
              setBackground(table.getSelectionBackground());

 } else
            } else  {
{
 setForeground(table.getForeground());
              setForeground(table.getForeground());
 setBackground(UIManager.getColor("Button.background"));
              setBackground(UIManager.getColor("Button.background"));
 }
            }
 setText((value == null) ? "" : value.toString());
            setText((value == null) ? "" : value.toString());
 return this;
            return this;
 }
          }
 }
        }
定义button cell editor

 class ButtonEditor extends DefaultCellEditor
class ButtonEditor extends DefaultCellEditor  {
{
 protected JButton button;
          protected JButton button;

 private String label;
          private String label;

 private boolean isPushed;
          private boolean isPushed;
 
          
 private String selectId;
          private String selectId;


 public ButtonEditor(JCheckBox checkBox)
          public ButtonEditor(JCheckBox checkBox)  {
{
 super(checkBox);
            super(checkBox);
 button = new JButton();
            button = new JButton();
 button.setOpaque(true);
            button.setOpaque(true);

 button.addActionListener(new ActionListener()
            button.addActionListener(new ActionListener()  {
{

 public void actionPerformed(ActionEvent e)
              public void actionPerformed(ActionEvent e)  {
{
 fireEditingStopped();
                fireEditingStopped();
 }
              }
 });
            });
 }
          }

 public Component getTableCellEditorComponent(JTable table, Object value,
          public Component getTableCellEditorComponent(JTable table, Object value,

 boolean isSelected, int row, int column)
              boolean isSelected, int row, int column)  {
{

 if (isSelected)
            if (isSelected)  {
{
 button.setForeground(table.getSelectionForeground());
              button.setForeground(table.getSelectionForeground());
 button.setBackground(table.getSelectionBackground());
              button.setBackground(table.getSelectionBackground());

 } else
            } else  {
{
 button.setForeground(table.getForeground());
              button.setForeground(table.getForeground());
 button.setBackground(table.getBackground());
              button.setBackground(table.getBackground());
 }
            }
 
           
 label = (value == null) ? "" : value.toString();
            label = (value == null) ? "" : value.toString(); 
 button.setText(label);
            button.setText(label);
 //get the value of the first cell in this selected row
//get the value of the first cell in this selected row
 selectId = table.getValueAt(row, 0).toString();
            selectId = table.getValueAt(row, 0).toString();
 isPushed = true;
            isPushed = true;
 return button;
            return button;
 }
          }


 //这里是点击button执行的操作
         //这里是点击button执行的操作 
                public Object getCellEditorValue()  {
{

 if (isPushed)
            if (isPushed)  {
{
 
              
 JOptionPane.showMessageDialog(null, "The first of this row is"+selectId, "", JOptionPane.ERROR_MESSAGE);
                                JOptionPane.showMessageDialog(null, "The first of this row is"+selectId, "", JOptionPane.ERROR_MESSAGE);
 }
            }
 isPushed = false;
            isPushed = false;
 return new String(label);
            return new String(label);
 }
          }


 public boolean stopCellEditing()
          public boolean stopCellEditing()  {
{
 isPushed = false;
            isPushed = false;
 return super.stopCellEditing();
            return super.stopCellEditing();
 }
          }


 protected void fireEditingStopped()
          protected void fireEditingStopped()  {
{
 super.fireEditingStopped();
            super.fireEditingStopped();
 }
          }
 }
        }
最后在table中加入他们, 假设添加到table中名为"button"的列
 table.getColumn("Button").setCellRenderer(new ButtonRenderer());
table.getColumn("Button").setCellRenderer(new ButtonRenderer());
 
            
    table.getColumn("Button").setCellEditor( new ButtonEditor(new JCheckBox()));