1 18 19 package org.apache.jorphan.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 import java.util.ArrayList ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import javax.swing.JLabel ; 29 import javax.swing.JPanel ; 30 import javax.swing.JTextField ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 34 43 public class JLabeledTextField 44 extends JPanel 45 implements JLabeledField, FocusListener 46 { 47 private JLabel mLabel; 48 private JTextField mTextField; 49 50 private ArrayList mChangeListeners = new ArrayList (3); 52 53 private String oldValue = ""; 55 56 59 public JLabeledTextField() 60 { 61 this("", 20); 62 } 63 64 70 public JLabeledTextField(String pLabel) 71 { 72 this(pLabel, 20); 73 } 74 75 public JLabeledTextField(String pLabel, int size) 76 { 77 super(); 78 mTextField = createTextField(size); 79 mLabel = new JLabel (pLabel); 80 mLabel.setLabelFor(mTextField); 81 init(); 82 } 83 84 public List getComponentList() 85 { 86 List comps = new LinkedList (); 87 comps.add(mLabel); 88 comps.add(mTextField); 89 return comps; 90 } 91 92 public void setEnabled(boolean enable) 93 { 94 super.setEnabled(enable); 95 mTextField.setEnabled(enable); 96 } 97 98 protected JTextField createTextField(int size) 99 { 100 return new JTextField (size); 101 } 102 103 106 private void init() 107 { 108 setLayout(new BorderLayout (5, 0)); 109 mTextField.addFocusListener(this); 113 114 add(mLabel, BorderLayout.WEST); 116 add(mTextField, BorderLayout.CENTER); 117 } 118 119 125 public void focusLost(FocusEvent pFocusEvent) 126 { 127 if (!oldValue.equals(mTextField.getText())) 129 { 130 notifyChangeListeners(); 131 } 132 } 133 134 137 public void focusGained(FocusEvent pFocusEvent) 138 { 139 oldValue = mTextField.getText(); 140 } 141 142 147 public void setLabel(String pLabel) 148 { 149 mLabel.setText(pLabel); 150 } 151 152 157 public void setText(String pText) 158 { 159 mTextField.setText(pText); 160 } 161 162 167 public String getText() 168 { 169 return mTextField.getText(); 170 } 171 172 177 public String getLabel() 178 { 179 return mLabel.getText(); 180 } 181 182 190 public void addChangeListener(ChangeListener pChangeListener) 191 { 192 mChangeListeners.add(pChangeListener); 193 } 194 195 200 public void removeChangeListener(ChangeListener pChangeListener) 201 { 202 mChangeListeners.remove(pChangeListener); 203 } 204 205 209 protected void notifyChangeListeners() 210 { 211 ChangeEvent ce = new ChangeEvent (this); 212 for (int index = 0; index < mChangeListeners.size(); index++) 213 { 214 ((ChangeListener ) mChangeListeners.get(index)).stateChanged(ce); 215 } 216 } 217 } 218 | Popular Tags |