CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Problem with Swing -.-

    Hey guys,

    i have a simple question.
    i have a JLabel jlbl_status in which i want so show the status of the current process.

    if i set the text with the setText() method the text is not displayed in that moment.
    It will be displayed after the whole action...

    for example:
    i have a JButton which trigges the following
    Code:
    log.info("recalculate Values");
    		RecalculateTableValuesJob job;
    		try {
    			job = new RecalculateTableValuesJob();
    		}catch(Exception e){
    			log.error("Cant start the recalc. job");
    			e.printStackTrace();
    			return;
    		}
    		setStatus("Calculation...");
    		setProgress(0);
    		job.start();
    		
    		while(job.isRunning){
    			try{Thread.sleep(1000); }catch(Exception e){}
    			int p = (int)(100*xlsData.getRowCount()/job.currentIndex);
    			setProgress(p);
    		}
    		setProgress(100);
    		
    		log.info("recalculate Values Done");
    btw. i also have the same problem with my JProgressBar ;-)

    my setStatus looks like
    Code:
    public void setStatus(String text) {
    		log.debug("Status: "+text);
    		jlbl_status.setText(text);
    	}
    i have tried also with
    jlbl_status.invalidate();
    jlbl_status.revalidate();
    jlbl_status.repaint();

    so PLEASE!! help me... why doesnt my JLabel repaint synchronously?

  2. #2
    Join Date
    Dec 2009
    Posts
    3

    Re: Problem with Swing -.-

    hey i found out, that when i do this action in my init of the program everything works fine.
    So it has something to do with my buttonevent?!
    Cuz only then it doent work -.-...

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Problem with Swing -.-

    You need to update your label text in the Swing Event dispatch Thread (EDT), not directly from your calculation thread. Swing provides a SwingWorker class you should use for background work that makes updating Swing components on the EDT very simple.

    See Concurrency In Swing for a full explanation & tutorial, with examples.

    It is a bad plan that admits of no modification...
    P. Syrus
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Dec 2009
    Posts
    3

    Re: Problem with Swing -.-

    It works!! =)
    You are a god ^^ why havn't i heard of this before?!...
    Well, thanks dlorde

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured