aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Instantiated components from within a thread aren’t repainting into a JFrame in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Instantiated components from within a thread aren’t repainting into a JFrame in Java" Watch "Instantiated components from within a thread aren’t repainting into a JFrame in Java" New topic
Author

Instantiated components from within a thread aren’t repainting into a JFrame in Java

Rigoberto Vides
Greenhorn

Joined: Feb 19, 2010
Posts: 4
NOTE: This thread is already at GameDev forum, but I think this problem should be of interest also in this forum. Thanks for your comprehension over this n00b.

Hi guys, I'm making this tetris variation, a piece should be spawned every 2 secs. I try to construct this piece (Block) from within a thread like this:


I'm using swingUtilities.invokeLater to send this thread to the swing event dispatcher, everything looks to work normal (i.e. the blocks instantiate each timeToSpawn) milisecs... but there is one BIG problem: they don't show on the Jframe! On the other hand whenever I just instantiate 1 or n blocks from within the same JFrame class like this for example:



They work perfectly fine and paint on the JFrame. What a mess.. I've been like crazy figuring out what's happening! My Block class extends Component and implements Runnable, it's run method looks like this:


It's a thread to update it's position each 450 milisecs... could this be the problem? although as I mentioned before, I just test this code and it worked, the problem is when I instantiate an object of this class within another thread.

As final and extra info. whenever I check the method toString on the Instantiated cubes from threads, they give me this [,0,0,0x0], but when I instantiate them within the same JFrame class they give me this result [,0,0,328x552] and they appear on the frame. this 328x552 value it's the same as the component's Dimension returned by getPreferredSize()... I tried to force them to that dimension by instantiate them like this:



But it didn't work, anyone knows what this [,0,0,328x552] value could mean?

Thanks in advance Everyone!

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Myself, I don't see a solution to your problem given the evidence you've presented, and it's a good bet that your bug is somewhere deep in code that you've not posted here. Keep using println statements of a debugger to try to isolate the problem, such as what you've already done (and that data sounds suspicious that your blocks have a 0x0 size).
Rigoberto Vides
Greenhorn

Joined: Feb 19, 2010
Posts: 4
Sorry for double-open a discussion, I didn't wanted to Is there any way that any admin or myself delete the one at GameDev?

I change the run() method on my BlockSpawner (that extends Thread) class to something like this:


And it apparently worked! spawns the first pair of blocks and they show on the JFrame, then, just shows the log "SPAWN" but doesn't paints the cubes... The way I construct this BlockSpawner Thread is something like this:



at the JFrame class. The toString() method continued to behave as I mentioned before:

Block:mainPkg.Block[,0,0,328x552] for the first (painted) pair of blocks
Block:mainPkg.Block[,0,0,0x0] for the rest of the non-painted blocks

Thank you for the support and sorry again.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
snippets are better than nothing, but I'd prefer to copy/paste/compile/run a demo program, so that I can
a) see what you see (or not see)
b) test some theories

strip everything unrelated to the problem, and post that
Rigoberto Vides
Greenhorn

Joined: Feb 19, 2010
Posts: 4
That Thread.sleep() is the bug's source... I placed it before instantiate the Blocks and now not even the first pair appears! Plus, i debbug the instantiated block with getSize(), and here are the results:

with pre Thread.sleep() instantiated blocks (only the first pair): size x:328 y:552
with post Thread.sleep() instantiated blocks (after the first loop): size x:0 y:0

just as @pete stein predicted...


Any ideas on how to fix this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Are you already aware how to handle concurrency in Swing?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiated components from within a thread aren’t repainting into a JFrame in Java
 
Similar Threads
Can anyone help me
GUI display(rendering) problem
java upgrade makes modal dialog lock up application
Applet working in appletviewer but no browser
Instantiated components from within a thread aren’t repainting into a JFrame in Java