使用 glasspane 从 swing 组件中删除鼠标事件/控件

发布于 2024-08-21 16:28:30 字数 1141 浏览 5 评论 0原文

我有一个客户端服务器应用程序,我在客户端使用 swing。我的 swing 客户端有一个主窗口 (jframe) 和许多面板、工具栏和菜单栏。 我想在客户端通过 glassPane 等待服务器响应时删除所有客户端操作/鼠标事件(或者只是抓取而不执行任何操作)。 这是我编写的代码:

private final static MouseAdapter mouseAdapter = new MouseAdapter() 
{
  public void mouseClicked(MouseEvent e) 
  {
   System.out.println("MouseClicked..!");
  }
 };

private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
private static Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);

但是

public static void startWaitCursor(JComponent comp)
{
  MainWindow root = ((MainWindow) comp.getTopLevelAncestor());

  root.getGlassPane().setCursor(WAIT_CURSOR);
  root.getGlassPane().addMouseListener(mouseAdapter);
  root.getGlassPane().setVisible(true);
}

public static void stopWaitCursor(JComponent comp)
{    
  MainWindow root = ((MainWindow) comp.getTopLevelAncestor()); 

  root.getGlassPane().setCursor(DEFAULT_CURSOR);
  root.getGlassPane().setVisible(false);
}

我无法管理抓取鼠标事件。更改 glassPane 上的光标工作正常,但我无法添加 mouseAdapter 或无法使 glassPane 成为顶级组件。

有什么想法吗?

谢谢。

I have a client-server application and i am using swing in the client side. My swing client has one main window (jframe) and lots of panels, toolbars and menubar in it.
I want to remove all client action/mouse events (or simply grab and do nothing) while client is waiting response from server by means of glasssPane.
Here is the code i wrote:

private final static MouseAdapter mouseAdapter = new MouseAdapter() 
{
  public void mouseClicked(MouseEvent e) 
  {
   System.out.println("MouseClicked..!");
  }
 };

private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
private static Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);

and

public static void startWaitCursor(JComponent comp)
{
  MainWindow root = ((MainWindow) comp.getTopLevelAncestor());

  root.getGlassPane().setCursor(WAIT_CURSOR);
  root.getGlassPane().addMouseListener(mouseAdapter);
  root.getGlassPane().setVisible(true);
}

public static void stopWaitCursor(JComponent comp)
{    
  MainWindow root = ((MainWindow) comp.getTopLevelAncestor()); 

  root.getGlassPane().setCursor(DEFAULT_CURSOR);
  root.getGlassPane().setVisible(false);
}

but i am not able to manage the grab mouse events. Changing cursors at the glassPane is working fine but either i am not able to add mouseAdapter or am not able to make glasssPane become to the top level component.

Any idea?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-08-28 16:28:31

我意识到我的代码正在工作,但我的问题与线程相关。我的代码类似于:

startWaitCursor(); 
work(); // server request that takes time 
stopWaitCursor();

并将其更改为:

startWaitCursor(); 
SwingUtilities.invokeLater(new Runnable() {
poblic void run() { 
try 
{ 
work(); // server request 
} 
finally 
{ 
stopWaitCursor(); 
}

通过进行此修改,我可以在客户端等待服务器响应时看到我在 startWaitCursor() 方法中所做的设置。

但还是有一个小问题。在 startWaitCursor() 方法中,我禁用了玻璃窗格的键、鼠标和焦点事件,但即使显示 glassPane,事件仍然被主框架捕获。

addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseMotionAdapter() {});
addKeyListener(this);
setFocusTraversalKeysEnabled(false);

服务器响应到达客户端并调用 stopWaitCursor() 方法后,主框架中处理的事件。

如果我在客户端等待时禁用应用程序的主框架,则光标不会更改为 wait_cursor,如果我不禁用主框架,则光标将被更改,但事件会排队。

干杯...

I realized that my code is working but my problem is threading related. My code was something like:

startWaitCursor(); 
work(); // server request that takes time 
stopWaitCursor();

and changed it to:

startWaitCursor(); 
SwingUtilities.invokeLater(new Runnable() {
poblic void run() { 
try 
{ 
work(); // server request 
} 
finally 
{ 
stopWaitCursor(); 
}

by doing this modification i could see the settings i made in the startWaitCursor() method while client is waiting response from the server.

But stil there is a small problem. In startWaitCursor() method i desabled key, mouse and focus events for the glass pane but events are still captured by main frame even glassPane is displayed.

addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseMotionAdapter() {});
addKeyListener(this);
setFocusTraversalKeysEnabled(false);

After server response reached to client and stopWaitCursor() method is invoked the events handled in the main frame.

If i disable the main frame of my application while client is waiting than cursor is not being changed to wait_cursor, if i am not disable the main frame then cursor is being changed but the events are queued.

cheers...

不甘平庸 2024-08-28 16:28:31

在挖掘了 Swing 线程问题几天后,我终于找到了真正的答案: SwingWorker

现在是我的最终代码类似于,

startWaitCursor();
SwingWorker worker = new SwingWorker() {
   public Object doInBackground() 
   {
      doWork(); // time consuming server request
      return null;
   }
   public void done() 
   {
      stopWaitCursor();
   }
};
worker.execute();

在 startWaitCursor() 方法中,我将 glasspane 设置为可见(具有 alpha 值背景),显示一条消息以警告用户正在执行耗时的工作,将光标设置为 wait_cursor (沙漏)并消耗所有按键、鼠标事件。就是这样。

通过使用 SwingWorker,我的客户端实际上是响应式的(它的工作方式就像没有发出服务器请求一样),但由于我显示玻璃窗格并消耗所有按键和鼠标事件,所以感觉没有响应。

真是松了一口气……SwingWorker 摇滚了……

干杯……

After digging swing threads issues couple of days, i finally found the real answer: SwingWorker

Now my final code is something like,

startWaitCursor();
SwingWorker worker = new SwingWorker() {
   public Object doInBackground() 
   {
      doWork(); // time consuming server request
      return null;
   }
   public void done() 
   {
      stopWaitCursor();
   }
};
worker.execute();

In startWaitCursor() method i set the glasspane visible (with alpha valued background), display a message to warn the user time consuming job is doing, set the cursor to wait_cursor (hourglass) and consume all the key, mouse events. That is it.

And by using SwingWorker my client is actually responsive (it is working as if no server request is made) but since i display the glasspane and consume all key and mouse events it feels like irresponsive.

What a relief.. SwingWorker rocks...

cheers..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文