Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Question about Swing and threads

Started by Kevinator Apr 8, 2005 at 11:27 AM 2 replies 1.2k views
Original Post
Kevinator
Kevinator
I was looking over the forum this morning and found a large thread discussing Swing and AWT. It was said that many Swing GUIs tend to be slow because programmers often stick a lot of code in event handlers, and people do their program logic inside the Swing thread. While I'm accquainted with threads, I'm not sure what is meant by the Swing thread, nor am I sure exactly what good practices are for event handlers. Take the following code for example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Driver {

    private Graphics2D g2;
    private JPanel dArea;
    int mapWidth, mapHeight, mHiP, mWip;
    // etc...

    public static void main(String[] args) {
        
        // The following code I found suggested by Sun in a tutorial
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	Driver d = new Driver();
                d.createGUI();
            }
        });
    }

    private void createGUI() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(555,600);
        frame.setLocation(50, 50);
        frame.setJMenuBar(createMenu());

        // set up other GUI components...

        frame.setVisible(true);
    }

    private JMenuBar createMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("A menu");
        JMenuItem item = new JMenuItem("A menu option");
        
        // Add event handler to menu option
        // is this acceptable? Is this bogging down my GUI?
        item.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {

                // do a bunch of logic...
		Room.size = Room.size / 2 + 1;
		mWiP = Room.size * mapWidth;
		mHiP = Room.size * mapHeight;
		dArea.setPreferredSize(new Dimension(mWiP, mHiP));
		dArea.revalidate();
		dArea.update(g2);
		dArea.repaint();
	    }
	});
        menu.add(item);
        menuBar.add(menu);
        return menuBar;
    }
}



Does that code display any nasty tendencies I should avoid with regards to threads, event handlers, or anything else? I really would like to create efficient GUIs in Swing. Thanks!
CaptainJester
CaptainJester
That is starting to look like you will overload the event thread. When you create an event handler, you register it with your Swing GUI. When ever one of those events that you are listening for occurs, your code is called from the event dispatch thread, not the main thread that your program is running in. So if you spend 2 seconds per call dealing with the event, another even cannot fire until you have returned from(exited) your event handling code. So the general rule is, if you are setting a couple of variables you are al right. If you are doing some calculations or looping, you should have that work run in another thread. In your event handling code, you create and start the thread. Then you can release contorl back to the event dispatch thread, so other event scan fire. The thread you started will get called shortly after(usually) to handle the task for that event.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Kevinator
Kevinator
That anon above me is me. Forgot to log in. ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.