ThreadsAndSwing

ThreadsAndSwing - Threads and Swing C HAPTER 9 IN THIS...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 9 Threads and Swing IN THIS CHAPTER • Why Isn’t the Swing Toolkit Multithread- Safe? 222 • Using SwingUtilities.invoke AndWait() 223 • Using SwingUtilities.invokeLater() 227 • Using SwingUtilities.isEventDispatch Thread() 230 • When invokeAndWait() and invokeLater() Are Not Needed 231 • The Need for Worker Threads in a GUI Setting 231 • Using a Worker Thread to Relieve the Event Thread 236 • Scrolling Text in a Custom Component 244 • Animating a Set of Images 249 • Displaying Elapsed Time on a JLabel 254 • Floating Components Around Inside a Container 257
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Swing graphical toolkit brings a host of new components to the Java platform. There’s a catch, though—Swing components are not designed for a multithreaded environment. In this chapter, I’ll show how you how to safely interact with Swing components in a multithread-safe manner using SwingUtilities.invokeAndWait() and SwingUtilities.invokeLater() . I’ll also show you some ways that animation can be achieved using Swing components and threads. Why Isn’t the Swing Toolkit Multithread-Safe? After Swing components have been displayed on the screen, they should only be operated on by the event-handling thread. The event-handling thread (or just event thread) is started auto- matically by the Java VM when an application has a graphical interface. The event thread calls methods like paint() on Component , actionPerformed() on ActionListener , and all of the other event-handling methods. Most of the time, modifications to Swing components are done in the event-handling methods. Because the event thread calls these methods, it is perfectly safe to directly change components in event-handling code. SimpleEvent (see Listing 9.1) shows safe Swing code. L ISTING 9.1 SimpleEvent.java—Safe Swing Code That Uses the Event Thread 1: import java.awt.*; 2: import java.awt.event.*; 3: import javax.swing.*; 4: 5: public class SimpleEvent extends Object { 6: private static void print(String msg) { 7: String name = Thread.currentThread().getName(); 8: System.out.println(name + “: “ + msg); 9: } 10: 11: public static void main(String[] args) { 12: final JLabel label = new JLabel(“————”); 13: JButton button = new JButton(“Click Here”); 14: 15: JPanel panel = new JPanel(new FlowLayout()); 16: panel.add(button); 17: panel.add(label); 18: 19: button.addActionListener(new ActionListener() { 20: public void actionPerformed(ActionEvent e) { 21: print(“in actionPerformed()”); 22: label.setText(“CLICKED!”); Threads P ART I 222
Background image of page 2
23: } 24: }); 25: 26: JFrame f = new JFrame(“SimpleEvent”); 27: f.setContentPane(panel); 28: f.setSize(300, 100); 29: f.setVisible(true); 30: } 31: } In SimpleEvent , two threads interact with the Swing components. First, the main thread creates the components (lines 12–15), adds them to panel (lines 16–17), and creates and configures a JFrame (lines 26–29). After setVisible() is invoked by main (line 29), it is no longer safe for
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

ThreadsAndSwing - Threads and Swing C HAPTER 9 IN THIS...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online