Java GUI: In Java for TextField monitoring __java

Source: Internet
Author: User
Tags gettext object object
As a cross-platform programming solution, Java obviously cannot provide powerful, intuitive, and fast design support in user interface design as Delphi does. For example, for text box control, when we want to feel in the program whether the user changes the contents of the text box, Delphi provides a direct event call OnValueChanged (), can be activated when the text box content is actually changed. and to implement this function in Java seems to be a bit troublesome, direct calls only keypressed () and keytyped (), the former only on the main keyboard area (characters, numbers, etc.) have reacted to the delete and other function keys do not respond, the latter seems to react too sensitive, It also responds positively to some function keys that do not change the contents of the text box.
So how do you set up event handling similar to OnValueChanged () for text boxes in Java? This depends first of all on whether the text box we use belongs to the AWT class or swing class. The AWT (abstract window Toolkit: Abstraction Windows Toolbox) was originally written to allow Java to have UI design capabilities, but was not applauded in version 1.0. After the Java 1.1 version, the new event-driven approach has been added to truly become practical, convenient and stable. The Swing class is designed to cope with the deficiencies of AWT, a component library that was launched after Java 1.1, which is based on beans (the component of Swing is bean), so it is a "lightweight" player relative to AWT. Swing more "Naturally" supports more object-oriented event-driven, designed UI skins that can be dynamically changed on different platforms and operating systems. However, in some cases, due to its rigorous encapsulation characteristics, the implementation process appears to be more verbose than the AWT class. Obviously, swing classes should be the preferred design UI if you don't take into account the continued use of old code.
Using the AWT class
For the TextField class in AWT, the key to realizing the monitoring is the use of textlistener. TextListener's statement reads as follows:
Interface TextListener extends EventListener
It is defined in the Java.awt.event package. This interface is provided to users to listen for changes in text content. The method it contains is textvaluechanged (), and the complete declaration is as follows:
public void textvaluechanged (TextEvent e) {}
When the content of the text in an object changes, the event is triggered and the statement specified in the method is executed. Note that TextListener is an interface (interface) and must first define a new class to execute (implements) it. The new class name defined in this article is awt_onvaluechanged.
In the description statement for the form, add a TextListener event for TextField (the TextField object named TextField1 in this article):
Textfield1.addtextlistener (New
Awt_onvaluechanged ());
The textvaluechanged () event is finally overloaded:
public void textvaluechanged (TextEvent e)
{
Write the actual code to achieve the desired function
......
}
Using Swing classes
For JTextField classes in swing, the process of implementing monitoring is relatively complex. Instead of setting textlistener in the JTextField, the monitoring task for the text is placed in a different interface document. Therefore, first you must request a document interface object for the JTextField object, using the method of Jtextfield1.getdocument () (the JTextField object named in this article is JTextField1). Once you have obtained the document, you can use Adddocumentlistener () to get a listening interface similar to the TextListener function. The full statement of Documentlistener is as follows:
Interface Documentlistener extends EventListener
It is defined in the package Java.swing.event.DocumentListener, which contains three methods:
? public void Changedupdate (Documentevent e): Listening for changes in text attributes;
? public void Insertupdate (Documentevent e): Insert event that listens for textual content;
? public void Removeupdate (Documentevent e): A deletion event that listens for textual content.
As with processing TextField components, defining a new class (this article is swing_onvaluechanged), adding a description, writing code, you can implement the functionality you want.
Programmatic implementation
The Java code for the key components of the implementation monitoring function is given below. The complete code is compiled and run on the Java virtual machine, as shown in Figure 1:

Figure 1

Changes are made in two edit boxes or when they are entered or deleted or checked, and as long as the contents of the text box are changed exactly, the corresponding event is triggered to produce the output (on the character interface).
public class JFrame1 extends JFrame
{
Public JFrame1 ()
{//Generate graphical interface
......
Event definition for the Exit button
Btnaction exitaction = new Btnaction ();
Jbutton1.addactionlistener (exitaction);
To define text monitoring for TextField components
Textfield1.addtextlistener (New Awt_onvalue
Changed ());
To define text monitoring for JTextField components
Jtextfield1.getdocument (). Adddocumentlistener
(New swing_onvaluechanged ());
}
Define new Awt_onvalue based on interface TextListener
Changed
Class Awt_onvaluechanged implements TextListener
{
public void textvaluechanged (TextEvent e)
{//Output changes and results
System.out.println ("Text Changed:" +textfield1.gettext ());
}
}
Define a new class based on the interface Documentlistener swing_onvaluechanged
Class Swing_onvaluechanged implements Documentlistener
{//Output changes and results
public void Changedupdate (Documentevent e) {
System.out.println ("Attribute Changed" +e);
}
public void Insertupdate (Documentevent e)
{//Output changes and results
System.out.println ("Text Inserted:" +textfield1.gettext ());
}
public void Removeupdate (Documentevent e)
{//Output changes and results
System.out.println ("Text Removed:" +textfield1.gettext ());
}
}
Define a new class that implements time monitoring for the exit button
Class Btnaction implements ActionListener
{//Receive Event
public void actionperformed (ActionEvent event)
{
Object object = Event.getsource ();
if (object = = JButton1)
Jbutton1_actionperformed (event);
}
}
Exit Button Event
void Jbutton1_actionperformed (java.awt.event.
ActionEvent event)
{//Exit program
System.exit (0);
}
}
Summary
In fact, in addition to text boxes (TextField and JTextField), as long as the text editing area, including textarea, JTextArea, Jtextpane, and so on, can use the above methods to implement the OnValueChanged () event, To monitor the content of the text area.
The above code is compiled in JDK 1.2 environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.