Write a sensitive graphical user interface with swing

Source: Internet
Author: User
Tags sleep thread

An insensitive graphical user interface lowers the usability of your application. When the following occurs, we usually say that the user interface is not responsive.

The phenomenon of not responding to events;

There is no renewal of the phenomenon;

These phenomena are largely related to the way events are handled, and when writing swing applications, we almost certainly have to write a method to respond to the mouse click button, keyboard return, and other events. In these methods we are going to write some code that triggers some action at run time. Common actions include finding, updating databases, and so on. In this article, through an analysis of an example, some basic concepts, common errors and a solution are presented.

Event-dispatching thread

We must remember that the code for the event response method is executed in event-dispatching thread, unless you enable another thread.

So, what is event-dispatching thread? Single-thread rule: Once a swing component is implemented (realized), all code that is likely to affect or depend on the state of this component should be executed in event-dispatching thread. There are two ways of implementing a component:

Call Show (), pack (), or setvisible (true) for the top-level component;

Adds a component to a container that has already been implemented.

The root of a single thread rule is that most of the methods of the Swing component library are not secure for multithreading.

To support a single threading model, the Swing component library provides a thread that is dedicated to accomplishing these operations related to swing components, and the first thread is event-dispatching thread. Our event response methods are usually called by this thread, unless you write your own code to invoke these event response methods. One of the common mistakes beginners often make is to do too many code in the event response method that is not directly associated with modifying the component. The most likely effect is to cause the component to react slowly. For example, the following code that responds to a button event:

String str = null;
this.textArea.setText("Please wait...");
try {
 //do something that is really time consuming
 str = "Hello, world!";
 Thread.sleep(1000L);
} catch (InterruptedException e) {
 e.printStackTrace();
}
this.textArea.setText(str);

The effect after execution is that the button seems to have been fixed for some time until it is done. The reason is that swing component updates and event responses are done in event-dispatching thread, and when the event responds, Event-dispatching thread is occupied by the event response method, so the component is not updated. It is not possible to update swing components until the event response method exits.

To solve this problem, someone might try to update the component by calling the repaint () method:

final String[] str = new String[1];
this.jTextArea1.setText("Please wait...");
this.repaint();
try {
 Thread.sleep(1000L);
}catch(InterruptedException e) {
 e.printStackTrace();
}
str[0] = "Done.";
jTextArea1.setText(str[0]);

But this method does not work as expected, the button is still fixed for some time, after viewing the source code of the repaint () method to know why.

PaintEvent e = new PaintEvent(this, PaintEvent.UPDATE,
new Rectangle(x, y, width, height));
Toolkit.getEventQueue().postEvent(e);

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.