JTable 加入JRadioButton

  1. public class RadioButtonColumn extends AbstractCellEditor
  2.         implements TableCellRenderer, TableCellEditor, ActionListener
  3.     {
  4.         JTable table;
  5.         JRadioButton renderButton;
  6.         JRadioButton editButton;
  7.         String text;
  8.         public RadioButtonColumn(JTable table, int column)
  9.         {
  10.             super();
  11.             this.table = table;
  12.             renderButton = new JRadioButton();
  13.             editButton = new JRadioButton();
  14.             editButton.setFocusPainted( false );
  15.             editButton.addActionListener( this );
  16.             TableColumnModel columnModel = table.getColumnModel();
  17.             columnModel.getColumn(column).setCellRenderer( this );
  18.             columnModel.getColumn(column).setCellEditor( this );
  19.         }
  20.         public Component getTableCellRendererComponent(
  21.             JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  22.         {
  23.             if (hasFocus)
  24.             {
  25.                 //renderButton.setForeground(table.getForeground());
  26.                 renderButton.setForeground(Color.BLUE);
  27.                 renderButton.setBackground(table.getBackground());
  28.             }
  29.             else if (isSelected)
  30.             {
  31.                 if(renderButton.isSelected())
  32.                 {
  33.                     renderButton.setSelected(false);
  34.                 }
  35.                 else
  36.                 {
  37.                     renderButton.setSelected(true);
  38.                 }
  39.                 renderButton.setForeground(table.getSelectionForeground());
  40.                  renderButton.setBackground(table.getSelectionBackground());
  41.             }
  42.             else
  43.             {
  44.                 renderButton.setForeground(table.getForeground());
  45.                 renderButton.setBackground(table.getBackground());
  46.             }
  47.             renderButton.setText( (value == null) ? this.text : value.toString() );
  48.             return renderButton;
  49.         }
  50.         public Component getTableCellEditorComponent(
  51.             JTable table, Object value, boolean isSelected, int row, int column)
  52.         {
  53.             text = (value == null) ? this.text : value.toString();
  54.             editButton.setText( text );
  55.             return editButton;
  56.         }
  57.         public Object getCellEditorValue()
  58.         {
  59.             return text;
  60.         }
  61.         public void actionPerformed(ActionEvent e)
  62.         {
  63.             fireEditingStopped();
  64.             System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
  65.         }
  66.     }

你可能感兴趣的:(JTable 加入JRadioButton)