1 33 package net.sf.jga.swing.spreadsheet; 34 35 import java.awt.Component ; 36 import java.awt.KeyboardFocusManager ; 37 import javax.swing.AbstractCellEditor ; 38 import javax.swing.JComponent ; 39 import javax.swing.JLabel ; 40 import javax.swing.JTable ; 41 import javax.swing.UIManager ; 42 import javax.swing.table.TableCellEditor ; 43 44 50 51 public class ComponentEditor extends AbstractCellEditor implements TableCellEditor { 52 53 static final long serialVersionUID = -2906356624578134466L; 54 55 static private ComponentEditor _instance; 57 58 61 static public synchronized ComponentEditor getInstance() { 62 if (_instance == null) 63 _instance = new ComponentEditor(); 64 65 return _instance; 66 } 67 68 public ComponentEditor (){ 69 } 70 71 private Object _value; 76 77 private JLabel _label = new JLabel (); 79 80 84 89 public Object getCellEditorValue() { 90 return _value; 91 } 92 93 97 102 public Component getTableCellEditorComponent(JTable table, Object object, boolean isSelected, 103 int row, int col) 104 { 105 _value = object; 106 107 if(object instanceof JComponent ) { 108 return (JComponent ) _value; 109 } 110 111 115 _label.setForeground(table.getSelectionForeground()); 116 _label.setBackground(table.getSelectionBackground()); 117 _label.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); 118 _label.setFont(table.getFont()); 119 _label.setText(object.toString()); 120 _label.setOpaque(true); 121 return _label; 122 } 123 124 128 public boolean stopCellEditing() { 129 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); 130 return super.stopCellEditing(); 131 } 132 } 133 | Popular Tags |