1 package unbbayes.gui.table;
2
3 import java.awt.Component;
4 import java.awt.event.FocusAdapter;
5 import java.awt.event.FocusEvent;
6
7 import javax.swing.DefaultCellEditor;
8 import javax.swing.JTable;
9 import javax.swing.JTextField;
10
11
12
13
14
15
16 public class ReplaceTextCellEditor extends DefaultCellEditor {
17 private static final long serialVersionUID = 1L;
18
19 protected JTextField textField = new JTextField();
20 protected String previousValue = "";
21
22 public ReplaceTextCellEditor() {
23 super(new JTextField());
24 this.textField = (JTextField) this.editorComponent;
25
26 this.textField.addFocusListener(new FocusAdapter() {
27 public void focusGained(FocusEvent e) {
28
29 String text = textField.getText();
30
31 if (previousValue.equals(text)) {
32 textField.selectAll();
33
34
35
36 } else if (text.length() > 0 && previousValue.length() < text.length()) {
37 textField.setText(text.substring(previousValue.length()));
38 }
39 }
40 });
41
42 }
43
44 public void setValue(Object value) {
45 textField.setText((value != null) ? value.toString() : "");
46 }
47
48 public Object getCellEditorValue() {
49 return textField.getText();
50 }
51
52 public Component getTableCellEditorComponent(JTable table, Object value,
53 boolean isSelected, int row, int column) {
54
55 if (isSelected == false) {
56 return null;
57 }
58
59
60 this.previousValue = value.toString();
61
62 this.textField.setText(this.previousValue);
63
64
65
66 return this.textField;
67 }
68
69 }