Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to "split off" processor-intensive calculations in Swing ActionListener?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default How to "split off" processor-intensive calculations in Swing ActionListener?

    I am writing a program to solve Sudoku puzzles. My user interface is written in Swing.

    When a user presses a "Solve" button, an ActionListener for this button is called. This ActionListener invokes a method that solves the Sudoku. However, this solver method is highly processor-intensive. The result is that my entire application becomes unresponsive.

    I've tried putting the solver method in its own thread, but this has exactly the same results. I don't know why. Perhaps because the thread is created and run from an ActionListener method?

    What are the best practices for "splitting off" processor-intensive calculations that are invoked by the user interface?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to "split off" processor-intensive calculations in Swing ActionListener?

    Be sure the UI/Swing portion of the app is being run on the EDT. Processes that are processor intensive should be run in a SwingWorker. You can find several articles on how to do this by searching 'swing multithreading' and reviewing tutorials and documentation for SwingWorker.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to "split off" processor-intensive calculations in Swing ActionListener?

    Thanks. I'll read more about SwingWorker.

    Just to be sure. My UI code (class ViewController_Graphics) is invoked as follows:

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ViewController_Graphics view = new ViewController_Graphics(model);
                }
            } );

    Is this the correct way to run it on the EDT?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to "split off" processor-intensive calculations in Swing ActionListener?

    If ViewController_Graphics presents the Swing UI, then it looks like you're doing the EDT part correctly, though I'm not sure of the purpose of the object 'view'. Sometimes that happens with a work in progress, so don't sweat it.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. nextLine().split(",") not work correctly
    By saeedeh in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 5th, 2014, 11:02 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. String.split"char"; error
    By bean in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 3rd, 2013, 05:30 PM
  5. Replies: 4
    Last Post: October 3rd, 2012, 07:40 AM