Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operations related to Swing components

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the operations related to Swing components are, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

An insensitive graphical user interface reduces the availability of the application. We usually say that the user interface is unresponsive when the following phenomena occur:

The phenomenon that ◆ does not respond to events

There is no update in ◆.

These phenomena are largely related to the way events are handled, and when writing Swing applications, we almost certainly have to write methods to respond to events such as mouse clicks and keyboard returns. In these methods, we will write some code to trigger some actions at run time. Common actions include finding and updating the database. In this article, through the analysis of an example, some basic concepts, common mistakes and a solution are introduced.

Event-dispatching thread

We must keep in mind that the code for event response methods is executed in event-dispatching thread unless you enable another thread.

So, what is event-dispatching thread? Single-threaded rule: once a Swing component is implemented (realized), all code that may affect or depend on the state of the component should be executed in event-dispatching thread. There are two ways to implement a component, calling show (), pack (), or setVisible (true) on the top-level component.

Add a component to a container that has been implemented.

The root cause of the single-thread rule is that most of the methods of the Swing component library are not safe for multithreading.

To support the single-threaded model, the Swing component library provides a thread dedicated to performing these operations related to Swing components, and this thread is event-dispatching thread. Our event response methods are usually called by this thread unless you write your own code to call these event response methods. A common mistake made by beginners here is to complete too much code in the event response method that is not directly related to modifying the component. The most likely effect is to cause the component to react slowly. For example, the following code responds to button events:

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 hold for a period of time until Done. It didn't play until it showed up. The reason is that both the update of the Swing component and the response to the event are done in event-dispatching thread, and when the event response, the event-dispatching thread is occupied by the event response method, so the component will not be updated. It is not possible to update the Swing component 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]) This is the end of this article on "what are the operations related to Swing components?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report