Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java swing清除事件队列_Java_Swing_Event Dispatch Thread_Eventqueue - Fatal编程技术网

java swing清除事件队列

java swing清除事件队列,java,swing,event-dispatch-thread,eventqueue,Java,Swing,Event Dispatch Thread,Eventqueue,是否有可能以标准的方式进行此操作 下面是一个场景 在EDT中开始做一些昂贵的事情(EDT被阻止,直到昂贵的操作结束) EDT被阻止时,用户继续单击/拖动鼠标按钮。所有鼠标动作都记录在某处 当EDT是免费的(用昂贵的东西完成)时,它开始处理鼠标事件 在步骤3中,我想要的是丢弃堆积起来的鼠标事件。EDT空闲后,任何新的鼠标事件都应以常规方式处理 关于如何实现这一目标的任何想法 PS:我无法阻止EDT被阻止(我无法控制程序中某些模块的行为) 编辑: 如果我可以安全地调用“SunToolkit.flus

是否有可能以标准的方式进行此操作

下面是一个场景

  • 在EDT中开始做一些昂贵的事情(EDT被阻止,直到昂贵的操作结束)

  • EDT被阻止时,用户继续单击/拖动鼠标按钮。所有鼠标动作都记录在某处

  • 当EDT是免费的(用昂贵的东西完成)时,它开始处理鼠标事件

  • 在步骤3中,我想要的是丢弃堆积起来的鼠标事件。EDT空闲后,任何新的鼠标事件都应以常规方式处理

    关于如何实现这一目标的任何想法

    PS:我无法阻止EDT被阻止(我无法控制程序中某些模块的行为)

    编辑: 如果我可以安全地调用“SunToolkit.flushPendingEvents()”,那么在EDT中启动昂贵的操作之前,我总是可以放置一个glasspane。在昂贵的操作结束后,然后在EDT线程上刷新所有事件-它们将转到一个不会做任何事情的玻璃窗格。然后让EDT正常工作

    编辑2: 我添加了一个SSCCE来演示这个问题

    public class BusyCursorTest2 extends javax.swing.JFrame { public BusyCursorTest2() { javax.swing.JButton wait = new javax.swing.JButton("Wait 3 seconds"); getContentPane().setLayout(new java.awt.GridLayout(2, 1, 0, 0)); getContentPane().add(wait); getContentPane().add(new javax.swing.JToggleButton("Click me")); setTitle("Busy Cursor"); setSize(300, 200); setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE); setVisible(true); wait.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent event) { final java.util.Timer timer = switchToBusyCursor(BusyCursorTest2.this); try { //do something expensive in EDT try { Thread.sleep(3000); } catch (InterruptedException e) { //do nothing } } finally { switchToNormalCursor(BusyCursorTest2.this, timer); } } }); } public static java.util.Timer switchToBusyCursor(final javax.swing.JFrame frame) { startEventTrap(frame); java.util.TimerTask timerTask = new java.util.TimerTask() { public void run() { startWaitCursor(frame); } }; final java.util.Timer timer = new java.util.Timer(); timer.schedule(timerTask, DELAY_MS); return timer; } public static void switchToNormalCursor(final javax.swing.JFrame frame, final java.util.Timer timer) { timer.cancel(); stopWaitCursor(frame); stopEventTrap(frame); } private static void startWaitCursor(javax.swing.JFrame frame) { frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR)); frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } private static void stopWaitCursor(javax.swing.JFrame frame) { frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR)); frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } private static void startEventTrap(javax.swing.JFrame frame) { frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } private static void stopEventTrap(javax.swing.JFrame frame) { frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } private static final java.awt.event.MouseAdapter mouseAdapter = new java.awt.event.MouseAdapter() { }; public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new BusyCursorTest2(); } }); } private static final int DELAY_MS = 250; } 公共类BusyCursorTest2扩展了javax.swing.JFrame{ 公共业务客户2(){ javax.swing.JButton wait=newjavax.swing.JButton(“等待3秒”); getContentPane().setLayout(新java.awt.GridLayout(2,1,0,0)); getContentPane().add(等待); getContentPane().add(新建javax.swing.JToggleButton(“单击我”); setTitle(“忙碌光标”); 设置大小(300200); setDefaultCloseOperation(javax.swing.JFrame.DISPOSE\u ON\u CLOSE); setVisible(真); wait.addActionListener(新java.awt.event.ActionListener(){ 已执行的公共无效操作(java.awt.event.ActionEvent事件){ final java.util.Timer Timer=switchToBusyCursor(BusyCursorTest2.this); 试一试{ //在EDT中做一些昂贵的事情 试一试{ 睡眠(3000); }捕捉(中断异常e){ //无所事事 } }最后{ 开关电源或定时器(BusyCursorTest2.this,定时器); } } }); } 公共静态java.util.Timer switchToBusyCursor(最终javax.swing.JFrame){ startEventTrap(框架); java.util.TimerTask TimerTask=new java.util.TimerTask(){ 公开募捐{ 起始光标(帧); } }; final java.util.Timer Timer=new java.util.Timer(); 时间表(timerTask,DELAY_MS); 返回计时器; } 公共静态void switchToNormalCursor(最终javax.swing.JFrame框架,最终java.util.Timer计时器){ timer.cancel(); 停止等待光标(帧); stopEventTrap(框架); } 私有静态void startWaitCursor(javax.swing.JFrame){ frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_Cursor)); frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } 私有静态void stopWaitCursor(javax.swing.JFrame){ frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_Cursor)); frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } 私有静态void startEventTrap(javax.swing.JFrame){ frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } 私有静态void stopEventTrap(javax.swing.JFrame){ frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } private static final java.awt.event.MouseAdapter MouseAdapter=new java.awt.event.MouseAdapter(){ }; 公共静态void main(字符串[]args){ javax.swing.SwingUtilities.invokeLater(新的Runnable(){ 公开募捐{ 新的BusyCursorTest2(); } }); } 专用静态最终整数延迟_MS=250; }
  • 运行SSCCE

  • 点击按钮“等待3秒”。它模拟了一个昂贵的操作。鼠标光标将变为“忙”

  • 光标忙时,单击切换按钮“单击我”。如果在三秒后,切换按钮更改其状态,则切换按钮接收到鼠标事件,并且未捕获鼠标事件

  • 我希望当光标看起来很忙时,生成的鼠标(和其他)事件被丢弃

    谢谢。

    读一下这个

    基本上,长时间运行的任务不应该在EDT上完成。Java为类似这样的任务提供了SwingWorker


    我想说得更详细些,但你不愿意接受答案。

    好的,我终于把所有的东西都准备好了。我在发布SSCCE以获得一个正确工作的示例。诀窍是使用“javax.swing.SwingUtilities.invokeLater()”方法隐藏glasspane。将必要的代码包装在可运行的文件中,然后使用invokeLater调用它。在这种情况下,Swing处理所有鼠标事件(因为glasspane截获了它们,所以不会发生任何事情),然后隐藏glasspane。这是SSCCE

    public class BusyCursorTest2 extends javax.swing.JFrame { public BusyCursorTest2() { javax.swing.JButton wait = new javax.swing.JButton("Wait 3 seconds"); getContentPane().setLayout(new java.awt.GridLayout(2, 1, 0, 0)); getContentPane().add(wait); getContentPane().add(new javax.swing.JToggleButton("Click me")); setTitle("Busy Cursor"); setSize(300, 200); setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE); setVisible(true); wait.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent event) { final java.util.Timer timer = switchToBusyCursor(BusyCursorTest2.this); try { //do something expensive in EDT or otherwise try { Thread.sleep(3000); } catch (InterruptedException e) { //do nothing } } finally { switchToNormalCursorEventThread(BusyCursorTest2.this, timer); } } }); } public static java.util.Timer switchToBusyCursor(final javax.swing.JFrame frame) { startEventTrap(frame); java.util.TimerTask timerTask = new java.util.TimerTask() { public void run() { startWaitCursor(frame); } }; final java.util.Timer timer = new java.util.Timer(); timer.schedule(timerTask, DELAY_MS); return timer; } public static void switchToNormalCursorEventThread(final javax.swing.JFrame frame, final java.util.Timer timer) { Runnable r = new Runnable() { public void run() { switchToNormalCursor(frame, timer); } }; javax.swing.SwingUtilities.invokeLater(r); } public static void switchToNormalCursor(final javax.swing.JFrame frame, final java.util.Timer timer) { timer.cancel(); stopWaitCursor(frame); stopEventTrap(frame); } private static void startWaitCursor(javax.swing.JFrame frame) { frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR)); frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } private static void stopWaitCursor(javax.swing.JFrame frame) { frame.getGlassPane().setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR)); frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } private static void startEventTrap(javax.swing.JFrame frame) { frame.getGlassPane().addMouseListener(mouseAdapter); frame.getGlassPane().setVisible(true); } private static void stopEventTrap(javax.swing.JFrame frame) { java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue(); frame.getGlassPane().removeMouseListener(mouseAdapter); frame.getGlassPane().setVisible(false); } private static final java.awt.event.MouseAdapter mouseAdapter = new java.awt.event.MouseAdapter() { }; public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new BusyCursorTest2(); } }); } private static final int DELAY_MS = 250; } 公共类BusyCursorTest2扩展了javax.swing.JFrame{ 公共业务客户2(){ javax.swing.JButton wait=newjavax.swing.JButton(“等待3秒”); getContentPane().setLayout(新java.awt.GridLayout(2,1,0,0)); getContentPane().add(等待); getContentPane().add(新建javax.swing.JToggleButton(“单击我”); setTitle(“忙碌光标”); 设置大小(300200); setDefaultCloseOperation(javax.swing.JFrame.DISPOSE\u ON\u CLOSE); setVisible(真); wait.addActionListener(新java.awt.event.ActionListener(){ 公众的
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Toolkit;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    /**
     * When blocking the EDT (Event Queue) in swing, the cursor won't update, and windows won't render.
     * This should show the hourglass even when you're blocking the EDT.
     *
     * Source:
     * https://stackoverflow.com/questions/7085239/java-swing-clear-the-event-queue
     * 
     * @author Kieveli, Santosh Tiwari
     *
     */
    public class BlockingWaitCursor {
    
       private static final java.awt.event.MouseAdapter mouseAdapter = new java.awt.event.MouseAdapter() {};
    
       /**
        * The Dialog or main window is required to show the cursor and animate it. The actionListener is called
        * as soon as initial setup is completed and the animation timer is running.
        * @param currentComponent A panel, dialog, frame, or any other swing component that is the current focus
        * @param action Your action to perform on the EDT. This is started extremely quickly and without delay.
        */
       public static void showWaitAndRun(Component currentComponent, ActionListener action ) {
    
          Timer timer = setupWaitCursor(currentComponent);
    
          try {
             // now allow our caller to execute their slow and delayed code on the EDT
             ActionEvent event = new ActionEvent(BlockingWaitCursor.class, ActionEvent.ACTION_PERFORMED, "run");
             action.actionPerformed(event);
          }
          finally {
             resetWaitCursor(currentComponent, timer);
          }
       }
    
       private static Timer setupWaitCursor(Component currentComponent) {
          final Component glassPane = findGlassPane(currentComponent);
          if ( glassPane == null ) {
             return null;
          }
    
          // block mouse-actions with a glass pane that covers everything
          glassPane.addMouseListener(mouseAdapter);
          glassPane.setVisible(true);
    
          // animate the wait cursor off of the EDT using a generic timer.
          Timer timer = new Timer();
          timer.schedule( new TimerTask() {
             @Override
             public void run() {
                glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                glassPane.addMouseListener(mouseAdapter);
                glassPane.setVisible(true);
             }
    
          }, 250l);
    
          return timer;
       }
    
       private static void resetWaitCursor(Component currentComponent, final Timer timer) {
          final Component glassPane = findGlassPane(currentComponent);
          if ( glassPane == null ) {
             return;
          }
          // Invoke later so that the event queue contains user actions to cancel while the loading occurred
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                if ( timer != null )
                   timer.cancel();
                Toolkit.getDefaultToolkit().getSystemEventQueue();
                glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                glassPane.removeMouseListener(mouseAdapter);
                glassPane.setVisible(false);  
             }
          });
       }
    
       private static Component findGlassPane(Component currentComponent) {
          // try to locate the glass pane by looking for a frame or dialog as an ancestor
          JFrame frame = findFrame(currentComponent);
          JDialog dialog = findDialog(currentComponent);
          Component glassPane = null;
          if ( frame != null )
             glassPane = frame.getGlassPane();
          if ( dialog != null )
             glassPane = dialog.getGlassPane();
          return glassPane;
       }
    
       private static JFrame findFrame(Component currentComponent) {
          // find the frame if it exists - it may be the currentComponent
          if ( currentComponent instanceof JFrame )
             return (JFrame) currentComponent;
    
          Window window = SwingUtilities.getWindowAncestor(currentComponent);
          if ( window == null )
             return null;
          if ( ! (window instanceof JFrame) )
             return null;
          return (JFrame)window;
       }
    
       private static JDialog findDialog(Component currentComponent) {
          // find the dialog if it exists - it may be the currentComponent
          if ( currentComponent instanceof JDialog )
             return (JDialog) currentComponent;
    
          Window window = SwingUtilities.getWindowAncestor(currentComponent);
          if ( window == null )
             return null;
          if ( ! (window instanceof JDialog) )
             return null;
          return (JDialog)window;
       }
    
    }