Content deleted Content added
→Loosely coupled and MVC: Removed useless letters. Tags: Manual revert Mobile edit Mobile web edit |
No edit summary |
||
Line 167:
Notice how all instantiation and handling of Swing components are done by creating an instance of the class,
which implements the Runnable interface. This is then run on the [[Event Dispatch Thread]] by use of the method {{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|invokeLater(java.lang.Runnable)|module=java.desktop}}), created in the main method (see [[Event dispatching thread#Swing and thread safety|Swing and thread safety]]). Although Swing code can be run without using this technique (for instance, by not implementing Runnable and moving all commands from the run method to the main method), it is considered to be good form, as Swing is not [[Thread safety|thread-safe]], meaning that invoking resources from multiple threads can result in thread interference and memory consistency errors.<ref>http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html The Event Dispatch Thread</ref>
===Text Field===
Text fields enable users to input text or data into your application. Creating a text field in Swing is straightforward – instantiate a JTextField object and add it to a container.
import javax.swing.*;
public class TextFieldExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("Text Field Example");
// Create a JTextField
JTextField textField = new JTextField(20);
// Add the text field to the JFrame
frame.add(textField);
// Set the size of the JFrame and make it visible
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Enhancing functionality in text fields improves user interaction. By attaching DocumentListener interfaces, you can dynamically monitor changes in the text content, enabling real-time validation, formatting, or auto-completion of input data.
Validating text field input is crucial for ensuring data integrity and preventing errors. Swing provides multiple validation techniques, including regular expressions, input masks, or custom validation logic. By implementing InputVerifier interfaces, you can define specific validation rules and offer immediate feedback to users when input is invalid.<ref>https://geeksprogramming.com/java-swing-tutorial-for-beginners/ The Event Dispatch Thread</ref>
===Another example===
|