?

Log in

No account? Create an account

Previous Entry | Next Entry

Java...

This is driving me nuts. Why doesn't this work properly???

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Main
{
	public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	public static int screenWidth = (int)screenSize.getWidth();
	public static int screenHeight = (int)screenSize.getHeight();
	public static int width = (int)screenWidth/5; 
	public static int height = (int)screenHeight/12;
	
	public static void main(String [] args)
	{
		//Creates a JFrame
		JFrame frameA = new JFrame("JFrame A");		
		frameA.setResizable(false);
		frameA.setSize(Main.width, Main.height);
		frameA.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
		frameA.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JProgressBar bar;
		bar = new JProgressBar(0, 100);
		bar.setStringPainted(true);
		frameA.add(bar);
		frameA.setVisible(true);
		
		//Runs through a fake progress test
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				for (int a = 0; a < 5000; a++)
					System.out.print(0);
	
				bar.setValue(i*10 + j + 1);
			}
		}
		
		//Get rid of JFrame A
		frameA.dispose();
		
		//Creates JFrame B
		JFrame frameB = new JFrame("JFrame B");		
		frameB.setResizable(false);
		frameB.setSize(Main.width, Main.height);
		frameB.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
		frameB.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		//Adds a button to the JFrame
		JButton button = new JButton("Go");

		//Lets us listen to the button being pressed
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//Creates JFrame C				
				JFrame frameC = new JFrame("JFrame C");		
				frameC.setResizable(false);
				frameC.setSize(Main.width, Main.height);
				frameC.setLocation((Main.screenWidth - Main.width)/2, (Main.screenHeight - Main.height)/2);
				frameC.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				
				//Adds a JProgressBar to the JFrame
				JProgressBar bar;
				bar = new JProgressBar(0, 100);
				bar.setStringPainted(true);
				frameC.add(bar);
				frameC.setVisible(true);
				
				//Runs through a fake progress test
				//WHY DOESN'T THIS SHOW UP PROPERLY?
				for (int i = 0; i < 10; i++)
				{
					for (int j = 0; j < 10; j++)
					{
						for (int a = 0; a < 5000; a++)
							System.out.print(0);
			
						bar.setValue(i*10 + j + 1);
					}
				}

				frameC.dispose();
			}
		});
		
		frameB.add(button);
		frameB.setVisible(true);
	}
}


I figured it out, thanks to a helpful guy at the Java Developers Forums.. multithreading's a bitch.

Comments

( 2 comments — Leave a comment )
chris462
Aug. 28th, 2008 06:55 am (UTC)
is this the fixed code? i don't see the diff.
happinessiseasy
Aug. 28th, 2008 02:56 pm (UTC)
No, I didn't modify it on here. The fix actually has some interesting implications for our software at work.

The problem is that Swing does everything GUI-related on the EventDispatchThread. So if you do a long, arduous task on the EDT, it ties up the GUI, blocking repaints, and any other Action/Key Listener events, effectively freezing the program until it's done. I think you can imagine which task we do that I might be referring to. So, in order to draw a progress bar FROM the EDT, you must invoke another thread. The easiest way is with

new Thread(new Runnable() { public void run () {//do something } }).start();

This lets the EDT hand it off to a new thread and then return from the actionPerformed() method to be able to respond to GUI events again. However, since Swing isn't thread-safe, the only way to actually draw a GUI object (such as a progress bar) from a NON-EDT thread is to use SwingUtilities.invokeLater(//change the GUI); which will place the task on the EDT stack to be performed by the EDT when it has time.
( 2 comments — Leave a comment )

Latest Month

December 2010
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Page Summary

Powered by LiveJournal.com
Designed by Naoto Kishi