使用 glasspane 从 swing 组件中删除鼠标事件/控件
我有一个客户端服务器应用程序,我在客户端使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到我的代码正在工作,但我的问题与线程相关。我的代码类似于:
并将其更改为:
通过进行此修改,我可以在客户端等待服务器响应时看到我在 startWaitCursor() 方法中所做的设置。
但还是有一个小问题。在 startWaitCursor() 方法中,我禁用了玻璃窗格的键、鼠标和焦点事件,但即使显示 glassPane,事件仍然被主框架捕获。
服务器响应到达客户端并调用 stopWaitCursor() 方法后,主框架中处理的事件。
如果我在客户端等待时禁用应用程序的主框架,则光标不会更改为 wait_cursor,如果我不禁用主框架,则光标将被更改,但事件会排队。
干杯...
I realized that my code is working but my problem is threading related. My code was something like:
and changed it to:
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.
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...
在挖掘了 Swing 线程问题几天后,我终于找到了真正的答案: SwingWorker
现在是我的最终代码类似于,
在 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,
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..