ThreadsAndSwing

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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 any thread other than the event thread to make changes to the components.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern