Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 05: More Swing Components © NIKO IBRAHIM, MIT PROGRAM STUDI SISTEM INFORMASI UNIVERSITAS KRISTEN MARANATHA Text Component Swing offers sophisticated text components, from plain-text entry boxes to HTML interpreters. It's a huge subject; we'll just scratch the surface here. More information: Learning Java, Ch 18.1 O’Reilly Java Swing 2nd Ed, Ch 19 Lots of Text Component subclasses: JTextField, JTextArea, JFormattedTextField, DocumentFilter, InputVerifier, JPasswordField, HTML & RTF viewer, JTextPane JTextField & JTextArea: TextEntryBox App. JFormattedTextField The JFormattedTextField component provides explicit support for editing complex formatted values such as numbers and dates. JFormattedTextField acts somewhat like a JTextField, except that it accepts a format-specifying object in its constructor and manages a complex object type (such as Date or Integer) through its setValue( ) and getValue( ) methods. FormatedFields.java Filtering Input JFormattedTextField does not know about all format types itself, instead it uses AbstractFormatter objects that know about particular format types. AbstractFormatter provide implementations of two interfaces: DocumentFilter & NavigationFilter A DocumentFilter attaches to implementations of Document and allows you to intercept editing commands, modifying them as you wish. DocumentFilter provides a low-level, edit-by-edit means of controlling or mapping user input. Filtering Input Example: DocFilter.java Validating Data Whereas character filtering prevents you from entering incorrect data, field validation happens after data has been entered. Normally, validation occurs when the user tries to change focus and leave the field, either by clicking the mouse or through keyboard navigation. Java 1.4 added the InputVerifier API, which allows you to validate the contents of a component before focus is transferred. Although we are going to talk about this in the context of text fields, an InputVerifier can actually be attached to any JComponent to validate its state in this way. Validating Data Example: Validator.java Password Input A JPasswordField behaves just like a JTextField (it's a subclass), except every character typed is echoed as the same, obfuscating character, typically an asterisk. The creation and use of JPasswordField is basically the same as for JTextField. If you find asterisks distasteful, you can tell the JPasswordField to use a different character using the setEchoChar( ) method. Normally, you would use getText( ) to retrieve the text typed into the JPasswordField. This method, however, is deprecated; you should use getPassword( ) instead. The getPassword( ) method returns a character array rather than a String object. This is done because character arrays are a little less vulnerable than Strings. Password Example: LoginDialog.java Timer The Timer class provides a mechanism to generate timed events. It has properties and events, and thus can be used in application builders that understand JavaBeans. It fires an ActionEvent at a given time. The timer can be set to repeat, and an optional initial delay can be set before the repeating event starts. Timer Example: ClockLabel.java & ClockTest.java Tree Tree components help you visualize hierarchical information and make traversal and manipulation of that information much more manageable. A tree consists of nodes, which can contain either a user-defined object along with references to other nodes, or a user-defined object only. (Nodes with no references to other nodes are commonly called leaves.) In modern windowing environments, the directory list is an excellent example of a tree. More resources about Tree nodes, event, selection: see “O’Reilly Java Swing 2nd Ed, Ch 17” Example of a Simple Tree: TestTree.java More Resources… O’Reilly Java Swing 2nd Edition, Chapter 15-17 Learning Java 3rd Ed, Ch 18 JFC Swing Tutorial Summary Congratulations, during the lecture you’ve learnt many more advanced swing components!