1 7 package javax.swing.plaf.basic; 8 9 import javax.swing.*; 10 import javax.swing.border.Border ; 11 12 import javax.swing.text.AttributeSet ; 13 import javax.swing.text.BadLocationException ; 14 import javax.swing.text.PlainDocument ; 15 16 import java.awt.*; 17 import java.awt.event.*; 18 19 import java.lang.reflect.Method ; 20 21 28 public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener { 29 protected JTextField editor; 30 private Object oldValue; 31 32 public BasicComboBoxEditor() { 33 editor = new BorderlessTextField("",9); 34 editor.setBorder(null); 35 } 36 37 public Component getEditorComponent() { 38 return editor; 39 } 40 41 46 public void setItem(Object anObject) { 47 if ( anObject != null ) { 48 editor.setText(anObject.toString()); 49 50 oldValue = anObject; 51 } else { 52 editor.setText(""); 53 } 54 } 55 56 public Object getItem() { 57 Object newValue = editor.getText(); 58 59 if (oldValue != null && !(oldValue instanceof String )) { 60 if (newValue.equals(oldValue.toString())) { 63 return oldValue; 64 } else { 65 Class cls = oldValue.getClass(); 67 try { 68 Method method = cls.getMethod("valueOf", new Class []{String .class}); 69 newValue = method.invoke(oldValue, new Object [] { editor.getText()}); 70 } catch (Exception ex) { 71 } 73 } 74 } 75 return newValue; 76 } 77 78 public void selectAll() { 79 editor.selectAll(); 80 editor.requestFocus(); 81 } 82 83 public void focusGained(FocusEvent e) {} 86 87 public void focusLost(FocusEvent e) {} 90 91 public void addActionListener(ActionListener l) { 92 editor.addActionListener(l); 93 } 94 95 public void removeActionListener(ActionListener l) { 96 editor.removeActionListener(l); 97 } 98 99 static class BorderlessTextField extends JTextField { 100 public BorderlessTextField(String value,int n) { 101 super(value,n); 102 } 103 104 public void setText(String s) { 106 if (getText().equals(s)) { 107 return; 108 } 109 super.setText(s); 110 } 111 112 public void setBorder(Border b) {} 113 } 114 115 130 public static class UIResource extends BasicComboBoxEditor  131 implements javax.swing.plaf.UIResource { 132 } 133 } 134 135
| Popular Tags
|