Documentos de Académico
Documentos de Profesional
Documentos de Cultura
27/09/06
JTextFields
JComponent JTextComponent JTextField JPasswordField
Single line of text Usually used for simple data entry, may be used as labels if you disable editing UNICODE compliant Supports
Text selection Clipboard operations
27/09/06
JTextField Constructors
JTextField() JTextField(String text) JTextField(int columns)
Columns in JTextField are based on lower case ms in the current font Does not limit input length
JTextField(String text, int columns) JTextField(Document doc, String text, int columns)
Uses the chosen document model.
JPasswordField Constructors
same arglist as JTextField
JPasswordField methods
public char getEchoChar() public void setEchoChar(char c) public boolean echoCharIsSet() Be aware that Java bytecode may pose significant problems when validating passwords. The JPasswordField is merely a JTextField with empty characters echoed back to the window.
3
27/09/06
public void scrollRectToVisible(Rectangle r) public Action[] getActions() protected Document createDefaultModel() public setScrollOffset(int offset)
Number of pixels of the text string which are out of view to the let of the controls visible area
From JTextComponent
public void setEditable(boolean canEdit)
27/09/06
27/09/06
One way to do it is pretend the JLabel is the target of the accelerator, but in fact the JTextField is the target
Use the setDisplayedMnemonic() method for JLabel
27/09/06
Note that setFocusAccelerator() is now commented out Use setLabelFor(Component c) method to link JLabel to component When mnemonic activated for JLabel, it will pass the focus to the component given to it by setLabelFor()
27/09/06
27/09/06
27/09/06
Using JTextFields
A text field is a basic text control that lets the user type in a small amount of text and fires an action event when the user indicates that text entry is complete (usually by pressing Return). To validate an entry in a text field
Container-validated Action-validated Keystroke-validated. Focus-validated Combination of the above (JFormattedTextField)
27/09/06
10
Container Validation
Sometimes the best way Monitor the closing or completion of a GUI screen and check all values then Think about all of the ways you can leave a panel
Window Closing OK/Cancel Leave tabbed pane Hide window Change of focus
27/09/06
11
Action Validation
The data in an action-validated field is checked each time the field fires an action event (each time the user presses the Return key). An actionvalidated field might, at any given point in time, contain invalid data. However, the data is validated before it's used for anything.
To create an action-validated field, provide an action listener for your field and implement its actionPerformed method as follows:
Use getText to get the contents of the text field. Evaluate the value returned by getText. If the value is valid, do whatever task or calculation is required. If the value is invalid, report an error and return without performing a task or calculation.
27/09/06
12
27/09/06
13
You can listen for editing changes by way of adding a PropertyChangeListener and listening for change events named value.
27/09/06
14
27/09/06
15
Locking Focus
To lock the focus down while the JFormattedTextField is an invalid edit state you can attach an InputVerifier.
public class FormattedTextFieldVerifier extends InputVerifier { public boolean verify(JComponent input) { if (input instanceof JFormattedTextField) { JFormattedTextField ftf = (JFormattedTextField)input; AbstractFormatter formatter = ftf.getFormatter(); if (formatter != null) { String text = ftf.getText(); try { formatter.stringToValue(text); return true; } catch (ParseException pe) { return false; } } } return true; } public boolean shouldYieldFocus(JComponent input) { return verify(input); } }
27/09/06
16
You can force the current value to be obtained from the current JFormattedTextField.AbstractFormatter by way of invoking commitEdit. commitEdit will be invoked whenever return is pressed in the JFormattedTextField.
27/09/06
17
27/09/06
18
27/09/06
19
27/09/06
20
27/09/06
21
Code Example,ValidatedTextField.java
27/09/06
22
27/09/06
23
Methods
void changedUpdate(DocumentEvent e)
Gives notification that an attribute or set of attributes changed.
void insertUpdate(DocumentEvent e)
Gives notification that there was an insert into the document.
void removeUpdate(DocumentEvent e)
Gives notification that a portion of the document has been removed
27/09/06
24
setLabelFor()
Set a displayed mnemonic on a JLabel
27/09/06
25
27/09/06
26
JTextComponent
So far, most JComponents covered have had very simple input and output event handling
Mostly dealing with ActionListeners for Buttons
JTextComponent provides basic functionality found in most text input objects Initial Text support in AWT was limited
Single font in a window No embedded graphics
JComponent JTextComponent JTextArea
JTextField JEditorPane
JPasswordField
27/09/06
27
27/09/06
28
JTextComponent components
JTextComponent is the foundation for Swing's text components, and provides these customizable features for all of its descendants:
A separate model, known as a document, to manage the component's content. A separate view, which is in charge of displaying the component on screen. A separate controller, known as an editor kit, that can read and write text and implements editing capabilities with action commands. Customizable keymaps and key bindings. Support for infinite undo/redo. Pluggable caret and support for caret change listeners.
27/09/06
29
27/09/06
30
JTextComponent features
Clipboard operations
textComponent.copy() textComponent.cut() textComponent.paste()
Save/Load operations
Direct input/output to stream using JTextComponent Stream may be local file or URL
textComponent.read(xcReadStream, http://www.mysite.com) textComponent.read(xcStreamStream)
27/09/06
31
Text operations
public public public public public void setText(String t) void getText() String getSelectedText() void replaceSelection(String content) String getText(int offset, int length) throws BadLocationException
Move caret
public public public public public Caret getCaret() void setCaret(Caret c) void setCaretPosition(int position) int getCaretPosition() void moveCaretPosition()
27/09/06
32
27/09/06
33
Document handling
In AWT, text was contained by several different types of objects, and TextListener and TextEvent classes watched for changes in that text. This proved to be limiting The Document class holds a document, but users of the Document class are responsible for displaying the contents Document is a Swing-based container used to hold text and to provide notifications of changes to such text.
Includes ability to mark-up(select text) Manage changes to text
In JDK 1.4, if JTextComponent does not have a Tooltip Text associated, the view under the mouse will be asked to provide one
If the View corresponds to an HTML element with an ALT attribute, then the tooltip text will be the value of that attribute.
27/09/06
34
27/09/06
35
Document structure
Code Example, PlainTextModel.java
27/09/06
36
Document class
selecting text
public abstract String getText(int offset, int length) throws BadLocationException public abstract void getText(int offset, int length, Segment text) throws BadLocationException public abstract void insertString(int offset, String str, AttributeSet a) throws BadLocationException public abstract void remove(int offset, int length) throws BadLocationException
Get/set properties
public abstract Object getProperty(Object key) public abstract void putProperty(Object key, Object value)
27/09/06
38
DocumentListener
A DocumentListener interface is designed to watch for changes in a document and react to them. You should never modify the contents of text component from within a document listener. If you attempt to modify the text in a text component from within a document listener, your program will likely deadlock!
27/09/06
39
Editor Kits
It is the editor kit that provides the code that actually inserts new content in a document, deletes characters, moves the cursor, allows the user to cut and paste etc All Swing text components supports standard editing commands such as cut, copy, paste, and inserting characters. Each editing command is represented and implemented by an action object.
The set of actions that an editor provides can be determined by calling its getActions() method
This makes it easy for you to associate a command with a GUI component, such as a menu item or button, and build a GUI around a text component. Under the hood, a text component uses an EditorKit object to create and manage its actions. Editor Kits supplied by JDK
27/09/06
40
There is a getText(int, int, Segment) call which sets the array, offset, and count members to point to a char array that contains data in the specified range. In some cases the char[] will point to the original data, so dont modify text in a segment
27/09/06 41
Positions in a document
Although the insertString remove, and getText methods use absolute offsets to specify locations within the model, you often want some relative ones.
Supports the change of content within the model If you have a pointer to the beginning of the text, and new is inserted into it, the start position is no longer valid.
27/09/06
42
27/09/06
44
27/09/06
45