Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can I execute a thread JPanel?

807598Oct 12 2006 — edited Oct 12 2006
I want my Jpanel to do a bouncing ball. So I decided to use a thread to update the coordinates regularly. How do I set the panel to visible and at the same time run run()? As you will see, it doesn't work (Code for drawing I haven't placed yet. Instead I placed System.out.println("hello"); to see if it runs):
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class bouncingBall extends JPanel implements Runnable
{
	private int balls;
	private int ballCount;
	
	public bouncingBall()
	{
		setSize(500,500);
		addMouseListener(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					
				}
				public void mousePressed(MouseEvent e)
				{
					
				}
				public void mouseReleased(MouseEvent e)
				{
					
				}
			}
				);
				
		ExecutorService threadExecutor = Executors.newFixedThreadPool( 1 );
		threadExecutor.execute( Thread.currentThread() ); 
	
	}
	public void run()
	{
		while(true)
		{
			System.out.println("Hello");	
		}
		
	}
	public void paintComponent(Graphics g)
	{
		
	}
	public static void main(String args[])
	{
		JFrame cage = new JFrame("Bouncing Balls");
		bouncingBall app = new bouncingBall();
		cage.setSize(app.getSize());
		cage.add(app);
		cage.setVisible(true);
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 9 2006
Added on Oct 12 2006
3 comments
2,003 views