1 16 17 package org.mc4j.console.swing.editor; 18 19 import javax.swing.*; 20 import java.awt.event.ActionListener ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.*; 23 24 28 29 public class ButtonEditor extends DefaultCellEditor { 30 protected JButton button; 31 private String label; 32 private boolean isPushed; 33 34 public ButtonEditor(JCheckBox checkBox) { 35 super(checkBox); 36 button = new JButton(); 37 button.setOpaque(true); 38 button.addActionListener(new ActionListener () { 39 public void actionPerformed(ActionEvent e) { 40 fireEditingStopped(); 41 } 42 }); 43 } 44 45 46 47 public Component getTableCellEditorComponent(JTable table, Object value, 48 boolean isSelected, int row, int column) { 49 if (isSelected) { 50 button.setForeground(table.getSelectionForeground()); 51 button.setBackground(table.getSelectionBackground()); 52 } else { 53 button.setForeground(table.getForeground()); 54 button.setBackground(table.getBackground()); 55 } 56 label = (value == null) ? "" : value.toString(); 57 button.setText(label); 58 isPushed = true; 59 return button; 60 } 61 62 public Object getCellEditorValue() { 63 if (isPushed) { 64 JOptionPane.showMessageDialog(button, label + ": Ouch!"); 67 } 69 isPushed = false; 70 return new String (label); 71 } 72 73 public boolean stopCellEditing() { 74 isPushed = false; 75 return super.stopCellEditing(); 76 } 77 78 protected void fireEditingStopped() { 79 super.fireEditingStopped(); 80 } 81 } | Popular Tags |