- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我们正在将 Swing 应用程序转换为 SWT,它已经可以使用了。真正让我抓狂的是,在 Windows 上使用 SWT(与 Swing 相比)时,即使鼠标光标位于另一个控件上,也只会滚动焦点控件(例如表格、列表、多行文本字段)。
是否有可能在我们的应用程序中更改此行为(不必安装第三方实用程序),例如通过为滚动事件安装一些独立于控件的钩子(Hook)/过滤器,将事件重定向到当前光标位置的控件,或者首先自动移动焦点。提前致谢。
最佳答案
原来的解决方案有很多问题。
下面是一个准备好复制的代码片段,它基于原始答案但处理了所有这些问题。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Scrollable;
/**
* The standard platform behavior on Windows is to scroll the widget with
* keyboard focus when the user turns the mouse wheel, instead of the widget
* currently under the mouse pointer. Many consider this annoying and Windows
* itself, as well as many popular Windows software, breaks this rule and
* implements the behavior seen on other platforms, which is to scroll the
* widget under the mouse.
*
* Win32MouseWheelFilter is a Listener implementation which will filter for
* SWT.MouseWheel events delivered to any Widget and try to redirect the event
* to the widget under the mouse or one of it's parents. The widget, or one of
* it's parents is considered a suitable target, if it either has Listeners for
* SWT.MouseWheel attached (assuming that those listeners would do something
* sensible with the event), or if its style bits contain SWT.H_SCROLL and/or
* SWT.V_SCROLL. In the later case a low level system event is generated, which
* is necessary to get the event handled by the native ScrollBar widgets. A
* vertical ScrollBar is preferred as the target, unless it is for some reason
* unsuitable for scrolling. In that case, horizontal scrolling would take
* place, if there is a suitable horizontal ScrollBar.
*
* Simply creating a new Win32MouseWheelFilter instance will install it as an
* event filter in the Display passed to the constructor. At an appropriate
* time, you may call dispose() to remove the filter again. On SWT platforms
* other than "win32", constructing an Win32MouseWheelFilter will have no effect.
*/
public class Win32MouseWheelFilter implements Listener {
private final Display fDisplay;
private int WM_VSCROLL;
private int WM_HSCROLL;
private int SB_LINEUP;
private int SB_LINEDOWN;
private Method fSendEventMethod32;
private Method fSendEventMethod64;
/**
* Creates a new Win32MouseWheelFilter instance and registers it as global
* event filter in the provided Display. Nothing will happen if the SWT
* platform is not "win32". If for some reason some SWT internals have
* changed since the writing of this class, and the Reflection-based
* extraction of some win32 specific fields of the SWT OS class fails,
* no filtering of wheel events will take place either.
*
* @param display
* The Display instance that the Win32MouseWheelFilter should install
* itself into as global event filter.
*/
public Win32MouseWheelFilter(Display display) {
fDisplay = display;
if (!SWT.getPlatform().equals("win32"))
return;
try {
Class<?> os = Class.forName("org.eclipse.swt.internal.win32.OS");
WM_VSCROLL = os.getDeclaredField("WM_VSCROLL").getInt(null);
WM_HSCROLL = os.getDeclaredField("WM_HSCROLL").getInt(null);
SB_LINEUP = os.getDeclaredField("SB_LINEUP").getInt(null);
SB_LINEDOWN = os.getDeclaredField("SB_LINEDOWN").getInt(null);
try {
// Try the 32-bit version first
fSendEventMethod32 = os.getDeclaredMethod("SendMessage",
int.class, int.class, int.class, int.class);
} catch (NoSuchMethodException e) {
// Fall back to the 64-bit version
fSendEventMethod64 = os.getDeclaredMethod("SendMessage",
long.class, int.class, long.class, long.class);
}
display.addFilter(SWT.MouseWheel, this);
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
System.out.println("Warning: Running on win32 SWT platform, "
+ "but unable to install Win32MouseWheelFilter filter.");
}
/**
* If the receiver had previously installed itself as global event filter,
* this method will remove it again from the display's filters.
*/
public final void dispose() {
fDisplay.removeFilter(SWT.MouseWheel, this);
}
public final void handleEvent(Event event) {
Control cursorControl = event.display.getCursorControl();
if (event.widget == cursorControl || cursorControl == null)
return;
if (event.widget instanceof Control) {
// If the original target control's bounds contain the mouse
// location, do not re-target the event, since it may indeed be the
// Control that needs to handle scrolling for an embedded Control
// that has focus.
Control control = (Control) event.widget;
Rectangle bounds = control.getBounds();
bounds.x = 0;
bounds.y = 0;
Point cursorPos = control.toControl(display.getCursorLocation());
if (bounds.contains(cursorPos))
return;
}
// Try to find the best target widget for the event, based on the
// cursorControl. A suitable target control is either one that has
// a listener for SWT.MouseWheel attached, or one that has either
// SWT.H_SCROLL or SWT.V_SCROLL in its style bits.
Control wheelControl = cursorControl;
int scrollStyle = SWT.H_SCROLL | SWT.V_SCROLL;
while (wheelControl != null
&& (wheelControl.getStyle() & scrollStyle) == 0
&& wheelControl.getListeners(SWT.MouseWheel).length == 0) {
wheelControl = wheelControl.getParent();
}
if (wheelControl == null) {
// The event would not be handled by anyone, bail out.
return;
}
int style = wheelControl.getStyle();
if ((style & scrollStyle) != 0 && wheelControl instanceof Scrollable) {
// Construct the data for the low level event based on which
// direction the target can scroll in. We need to use a low-level
// event since otherwise it won't be handled by the native
// ScrollBar widgets.
int msg;
// Prefer vertical scrolling. However, if the
// there is no vertical ScrollBar, or if it's somehow disabled,
// then switch to horizontal scrolling instead.
if ((style & SWT.V_SCROLL) != 0 ) {
ScrollBar vBar = ((Scrollable) wheelControl).getVerticalBar();
if (vBar == null
|| ((vBar.getMinimum() == 0
&& vBar.getMaximum() == 0
&& vBar.getSelection() == 0)
|| !vBar.isEnabled()
|| !vBar.isVisible())) {
// There is no vertical ScrollBar, or it can't be used.
msg = WM_HSCROLL;
} else
msg = WM_VSCROLL;
} else {
msg = WM_HSCROLL;
}
int count = event.count;
int wParam = SB_LINEUP;
if (event.count < 0) {
count = -count;
wParam = SB_LINEDOWN;
}
try {
// Obtain the control's handle via Reflection and
// deliver the event using the low level platform method.
// (64 and 32 bit versions)
if (fSendEventMethod32 != null) {
int handle = org.eclipse.swt.widgets.Control.class
.getDeclaredField("handle").getInt(wheelControl);
for (int i = 0; i < count; i++)
fSendEventMethod32.invoke(null, handle, msg, wParam, 0);
} else {
long handle = org.eclipse.swt.widgets.Control.class
.getDeclaredField("handle").getLong(wheelControl);
for (int i = 0; i < count; i++)
fSendEventMethod64.invoke(null, handle, msg, wParam, 0);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
} else {
// It makes no sense using the low-level OS event delivery, since
// Widgets without the scrolling style bits won't receive this
// event. Since we selected this widget based on the fact that it
// has SWT.MouseWheel listeners attached, use the regular SWT event
// notification system.
// Convert mouse location, since the event contains it in the wrong
// coordinate space (the one of the original event target).
Point cursorPos = wheelControl.toControl(
event.display.getCursorLocation());
event.x = cursorPos.x;
event.y = cursorPos.y;
event.widget = wheelControl;
wheelControl.notifyListeners(event.type, event);
}
// We re-targeted the event, or re-posted a new event to another widget,
// so prevent this event from being processed any further.
event.type = SWT.None;
event.doit = false;
}
}
关于java - Windows 上的 SWT : scroll control at cursor (not focused one),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6629160/
我实际上是在尝试在 Java SWT 中重现类似的东西,但到目前为止还没有成功: 我看到的问题是,据我所知,SWT.TITLE 需要显示 SWT.CLOSE、SWT.MAX 和 SWT.MIN 谁能告
以下是我的代码: Group leftGroup = new Group(parent, SWT.NONE); leftListWidget = new org.eclipse.swt.widgets
我正在尝试运行一个 hello world SWT 应用程序: public static void main(String args[]) throws IOException{ Displ
我有一个 SWT 窗口,其中有组小部件,我在其中放置了几个其他小部件,我设置了组的标题及其工作正常。组标题颜色始终为蓝色(在我的情况下我不确定)并且不会与组内的其他 child 同步。所以我想知道是否
我正在使用 SWT CTabFolder 在我的应用程序中构建选项卡。我有点不喜欢标签的视觉外观,它太像 Eclipse。有没有办法改变标签的方面?我想更改关闭按钮,例如标签的位置,它们的外观可能..
对于我在使用大型 SWT 表的 RCP 应用程序中追逐了一段时间的问题,这更多是我想分享的答案。 问题是 SWT Table.remove(int start, int end) 方法的性能。它提供了
如何在不使用鼠标监听器的情况下编辑 SWT 表值? 最佳答案 做 TableEditor以下链接中的片段有帮助吗? SWT Snippets TableEditor中的第一个例子部分使用 Select
我想要一个带有两行工具提示的控件。例如 Label: Label label = new Label( parent, SWT.NONE ); label.setText( "label text"
A 想在我的 View 上加载下拉菜单时添加默认文本。我怎样才能在 SWT 和 JFace 中做到这一点? 最佳答案 我猜您想要做的是在将某些内容添加到 View 时在组合中显示某些内容(默认情况下它
在我的登录对话框中,有一个按钮: Text pwdT = new Text(container, SWT.BORDER|SWT.PASSWORD); Button plainBtn = new Bu
如何以编程方式垂直滚动 SWT 表? 在表上实现搜索功能。当找到一个项目时,它将滚动到找到的项目。 最佳答案 您可能想尝试几种方法: Table.showItem(TableItem) Table.s
我有一个 Composite我希望能够以编程方式启用/禁用。 Control.setEnabled(boolean enabled)方法工作正常,但它没有提供任何小部件被禁用的视觉信息。 我想做的是让
有几个“高级”表格/电子表格 SWT 小部件(Nattable、Nebula Grid),但它们都不支持真正的大数据集。 Nattable 是最接近的一种,但它在使用的数据类型方面仍然存在限制,导致表
以下问题让我感到困惑:我有一个 SWT Java 应用程序,并且在以下情况下从 SWT 3 切换到 SWT 4 会导致 KeyDown 事件出现奇怪的行为: 我有一个包含浏览器小部件的外壳。我正在通过
我在 eclipse SWT 应用程序中有许多表。它们是 Table/TableViewer 表格,由一个标题、左下方一行文本和一系列图形组成。除文本列外还有 32 列,编号从 0 到 31。在经典模
原目标: 我有一个 TreeMenu 用于显示我的菜单。 在这棵树中,用户可以选择不同的项目。 我想禁用树,以便用户在选择第一个项目后无法选择新项目。 问题是,我们不能使用 setEnabled,因为
是否已经有人在 IDEA 中编写了类似于“导航栏”的 SWT 控件?对于那些不知道我的意思的人:它看起来像 path1 > path2 > path3 > 其中每个“路径”都是模拟一个按钮的完整路径的
我需要使用 SWT/JFace 实现多选组合框,最好的方法是什么?我应该更改源代码还是应该尝试扩展组合? 最佳答案 不可能扩展 Combo 可以通过重写 checkSubclass() 来扩展 Com
我想用大量行(最多 3 mio)填充 SWT 虚拟表。当我使用懒惰的内容提供者时,我不能使用过滤器,而当我使用普通的内容提供者时,更改过滤器的性能会变得非常糟糕。有没有办法在 SWT 或 JFace
除了外观之外,组控制和复合之间有什么区别?我可以将容器放入组内吗?如果是,我必须使用什么布局?我尝试将容器放置在组内,但容器内的控件未显示。 Group grp= new Group(containe
我是一名优秀的程序员,十分优秀!