Java 侦听器未听到NatTable单元格的单击
以下是SSCCE: 创建1x1 NatTable,并将标签按钮应用于单个 内含细胞 添加一个ButtonCellPainter,使单元格看起来像一个按钮 尝试添加一个不起作用的单击侦听器,否则我会看到您在stderr中 我感到困惑的是混淆的开始和结束之间的注释行-剩下的是建立示例的东西。我试着按照提供的“将单元格渲染为链接”和“按钮”示例进行操作,但显然有些地方我弄错了 NatTable的性能和美学真的令人惊叹;很高兴有人在这里指出我的错误,这样我就可以将其添加到我的项目中:-Java 侦听器未听到NatTable单元格的单击,java,eclipse,nattable,Java,Eclipse,Nattable,以下是SSCCE: 创建1x1 NatTable,并将标签按钮应用于单个 内含细胞 添加一个ButtonCellPainter,使单元格看起来像一个按钮 尝试添加一个不起作用的单击侦听器,否则我会看到您在stderr中 我感到困惑的是混淆的开始和结束之间的注释行-剩下的是建立示例的东西。我试着按照提供的“将单元格渲染为链接”和“按钮”示例进行操作,但显然有些地方我弄错了 NatTable的性能和美学真的令人惊叹;很高兴有人在这里指出我的错误,这样我就可以将其添加到我的项目中:- import o
import org.eclipse.nebula.widgets.nattable.*;
import org.eclipse.nebula.widgets.nattable.config.*;
import org.eclipse.nebula.widgets.nattable.data.*;
import org.eclipse.nebula.widgets.nattable.grid.layer.*;
import org.eclipse.nebula.widgets.nattable.hideshow.*;
import org.eclipse.nebula.widgets.nattable.layer.*;
import org.eclipse.nebula.widgets.nattable.layer.cell.*;
import org.eclipse.nebula.widgets.nattable.painter.cell.*;
import org.eclipse.nebula.widgets.nattable.selection.*;
import org.eclipse.nebula.widgets.nattable.ui.action.*;
import org.eclipse.nebula.widgets.nattable.ui.binding.*;
import org.eclipse.nebula.widgets.nattable.ui.matcher.*;
import org.eclipse.nebula.widgets.nattable.viewport.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class NatTableTest extends Dialog implements IDataProvider, IConfigLabelAccumulator {
@Override protected Control createDialogArea(Composite parent) {
Composite toReturn = (Composite) super.createDialogArea(parent);
toReturn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
IDataProvider dataProvider = this;
DataLayer bodyDataLayer = new DataLayer(dataProvider);
bodyDataLayer.setConfigLabelAccumulator(this);
RowHideShowLayer rowHideShowLayer = new RowHideShowLayer(bodyDataLayer);
SelectionLayer selectionLayer = new SelectionLayer(rowHideShowLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
DataLayer rowDataLayer = new DefaultRowHeaderDataLayer(dataProvider);
ILayer rowLayer = new RowHeaderLayer(rowDataLayer, viewportLayer, selectionLayer);
DataLayer headerDataLayer = new DefaultColumnHeaderDataLayer(dataProvider);
ILayer headerLayer = new ColumnHeaderLayer(headerDataLayer, viewportLayer, selectionLayer);
DataLayer cornerDataLayer = new DataLayer(dataProvider);
ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowLayer, headerLayer);
GridLayer gridLayer = new GridLayer(viewportLayer, headerLayer, rowLayer, cornerLayer);
NatTable nattable = new NatTable(toReturn, gridLayer, false);
nattable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
nattable.addConfiguration(new DefaultNatTableStyleConfiguration());
// ================================================== START OF CONFUSION
ButtonCellPainter buttonPainter = new ButtonCellPainter(new TextPainter());
buttonPainter.addClickListener(new IMouseAction(){
@Override public void run(NatTable table, MouseEvent event) {
System.err.println("YOU ARE HERE");
}
});
nattable.addConfiguration(new AbstractUiBindingConfiguration(){
@Override public void configureUiBindings(UiBindingRegistry registry) {
CellLabelMouseEventMatcher mouseEventMatcher = new CellLabelMouseEventMatcher("BODY",1,"BUTTON");
registry.registerMouseDownBinding(mouseEventMatcher, buttonPainter);
}
});
IConfigRegistry configRegistry = new ConfigRegistry();
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, buttonPainter, "NORMAL","BUTTON");
nattable.setConfigRegistry(configRegistry);
// ================================================== END OF CONFUSION
nattable.configure();
return toReturn;
}
public NatTableTest(Shell parentShell) {super(parentShell);}
// IDataProvider methods
@Override public int getRowCount() {return 1;}
@Override public int getColumnCount() {return 1;}
@Override public Object getDataValue(int var1, int var2) {return "X";}
@Override public void setDataValue(int var1, int var2, Object var3) {}
// IConfigLabelAccumulator methods
@Override public void accumulateConfigLabels(LabelStack labels, int col, int row) {labels.addLabel("BUTTON");}
}
编辑:在调试过程中,在下表的UiBindingRegistry RegionLabel中似乎只包含正文:
。。。按钮是否也应在此处返回,因为标签已添加到该单元格中,我可以告诉您该区域已正确添加,因为该单元格已渲染为按钮
NatTable is 2.0.0.201910310701问题在于单击操作是独占的。所以第一场比赛就赢了。在本例中,将触发SelectCellAction,该操作由DefaultSelectionBindings注册。而且,由于选择被触发,因此将跳过按钮单击 在代码中,只需使用registerFirstMouseDownBinding即可解决此问题 这样,您的绑定将在注册表顶部注册,因此首先进行检查。由于我们的检查具有更多条件区域标签、状态掩码、鼠标按钮和单元格标签,因此仅在身体区域中带有按钮标签的单元格上触发。然后无法选择按钮单元格,这是正确的IMHO。但您仍然可以选择其他没有按钮单元格标签的单元格
关于第二个问题:否,按钮标签不应是区域标签的一部分。这是一个单元格标签,不是区域标签。谢谢您,先生!registerMouseDownBinding->registerFirstMouseDownBinding是所需要的一切:-再次强调:这个插件有多好-数千行,各种小部件嵌入在单元格中&它的性能就像一个梦:-供将来参考:我使用registerFirstSingleClickBinding,因为我在单击单元格时弹出了一个对话框,registerFirstMouseDownBinding导致“鼠标向上”丢失。。。
private IMouseAction getMouseEventAction(MouseEventTypeEnum mouseEventType, MouseEvent event) {
try {
LinkedList<MouseBinding> mouseEventBindings = (LinkedList) this.mouseBindingsMap.get(mouseEventType);
if (mouseEventBindings != null) {
LabelStack regionLabels = this.natTable.getRegionLabelsByXY(event.x, event.y);
Iterator var6 = mouseEventBindings.iterator();
while (var6.hasNext()) {
MouseBinding mouseBinding = (MouseBinding) var6.next();
if (mouseBinding.getMouseEventMatcher().matches(this.natTable, event, regionLabels)) {
return mouseBinding.getAction();
}
}
}
} catch (Exception var7) {
log.error("Exception on retrieving a mouse event action", var7);
}
return null;
}
nattable.addConfiguration(new AbstractUiBindingConfiguration(){
@Override public void configureUiBindings(UiBindingRegistry registry) {
CellLabelMouseEventMatcher mouseEventMatcher = new CellLabelMouseEventMatcher("BODY",1,"BUTTON");
registry.registerFirstMouseDownBinding(mouseEventMatcher, buttonPainter);
}
});