我有一堆复选框和单选按钮,允许用户过滤 JTable
中的项目。每个事件都需要线程化以避免 GUI 卡住。
我想做的是将事件排队并按照收到的顺序对其进行操作。
目前我有以下设计,但我认为一定有更好的方法。基本上,对于每个潜在的排队方法,我都有一个映射到它的整数。该整数值会添加到整数的每个 actionPerformed
方法执行中的列表中,这些整数将被循环并转换为函数调用。
我可能正在考虑使用 EventQueue 来调用它的运行。此类功能的最佳模式或技术是什么?
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();
}
}
最佳答案
事件调度线程 (EDT)
有自己的队列
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
//do stuff
}
});
为什么这还不够?如果这些是长时间运行的任务(这很难想象),我会查看 SwingWorker .
关于java - 如何对一堆用户驱动的 java swing 事件进行排队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6158623/