In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the methods provided by SwingUtilities". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what are the methods provided by SwingUtilities?"
Java Swing GUI multithreaded SwingUtilities.invokeLater and invokeAndWait in Java Swing is not thread-safe and is a single-threaded design. As a result, Swing components that will be drawn on the screen can only be accessed from the event dispatch thread. An event dispatching thread is a thread that calls callback methods such as paint and update. It is also an event handling method defined in the event listener interface. For example, the actionPerformed method in ActionListener is called in the event dispatching thread.
Swing is event-driven, so it is natural to update the visible GUI in the callback function. For example, when a button is pressed and the item list needs to be updated, the update of the list is usually implemented in the actionPerformed method of the event listener associated with the button, and it is not normal to update the Swing component from a thread other than the event dispatch thread.
Sometimes you need to update the Swing component from a thread other than the event dispatch thread. For example, there are time-consuming operations in actionPerformed that take a long time to return, and it takes a long time after the button is activated to see the updated list. The button will remain pressed for a long time until the actionPerformed returns. Generally speaking, time-consuming operations should not be performed in event handling methods, because event handling returns. Other events cannot be triggered, and the interface is similar to a stuck condition, so it may be better to perform more time-consuming operations on a separate thread, which immediately updates the user interface and releases the event dispatching thread to dispatch other events.
The SwingUtilities class provides two methods: invokeLate and invoteAndWait, both of which queue runnable objects on the event dispatch thread. When the runnable object is at the head of the event dispatch queue, its run method is called. The effect is to allow the event dispatching thread to call any block of code in another thread.
StartButton.addActionListener (new ActionListener ()) {public void actionPerformed (ActionEvent e) {GetInfoThread t = new GetInfoThread (Test.this); t.start (); startButton.setEnabled (false);}} class GetInfoThread extends Thread {Test applet; public GetInfoThread (Test applet) {this.applet = applet;} public void run () {while (true) {try {Thread.sleep Applet.getProgressBar () .setValue (Math.random () * 100);} catch (InterruptedException e) {e.printStackTrace ();}
An important difference between invokeLater and invoikeAndWait: invokeLater can be called from the event dispatch thread, but invokeAndWait cannot be called from the event dispatch thread. The problem with calling invokeAndWait from the event dispatch thread is that invokeAndWait locks the thread that calls it until the runnable object is dispatched from the event dispatch thread and the run method of the runnable object is activated. If invoikeAndWait is called from the event dispatch thread, deadlock occurs Because invokeAndWait is waiting for event dispatch, however, because invokeAndWait is called from the event dispatch thread, events cannot be dispatched until invokeAndWait returns.
The event dispatch thread can dispatch the thread only when actionPerformed (); is returned, while the use of invokeAndWait in actionPerformed causes actionPerformed to fail to return. So it is not possible to dispatch threads in invokeAndWait.
Because Swing is thread-safe, it is not safe to access Swing components from threads other than the event dispatch thread, and the SwingUtilities class provides both methods for executing code in the event dispatch thread
The above is all the contents of the article "what are the methods provided by SwingUtilities". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.