Monday 15 March 2010

java - Why isn't my JFrame repainting like I told it to? -



java - Why isn't my JFrame repainting like I told it to? -

when instantiating modified jpanel class, pass in file through constructor. file (xml) gets read , info gets used me later in paint method. however, right after parse data, phone call both repaint() , revalidate(), gui looks same. phone call these methods both in main class extends jframe , panel class extends jpanel.

when take xml file jfilechooser, drawpanel class gets instantiated other constructor, taking in file , parsing it, calling repaint , revalidate. omitted of code save time.

here's main class' code:

public class mycode extends jframe { public mycode() { super("roadway simulator"); setsize(800, 600); setlocationrelativeto(null); setdefaultcloseoperation(jframe.exit_on_close); setlayout(null); drawie = new drawpanel(); drawie.setsize(new dimension(width - 200, height)); drawie.setminimumsize(new dimension(width - 200, height)); drawie.setmaximumsize(new dimension(width - 200, height)); drawie.setlocation(0, 0); add(drawie); setvisible(true); try{thread.sleep(500);revalidate();repaint();}catch(interruptedexception eeee){} } public static void main(string[] args){new mycode();} }

here's drawpanel class:

class drawpanel extends jpanel { boolean drawroad = false; public drawpanel() { super(); } public drawpanel(document doc){ super(); //the alter in paint method drawroad = true; revalidate(); repaint(); } public paint(graphics g){ super.paint(g); if(drawroad){ g.setcolor(color.black); g.fillrect(0,0,600,600); } } }

my code same above, lot more detail. why isn't jframe repainting?

here:

try{thread.sleep(500);revalidate();repaint();}catch(interruptedexception eeee){}

understand thread.sleep(...) swing gui when called on swing event thread -- puts current thread happens swing event thread, 1 responsible drawing , user interaction, sleep. in other words, set entire application sleep.

solution -- don't phone call ever on event thread.

as aside, there's no cost putting each method phone call on own line, , no reason long line you've posted serves no purpose other confuse.

java swing jframe jpanel repaint

No comments:

Post a Comment