[Table] Swing Table 中使用 button

使用到自定义的 CellRenderer和 CellEditor. 它们以作为inner class加入到table所在的class中

定义一个cell的Jbutton渲染对象

class  ButtonRenderer  extends  JButton  implements  TableCellRenderer  {

          
public ButtonRenderer() {
            setOpaque(
true);
          }


          
public Component getTableCellRendererComponent(JTable table, Object value,
              
boolean isSelected, boolean hasFocus, int row, int column) {
            
if (isSelected) {
              setForeground(table.getSelectionForeground());
              setBackground(table.getSelectionBackground());
            }
 else {
              setForeground(table.getForeground());
              setBackground(UIManager.getColor(
"Button.background"));
            }

            setText((value 
== null? "" : value.toString());
            
return this;
          }

        }



定义button cell editor

class  ButtonEditor  extends  DefaultCellEditor  {
          
protected JButton button;

          
private String label;

          
private boolean isPushed;
          
          
private String selectId;

          
public ButtonEditor(JCheckBox checkBox) {
            
super(checkBox);
            button 
= new JButton();
            button.setOpaque(
true);
            button.addActionListener(
new ActionListener() {
              
public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
              }

            }
);
          }


          
public Component getTableCellEditorComponent(JTable table, Object value,
              
boolean isSelected, int row, int column) {
            
if (isSelected) {
              button.setForeground(table.getSelectionForeground());
              button.setBackground(table.getSelectionBackground());
            }
 else {
              button.setForeground(table.getForeground());
              button.setBackground(table.getBackground());
            }

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


         //这里是点击button执行的操作 
                public Object getCellEditorValue() {
            
if (isPushed) {
              
                                JOptionPane.showMessageDialog(
null"The first of this row is"+selectId, "", JOptionPane.ERROR_MESSAGE);
            }

            isPushed 
= false;
            
return new String(label);
          }


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


          
protected void fireEditingStopped() {
            
super.fireEditingStopped();
          }

        }


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

你可能感兴趣的:([Table] Swing Table 中使用 button)