DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006

    Swing & threading: Trying to use invokeLater in a loop and failing

    Hi everyone,
    First off I'd like to say thank you for providing a community where I can ask questions that I can't find solutions to anywhere else.
    I've been working on some code for awhile that takes an image from a file or a camera and processes it, then calls another class to create and mantain the GUI. I'm completely new to the execution of threading, but I understand the concept - I'm just such a neophyte that I can't make the two converge.
    Anyway, my processing code works and my GUI sort of does. I've been teaching myself Swing while working on the GUI, so please pardon its messyness.
    Problem: GUI fails to repaint when I call a long processing method/methods. I know this can be solved with threads, but I don't know how to impliment them.
    Here's what I'd like to do, and here's what I've tried:
    Goal: Everything in the GUI works except my jToggleButton. I would like it to execute a method only when it is selected - meaning that I'll have to find a way to break an infinite loop. One problem, though, is that my GUI is not updating so even if I had the code, I couldn't just say "click again to stop." I'd also like my GUI to update a few components (or the whole thing, doesn't matter) after every iteration of the loop.
    What I've tried: Just about everything I could find. I TRIED threading but it confused me, I've tried just using for loops to see if invokeLater or invokeAndWait would work, and I've tried putting my update code just about everywhere. I've also used repaint() paintImmediately(), etc. I think the fact that I'm calling from within a loop means that even invokeLater won't execute until the loop is "done" - which it never is, since I can't stop it. It does update after the for loop is complete.

    Here's some code from the updateGUI thread - I'd be happy to post more, but I code messily. My two classes are easily 1,000 lines (with comments) together. I've posted the sections that are most directly related to my problems.

    private void jToggleButton1_itemStateChanged(ItemEvent e) throws IOException, NoPlayerException, CannotRealizeException, InterruptedException, AWTException, InvocationTargetException
      	while (jToggleButton1.isSelected())
      	{ 		processObj.startFromGUI(processObj,false,true,edgeFind,leftFront,rightFront,deadCenter);//This should be for getting camera images	
      		Thread.sleep(50);//Pause so we don't overload the camera
      Runnable doRun = new Runnable() { public void run() { updateGUI(processObj); }};
      public void updateGUI(Processor processObj)
        jLabel500.setIcon(new ImageIcon(processObj.BI));//Update large image
        jLabel750.setIcon(new ImageIcon(processObj.processed));//Update small image
        jLabel11.setText("Steering servo value is " + processObj.Steering_PWM + ".");//Update servo value
        jLabel12.setText("Column heading is " + processObj.goTowards + ".");//Update goTowards value  	
        jLabel16.setText("Brake servo value is " + processObj.Brake_PWM + ".");//Update servo value
    Thank you!

  2. #2
    Join Date
    Mar 2006


    You could try using a Timer; this should cause less stress to your GUI. In this case, add a TimerTask member variable and replace your jToggleButton1_itemStateChanged() with that below.

    TimerTask timerTask = null;
    private void jToggleButton1_itemStateChanged(ItemEvent e) throws IOException, NoPlayerException, CannotRealizeException, InterruptedException, AWTException, InvocationTargetException
      	if (jToggleButton1.isSelected())
      	    processObj.startFromGUI(processObj,false,true,edge  Find,leftFront,rightFront,deadCenter);//This should be for getting camera images	
      	    timerTask = new TimerTask() { public void run() { updateGUI(processObj); }};
      	    Timer.schedule(timerTask, 50, 50);
      	else {
      	    if (timerTask != null) {
      	        timerTask = null;

  3. #3
    Join Date
    Feb 2006
    Thanks, I solved this a few days ago by remaking all my threads and then making the toggle switch them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center