2004-08-12 Janis Johnson <janis187@us.ibm.com>
[official-gcc.git] / libjava / javax / swing / JTextField.java
blob8872af6906da9d975f4d575ba0a434c6a318ae03
1 /* JTextField.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.awt.Dimension;
41 import java.awt.Font;
42 import java.awt.FontMetrics;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import javax.accessibility.AccessibleStateSet;
46 import javax.swing.text.Document;
47 import javax.swing.text.JTextComponent;
48 import javax.swing.text.PlainDocument;
51 public class JTextField extends JTextComponent
52 implements SwingConstants
54 /**
55 * AccessibleJTextField
57 protected class AccessibleJTextField extends AccessibleJTextComponent
59 private static final long serialVersionUID = 8255147276740453036L;
61 /**
62 * Constructor AccessibleJTextField
64 protected AccessibleJTextField()
68 /**
69 * getAccessibleStateSet
70 * @return AccessibleStateSet
72 public AccessibleStateSet getAccessibleStateSet()
74 return null;
78 private static final long serialVersionUID = 353853209832607592L;
80 public static final String notifyAction = "notify-field-accept";
82 private int columns;
84 private int align;
86 /**
87 * Creates a new instance of <code>JTextField</code>.
89 public JTextField()
91 this(null, null, 0);
94 /**
95 * Creates a new instance of <code>JTextField</code>.
97 * @param text the initial text
99 public JTextField(String text)
101 this(null, text, 0);
105 * Creates a new instance of <code>JTextField</code>.
107 * @param columns the number of columns
109 * @exception IllegalArgumentException if columns %lt; 0
111 public JTextField(int columns)
113 this(null, null, columns);
117 * Creates a new instance of <code>JTextField</code>.
119 * @param text the initial text
120 * @param columns the number of columns
122 * @exception IllegalArgumentException if columns %lt; 0
124 public JTextField(String text, int columns)
126 this(null, text, columns);
130 * Creates a new instance of <code>JTextField</code>.
132 * @param doc the document to use
133 * @param text the initial text
134 * @param columns the number of columns
136 * @exception IllegalArgumentException if columns %lt; 0
138 public JTextField(Document doc, String text, int columns)
140 setDocument(doc == null ? createDefaultModel() : doc);
141 setText(text);
142 setColumns(columns);
146 * Creates the default model for this text field.
147 * This implementation returns an instance of <code>PlainDocument</code>.
149 * @return a new instance of the default model
151 protected Document createDefaultModel()
153 return new PlainDocument();
157 * Returns the class ID for the UI.
159 * @return "TextFieldUI";
161 public String getUIClassID()
163 return "TextFieldUI";
167 * Adds a new listener object to this text field.
169 * @param listener the listener to add
171 public void addActionListener(ActionListener listener)
173 listenerList.add(ActionListener.class, listener);
177 * Removes a listener object from this text field.
179 * @param listener the listener to remove
181 public void removeActionListener(ActionListener listener)
183 listenerList.remove(ActionListener.class, listener);
187 * Returns all registered <code>ActionListener</code> objects.
189 * @return an array of listeners
191 * @since 1.4
193 public ActionListener[] getActionListeners()
195 return (ActionListener[]) getListeners(ActionListener.class);
199 * Sends an action event to all registered
200 * <code>ActionListener</code> objects.
202 protected void fireActionPerformed()
204 ActionEvent event = new ActionEvent(this, 0, notifyAction);
205 ActionListener[] listeners = getActionListeners();
207 for (int index = 0; index < listeners.length; ++index)
208 listeners[index].actionPerformed(event);
212 * Returns the number of columns of this text field.
214 * @return the number of columns
216 public int getColumns()
218 return columns;
221 public void setColumns(int columns)
223 if (columns < 0)
224 throw new IllegalArgumentException();
226 this.columns = columns;
227 invalidate();
228 repaint();
231 public int getHorizontalAlignment()
233 return align;
236 public void setHorizontalAlignment(int newAlign)
238 int oldAlign = align;
239 align = newAlign;
240 invalidate();
241 repaint();
242 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
245 public void setFont(Font newFont)
247 super.setFont(newFont);
248 revalidate();
251 public Dimension getPreferredSize()
253 Dimension size;
254 FontMetrics fm = getFontMetrics(getFont());
255 int fontHeight = fm.getMaxAscent() + fm.getMaxDescent();
256 int columnWidth = fm.charWidth('m');
258 if (columns != 0)
260 size = new Dimension(columns * columnWidth + 4, fontHeight + 4);
262 else
264 size = new Dimension(10, 10);
267 return size;