Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Thread to EDT

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Thread to EDT

    So I am having an issue with making a thread as EDT. This is the prototype of program I have:
    public class Program implements Runnable {
    	public Program() {
    		new Thread(this).start();
    	}
     
    	public static void main(String[] args) {
    		Program p = new Program();
    	}
     
    	public void paintComponent(g) {
    		...
    	}
     
    	public void run() {
    		while(true) {
    			...
    			repaint();
    			Thread.sleep(x);
    		}
    	}
    }
    The program also has a keyListener and the repainting is related with the button is pressed. I tried to do like this, but it just made the program to freeze:
    public class Program {
    	public Program() {
    		SwingUtilities.invokeLater/invokeAndWait(new Runnable() {
    			public void run() {
    				while(true) {
    					...
    					repaint();
    					Thread.sleep(x);
    				}
    			}
    		});
    	}
     
    	public static void main(String[] args) {
    		Program p = new Program();
    	}
     
    	public void paintComponent(g) {
    		...
    	}
    }
    I've read several topics about EDT, but I couldn't find info about the issue I have.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Thread to EDT

    Swing already has its own EDT. Dispatching another Runnable to be executed on this thread that is an infinite loop (your second example) will lock of the EDT and hence anything that needs to be run on the EDT (for example repaint). Any particular reason you wish to 'make your own EDT'?

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Thread to EDT

    Quote Originally Posted by copeg View Post
    Any particular reason you wish to 'make your own EDT'?
    I have the bones of the program which uses a thread I wrote in the first post and the guy I am doing this for told me to consider running it in EDT instead. It is just a simple game. Everything what the program does is recalculates the positions based on pressed buttons and repaints.
    Last edited by Asido; October 15th, 2010 at 10:33 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Thread to EDT

    Not sure if there still is a problem or if what I posted above helps. But here is a link to one of the more thorough articles related to threading and Swing: Threads and Swing

Similar Threads

  1. Thread runs only once and not again
    By enflation in forum Threads
    Replies: 3
    Last Post: June 9th, 2010, 10:51 AM
  2. Is thread the best option?
    By 256mxr in forum Threads
    Replies: 1
    Last Post: May 22nd, 2010, 08:24 PM
  3. Thread Blocked
    By Rakesh_Yadav in forum Threads
    Replies: 1
    Last Post: February 19th, 2010, 10:54 AM
  4. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 05:23 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM