You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NatTable中一级节点无法展开问题求助

NatTable一级节点展开后无法即时显示的问题

我正在开发一个包含2列和带复选框行头的NatTable组件,遇到以下问题:

  • 根节点可以正常展开/折叠
  • 点击一级节点展开时,仅能看到滚动条变化,但节点内容不显示;切换到其他应用再返回当前窗口,节点才会显示展开状态
  • 折叠功能在任何场景下都正常工作
  • 调试确认两种场景下TreeExpandCollapseAction均已触发,且调试过程中展开功能恢复正常

以下是相关的代码片段:

public class AdvancedPackageManagementNattable {

  public static final String COLUMN_COMPONENT_NAME = "componentTreeRoot";
  public static final String COLUMN_VERSION = "version";


  ICellPainter checkBoxPainter;


  public Control createAdvancePackageTable(final Composite parent) {
    Composite composite = new Composite(parent, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, true).span(9, 9).align(SWT.FILL, SWT.FILL).applyTo(composite);
    GridLayoutFactory.fillDefaults().applyTo(composite);

    ConfigRegistry configRegistry = new ConfigRegistry();
    configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, DefaultComparator.getInstance());

    EventList<ComponentTreeItem> eventList = GlazedLists.eventList(datums.values());
    SortedList<ComponentTreeItem> sortedList = new SortedList<>(eventList, null);


    String[] propertyNames = new String[] {
        AdvancedPackageManagementNattable.COLUMN_COMPONENT_NAME,
        AdvancedPackageManagementNattable.COLUMN_VERSION };
    IColumnPropertyAccessor<ComponentTreeItem> columnPropertyAccessor =
        new ReflectiveColumnPropertyAccessor<>(propertyNames);

    Map<String, String> propertyNameToLabel = new HashMap<>();
    propertyNameToLabel.put(AdvancedPackageManagementNattable.COLUMN_COMPONENT_NAME, "Component(Package) Name");
    propertyNameToLabel.put(AdvancedPackageManagementNattable.COLUMN_VERSION, "Version Number");

    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyNameToLabel);
    DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);


    // Body layer
    ISortModel sortModel =
        new GlazedListsSortModel<>(sortedList, columnPropertyAccessor, configRegistry, columnHeaderDataLayer);

    final TreeList<ComponentTreeItem> treeList =
        new TreeList<>(sortedList, new ComponentTreeFormat(sortModel), TreeList.nodesStartCollapsed());
    GlazedListTreeData<?> treeData = new GlazedListTreeData<>(treeList);

    ListDataProvider<?> bodyDataProvider =
        new ListDataProvider<>(treeList, new TreeReflectiveColumnPropertyAccessor(columnPropertyAccessor));
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);


    ColumnReorderLayer columnReorderLayer = new ColumnReorderLayer(bodyDataLayer);
    ColumnHideShowLayer columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
    SelectionLayer selectionLayer = new SelectionLayer(columnHideShowLayer);

    final TreeLayer treeLayer = new TreeLayer(selectionLayer, new GlazedListTreeRowModel<>(treeData));

    ViewportLayer viewportLayer = new ViewportLayer(treeLayer);

    ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);

    ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderDataLayer);
    columnHeaderDataLayer.setConfigLabelAccumulator(labelAccumulator);
    SortHeaderLayer<?> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, sortModel, false);

    // row header
    RowHeaderDataProvider rowHeaderDataProvider = new RowHeaderDataProvider(bodyDataProvider, bodyDataLayer);
    DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider);
    RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);

    // Corner layer
    CornerLayer cornerLayer =
        createCornerLayer(columnHeaderDataProvider, sortHeaderLayer, rowHeaderDataProvider, rowHeaderLayer);

    // Grid
    GridLayer gridLayer = new GridLayer(viewportLayer, sortHeaderLayer, rowHeaderLayer, cornerLayer);
    NatTable natTable = new NatTable(composite, gridLayer, false);
    bodyDataLayer.setColumnPercentageSizing(true);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);


    natTable.setConfigRegistry(configRegistry);
    addConfigurations(natTable, bodyDataLayer);
    columnHeaderDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());


    final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
    bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
    registerColumnLabels(columnLabelAccumulator);

    natTable.configure();

    addStyleThemeConfiguration(natTable);
    return natTable;
  }

  private CornerLayer createCornerLayer(final IDataProvider columnHeaderDataProvider,
      final SortHeaderLayer<?> sortHeaderLayer, final RowHeaderDataProvider rowHeaderDataProvider,
      final RowHeaderLayer rowHeaderLayer) {
    DefaultCornerDataProvider cornerDataProvider =
        new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    return new CornerLayer(cornerDataLayer, rowHeaderLayer, sortHeaderLayer);
  }


  private void addStyleThemeConfiguration(final NatTable natTable) {
    natTable.setTheme(new ModernNatTableThemeConfiguration());
    natTable.configure();
  }


  private void addConfigurations(final NatTable natTable, final DataLayer bodyDataLayer) {
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
    natTable.addConfiguration(new SingleClickSortConfiguration());
    natTable.addConfiguration(new CommonNattableConfiguration(bodyDataLayer));
    natTable.addConfiguration(new DefaultEditBindings());
  }


  private void registerColumnLabels(final ColumnOverrideLabelAccumulator columnLabelAccumulator) {
    columnLabelAccumulator.registerColumnOverrides(0, COLUMN_COMPONENT_NAME);
    columnLabelAccumulator.registerColumnOverrides(1, COLUMN_VERSION);
  }


}

public class CommonNattableConfiguration extends DefaultNatTableStyleConfiguration {

  private static final String CONFIG_LABEL_VERSION = "version";

  private final DataLayer dataLayer;

  public CommonNattableConfiguration(final DataLayer bodyDataLayer) {
    this.dataLayer = bodyDataLayer;
  }

  @Override
  public void configureRegistry(final IConfigRegistry configRegistry) {
    super.configureRegistry(configRegistry);
    addVersionComboConfig(configRegistry);
    addCheckBoxConfig(configRegistry);
    nattableStyleConfig(configRegistry);

  }

  private void nattableStyleConfig(final IConfigRegistry configRegistry) {
    Style style = new Style();
    style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_WIDGET_LIGHT_SHADOW);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL,
        GridRegion.CORNER);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL,
        GridRegion.COLUMN_HEADER);
  }

  private void addVersionComboConfig(final IConfigRegistry configRegistry) {
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE,
        DisplayMode.EDIT, CommonNattableConfiguration.CONFIG_LABEL_VERSION);
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new VersionComboBoxEditor(this.dataLayer),
        DisplayMode.EDIT, CommonNattableConfiguration.CONFIG_LABEL_VERSION);
    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new VersionDisplayConverter(),
        DisplayMode.NORMAL, CommonNattableConfiguration.CONFIG_LABEL_VERSION);

    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new ComponentNameDisplayConverter(),
        DisplayMode.NORMAL, AdvancedPackageManagementNattable.COLUMN_COMPONENT_NAME);


  }

  private void addCheckBoxConfig(final IConfigRegistry configRegistry) {
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.EDIT,
        GridRegion.ROW_HEADER);

    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE,
        DisplayMode.EDIT, GridRegion.ROW_HEADER);

    Style style = new Style();
    style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);

    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new RowHeaderCheckboxPainter(),
        DisplayMode.NORMAL, GridRegion.ROW_HEADER);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new RowHeaderCheckboxPainter(),
        DisplayMode.SELECT, GridRegion.ROW_HEADER);

    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(),
        DisplayMode.NORMAL, GridRegion.ROW_HEADER);

    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(),
        DisplayMode.EDIT, GridRegion.ROW_HEADER);

  }


  @Override
  public void configureUiBindings(final UiBindingRegistry uiBindingRegistry) {
    uiBindingRegistry.registerSingleClickBinding(
        new MouseEventMatcher(SWT.NONE, GridRegion.ROW_HEADER, MouseEventMatcher.LEFT_BUTTON), new MouseEditAction() {

          /**
           * {@inheritDoc}
           */
          @Override
          public void run(final NatTable natTable, final MouseEvent event) {
            super.run(natTable, event);
            natTable.refresh(false);
          }
        });

  }

}

恳请各位提供解决思路或方案。


内容的提问来源于stack exchange,提问作者Paul-E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:45:55