如何在 SWT Table 上绘制选择框?
How to draw selection box on SWT Table?
我一直在谷歌上搜索可能的解决方案,但没有找到类似的解决方案。所以,我想问问是否可以。
我想在 SWT Table
小部件中使用鼠标拖动时 select 多个项目。就像在 native-OS 中一样。
我已将 Table
设置为 SWT.MULTI
,但这仅适用于键盘。
我很好奇,既然 SWT 使用 native-OS 调用来绘制它的 GUI,为什么这个功能没有在 Table
小部件中立即实现?
这已经在 Angry IP Scanner 实用软件中实现,如附图所示。
如有任何帮助,我将不胜感激。
我认为您可以使用 Tracker 小部件来实现类似的行为。这是一个简单的 POC 来展示可以做什么。
public class TableShellExample {
Display d;
Shell s;
TableShellExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
s.setLayout(new FillLayout());
Table table = new Table(s, SWT.MULTI | SWT.FULL_SELECTION);
TableColumn tc1 = new TableColumn(table, SWT.CENTER);
TableColumn tc2 = new TableColumn(table, SWT.CENTER);
TableColumn tc3 = new TableColumn(table, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(70);
tc2.setWidth(70);
tc3.setWidth(80);
table.setHeaderVisible(true);
TableItem item1 = new TableItem(table, SWT.NONE);
item1.setText(new String[] { "Tim", "Hatton", "Kentucky" });
TableItem item2 = new TableItem(table, SWT.NONE);
item2.setText(new String[] { "Caitlyn", "Warner", "Ohio" });
TableItem item3 = new TableItem(table, SWT.NONE);
item3.setText(new String[] { "Reese", "Miller", "Ohio" });
TableItem item4 = new TableItem(table, SWT.NONE);
item4.setText(new String[] { "Rey", "Miller", "Texas" });
TableItem item5 = new TableItem(table, SWT.NONE);
item5.setText(new String[] { "Json", "Miller", "Alabama" });
if((table.getStyle() & SWT.MULTI ) != 0 && (table.getStyle() & SWT.FULL_SELECTION ) != 0)
table.addListener(SWT.DragDetect, e -> {
TableItem item = table.getItem(new Point(e.x, e.y));
int startIndex = -1;
int endIndex = -1;
if (item != null) {
startIndex = table.indexOf(item);
Tracker tracker = new Tracker(table.getParent(), SWT.RESIZE);
tracker.setStippled(true);
tracker.setRectangles(new Rectangle[] { new Rectangle(e.x,e.y,1,1) });
if (tracker.open()) {
TableItem finishedItem = table.getItem(table.toControl(Display.getDefault().getCursorLocation()));
if (finishedItem != null) {
endIndex = table.indexOf(finishedItem);
}
}
tracker.dispose();
table.deselectAll();
if (startIndex != -1 && endIndex != -1) {
table.setSelection(Math.min(startIndex, endIndex), Math.max(startIndex, endIndex));
}
}
});
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new TableShellExample();
}
}
我想分享这个以防其他人觉得它有用。
这个功能已经实现了(无需像我想的那样添加任何额外的 "fancy" 代码)只需将 SWT.Selection
侦听器添加到您的 Table
就可以了!
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// to-do...
}
}
我一直在谷歌上搜索可能的解决方案,但没有找到类似的解决方案。所以,我想问问是否可以。
我想在 SWT Table
小部件中使用鼠标拖动时 select 多个项目。就像在 native-OS 中一样。
我已将 Table
设置为 SWT.MULTI
,但这仅适用于键盘。
我很好奇,既然 SWT 使用 native-OS 调用来绘制它的 GUI,为什么这个功能没有在 Table
小部件中立即实现?
这已经在 Angry IP Scanner 实用软件中实现,如附图所示。
如有任何帮助,我将不胜感激。
我认为您可以使用 Tracker 小部件来实现类似的行为。这是一个简单的 POC 来展示可以做什么。
public class TableShellExample {
Display d;
Shell s;
TableShellExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
s.setLayout(new FillLayout());
Table table = new Table(s, SWT.MULTI | SWT.FULL_SELECTION);
TableColumn tc1 = new TableColumn(table, SWT.CENTER);
TableColumn tc2 = new TableColumn(table, SWT.CENTER);
TableColumn tc3 = new TableColumn(table, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(70);
tc2.setWidth(70);
tc3.setWidth(80);
table.setHeaderVisible(true);
TableItem item1 = new TableItem(table, SWT.NONE);
item1.setText(new String[] { "Tim", "Hatton", "Kentucky" });
TableItem item2 = new TableItem(table, SWT.NONE);
item2.setText(new String[] { "Caitlyn", "Warner", "Ohio" });
TableItem item3 = new TableItem(table, SWT.NONE);
item3.setText(new String[] { "Reese", "Miller", "Ohio" });
TableItem item4 = new TableItem(table, SWT.NONE);
item4.setText(new String[] { "Rey", "Miller", "Texas" });
TableItem item5 = new TableItem(table, SWT.NONE);
item5.setText(new String[] { "Json", "Miller", "Alabama" });
if((table.getStyle() & SWT.MULTI ) != 0 && (table.getStyle() & SWT.FULL_SELECTION ) != 0)
table.addListener(SWT.DragDetect, e -> {
TableItem item = table.getItem(new Point(e.x, e.y));
int startIndex = -1;
int endIndex = -1;
if (item != null) {
startIndex = table.indexOf(item);
Tracker tracker = new Tracker(table.getParent(), SWT.RESIZE);
tracker.setStippled(true);
tracker.setRectangles(new Rectangle[] { new Rectangle(e.x,e.y,1,1) });
if (tracker.open()) {
TableItem finishedItem = table.getItem(table.toControl(Display.getDefault().getCursorLocation()));
if (finishedItem != null) {
endIndex = table.indexOf(finishedItem);
}
}
tracker.dispose();
table.deselectAll();
if (startIndex != -1 && endIndex != -1) {
table.setSelection(Math.min(startIndex, endIndex), Math.max(startIndex, endIndex));
}
}
});
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new TableShellExample();
}
}
我想分享这个以防其他人觉得它有用。
这个功能已经实现了(无需像我想的那样添加任何额外的 "fancy" 代码)只需将 SWT.Selection
侦听器添加到您的 Table
就可以了!
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// to-do...
}
}