简体   繁体   中英

How to queue up a bunch of user driven java swing events

I have a bunch of checkboxes and radio buttons that allow a user to filter out items in a JTable .Each of the events need to be threaded to avoid the GUI freezing up.

What I would like to do is queue up the events and act on them in the order they were received.

Currently I have the following design but I think there must be a better way. Basically, I have for each potential queued method, an integer that maps to it. That integer value is added to a list in each actionPerformed method execution of integers that are looped through and translated to function calls.

I was possibly thinking about using a EventQueue to call run on it. What is the best pattern or technique for this type of functionality?

Vector<Integer> taskQueue;
/**
 * Adds tasks to a queue things are completed in the order they were added. This       allows for
 * threading and concurrency issues.One task is done at a time.
 * @param  
 */ 
private void doTasks()
{
     if(!queueIsRunning)
     {
         doTasks = new Thread(){
            public void run()
            {
               queueIsRunning = true;
                while(!taskQueue.isEmpty())
                {
                    intVal = taskQueue.get(0);

                    taskQueue.remove(0);
                    switch(intVal)
                    {
                      case DOM_HIGHLIGH_TASK:
                          System.out.println("starting dom highlight");
                          highlightDom();
                          break;
                      case FLASH_HIGHLIGH_TASK:
                          System.out.println("starting flash highlight");
                          higlightFlash();
                          break;
                      case SQL_HIGHLIGH_TASK: 
                          System.out.println("starting sql highlight");
                          highlightSQL();
                          break;
                      case INFO_HIGHLIGH_TASK:   
                          System.out.println("starting infoleak highlight");
                          highlightInfoLeak();
                          break;
                      case HTMLCOM_HIGHLIGH_TASK:
                          System.out.println("starting htmlcomm highlight");
                          highlightHtmlComments();
                          break;
                      case SORT_EXTENSIONS:
                          sortExtensions();
                    }
                }
               queueIsRunning = false;

            }
        };
        doTasks.start();
     }

}

The Event Dispatch Thread (EDT) has its own queue

SwingUtilities.invokeLater(new Runnable(){
    @Override
    public void run(){
        //do stuff
    }
});

Why is that not sufficient? If these are long-running tasks (which is hard to imagine), I'd check outSwingWorker .

If you're going for that pattern, use the event dispatch as mentioned by the other answer, but also ArrayBlockingQueue , which is present from Java 1.5 onwards. Then you can just use the take() method which will block until the command queue has elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM