删除 Nattable 中的行号
Remove row numbers in Nattable
我在我的应用程序中将 Nattable 用于几个不同的 table。现在我需要找到一种方法来删除第一列中的行号。有什么方法可以 hide/remove 来自 table 的行号?
我绘制 table 的来源是:
public void generate() {
ConfigRegistry configRegistry = new ConfigRegistry();
ColumnGroupModel columnGroupModel = new ColumnGroupModel();
ColumnGroupModel columnGroupGroup = new ColumnGroupModel();
bodyLayer = new BodyLayerStack<T>(this.expandTree, getItems(), columnPropertyAccessor, configRegistry,
treeformat);
columnLabelAccumulator = new CustomLabelAccumulator(bodyLayer.getBodyDataLayer(),
(ListDataProvider<T>) bodyLayer.getBodyDataProvider());
applyColumnLabels(columnDefinitions, columnLabelAccumulator);
bodyLayer.getBodyDataLayer().setConfigLabelAccumulator(columnLabelAccumulator);
// create the column header layer stack
IDataProvider columnHeaderDataProvider = new NATColumnHeaderDataProvider(columnDefinitions);
DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer,
bodyLayer.getSelectionLayer());
ILayer columnHeaderToUse = null;
if (sortEnabled) {
SortHeaderLayer<T> sortHeaderLayer = new SortHeaderLayer<T>(columnHeaderLayer, new GlazedListsSortModel<T>(
bodyLayer.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer));
columnGroupHeaderLayer = new ColumnGroupHeaderLayer(sortHeaderLayer, bodyLayer.getSelectionLayer(),
columnGroupModel);
columnHeaderToUse = sortHeaderLayer;
} else {
columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, bodyLayer.getSelectionLayer(),
columnGroupModel);
columnHeaderToUse = columnGroupHeaderLayer;
}
columnGroupGroupHeaderLayer = new ColumnGroupGroupHeaderLayer(columnGroupHeaderLayer,
bodyLayer.getSelectionLayer(), columnGroupGroup);
applyColumnWidths(bodyLayer.getBodyDataLayer());
applyColumnHeaderGroups();
// Note: The column header layer is wrapped in a filter row composite.
// This plugs in the filter row functionality
DefaultGlazedListsStaticFilterStrategy<T> filterStrategy = new DefaultGlazedListsStaticFilterStrategy<T>(
bodyLayer.getFilterList(), columnPropertyAccessor, configRegistry);
for (int i = 0; i < staticFilters.size(); i++) {
filterStrategy.addStaticFilter(staticFilters.get(i));
}
if (showHeaderGroupRow) {
columnHeaderToUse = columnGroupHeaderLayer;
if (showHeaderGroupGroupsRow)
columnHeaderToUse = columnGroupGroupHeaderLayer;
}
FilterRowHeaderComposite<T> filterRowHeaderLayer = new FilterRowHeaderComposite<T>(filterStrategy,
columnHeaderToUse, columnHeaderDataLayer.getDataProvider(), configRegistry);
ILayer finalHeaderRow = columnHeaderToUse;
if (showFilterRow) {
finalHeaderRow = filterRowHeaderLayer;
}
// build the row header layer
DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(
bodyLayer.getBodyDataProvider());
DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider);
rowHeaderDataLayer.setDefaultColumnWidth(40);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
// build the corner layer
ILayer cornerLayer = new CornerLayer(
new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
rowHeaderLayer, finalHeaderRow);
// build the grid layer
compositeGridLayer = new GridLayer(bodyLayer, finalHeaderRow, rowHeaderLayer, cornerLayer);
if (editorConfiguration != null) {
compositeGridLayer.addConfiguration(editorConfiguration);
}
natTable = new NatTable(parent, compositeGridLayer, false);
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
compositeGridLayer.registerCommandHandler(new LandscapePrintHandler(compositeGridLayer));
addStyling();
natTable.addLayerListener(new ILayerListener() {
@Override
public void handleLayerEvent(ILayerEvent event) {
int rowNumber = -1;
int columnNumber = -1;
if (event instanceof CellVisualChangeEvent && !(event instanceof CellSelectionEvent)) {
CellVisualChangeEvent visualChangeEvent = (CellVisualChangeEvent) event;
rowNumber = visualChangeEvent.getRowPosition();
columnNumber = visualChangeEvent.getColumnPosition();
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, selectedRow,
bodyLayer.getBodyDataLayer());
T newObject = dataProvider.getRowObject(transformedRow);
notifyModificationListeners(newObject, getSelectedItem());
}
if (event instanceof CellSelectionEvent) {
CellSelectionEvent cellEvent = (CellSelectionEvent) event;
rowNumber = cellEvent.getRowPosition();
columnNumber = cellEvent.getColumnPosition();
launchGridSelectionUpdated();
}
if (rowNumber > -1) {
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, rowNumber,
bodyLayer.getBodyDataLayer());
T ob = dataProvider.getRowObject(transformedRow);
notifyItemSelectionlisteners(ob);
}
if (columnNumber > -1) {
int transformedColumn = LayerUtil.convertColumnPosition(compositeGridLayer, columnNumber,
bodyLayer.getBodyDataLayer());
selectedColumnPlaceholder.clear();
selectedColumnPlaceholder.add(columnDefinitions.get(transformedColumn));
}
if (rowNumber != -1 && columnNumber != -1) {
// First start of the selection process
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, rowNumber,
bodyLayer.getBodyDataLayer());
setSelectedItem((T) Utils.cloneObject(dataProvider.getRowObject(transformedRow)));
selectedRow = rowNumber;
}
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.configure();
// natTable.setTheme(new ModernNatTableThemeConfiguration());
ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new CustomGroupByThemeExtension());
natTable.setTheme(modernTheme);
freezeColumns();
registerCopyHandlers(getSelectionLayer(), columnHeaderDataLayer);
natTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if (e.button == MouseEventMatcher.RIGHT_BUTTON) {
int column = compositeGridLayer.getColumnPositionByX(e.x);
int row = bodyLayer.getBodyDataLayer().getRowPositionByY(e.y);
row = compositeGridLayer.getRowPositionByY(e.y);
ListDataProvider<T> dataProvider = bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, row,
bodyLayer.getBodyDataLayer());
if (transformedRow != -1) {
T item = dataProvider.getRowObject(transformedRow);
notifyRightClickListeners(e.x, e.y, item, getColumnDefinition(column));
}
}
}
});
attachToolTip();
}
如果您不想要 header 行中的行号,请不要使用 GridLayer
。您还可以使用 CompositeLayer
并且只有一个列 header 和一个 body。
NatTable 示例应用程序中有几个示例可以说明这一点。
我在我的应用程序中将 Nattable 用于几个不同的 table。现在我需要找到一种方法来删除第一列中的行号。有什么方法可以 hide/remove 来自 table 的行号?
我绘制 table 的来源是:
public void generate() {
ConfigRegistry configRegistry = new ConfigRegistry();
ColumnGroupModel columnGroupModel = new ColumnGroupModel();
ColumnGroupModel columnGroupGroup = new ColumnGroupModel();
bodyLayer = new BodyLayerStack<T>(this.expandTree, getItems(), columnPropertyAccessor, configRegistry,
treeformat);
columnLabelAccumulator = new CustomLabelAccumulator(bodyLayer.getBodyDataLayer(),
(ListDataProvider<T>) bodyLayer.getBodyDataProvider());
applyColumnLabels(columnDefinitions, columnLabelAccumulator);
bodyLayer.getBodyDataLayer().setConfigLabelAccumulator(columnLabelAccumulator);
// create the column header layer stack
IDataProvider columnHeaderDataProvider = new NATColumnHeaderDataProvider(columnDefinitions);
DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer,
bodyLayer.getSelectionLayer());
ILayer columnHeaderToUse = null;
if (sortEnabled) {
SortHeaderLayer<T> sortHeaderLayer = new SortHeaderLayer<T>(columnHeaderLayer, new GlazedListsSortModel<T>(
bodyLayer.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer));
columnGroupHeaderLayer = new ColumnGroupHeaderLayer(sortHeaderLayer, bodyLayer.getSelectionLayer(),
columnGroupModel);
columnHeaderToUse = sortHeaderLayer;
} else {
columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, bodyLayer.getSelectionLayer(),
columnGroupModel);
columnHeaderToUse = columnGroupHeaderLayer;
}
columnGroupGroupHeaderLayer = new ColumnGroupGroupHeaderLayer(columnGroupHeaderLayer,
bodyLayer.getSelectionLayer(), columnGroupGroup);
applyColumnWidths(bodyLayer.getBodyDataLayer());
applyColumnHeaderGroups();
// Note: The column header layer is wrapped in a filter row composite.
// This plugs in the filter row functionality
DefaultGlazedListsStaticFilterStrategy<T> filterStrategy = new DefaultGlazedListsStaticFilterStrategy<T>(
bodyLayer.getFilterList(), columnPropertyAccessor, configRegistry);
for (int i = 0; i < staticFilters.size(); i++) {
filterStrategy.addStaticFilter(staticFilters.get(i));
}
if (showHeaderGroupRow) {
columnHeaderToUse = columnGroupHeaderLayer;
if (showHeaderGroupGroupsRow)
columnHeaderToUse = columnGroupGroupHeaderLayer;
}
FilterRowHeaderComposite<T> filterRowHeaderLayer = new FilterRowHeaderComposite<T>(filterStrategy,
columnHeaderToUse, columnHeaderDataLayer.getDataProvider(), configRegistry);
ILayer finalHeaderRow = columnHeaderToUse;
if (showFilterRow) {
finalHeaderRow = filterRowHeaderLayer;
}
// build the row header layer
DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(
bodyLayer.getBodyDataProvider());
DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider);
rowHeaderDataLayer.setDefaultColumnWidth(40);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
// build the corner layer
ILayer cornerLayer = new CornerLayer(
new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
rowHeaderLayer, finalHeaderRow);
// build the grid layer
compositeGridLayer = new GridLayer(bodyLayer, finalHeaderRow, rowHeaderLayer, cornerLayer);
if (editorConfiguration != null) {
compositeGridLayer.addConfiguration(editorConfiguration);
}
natTable = new NatTable(parent, compositeGridLayer, false);
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
compositeGridLayer.registerCommandHandler(new LandscapePrintHandler(compositeGridLayer));
addStyling();
natTable.addLayerListener(new ILayerListener() {
@Override
public void handleLayerEvent(ILayerEvent event) {
int rowNumber = -1;
int columnNumber = -1;
if (event instanceof CellVisualChangeEvent && !(event instanceof CellSelectionEvent)) {
CellVisualChangeEvent visualChangeEvent = (CellVisualChangeEvent) event;
rowNumber = visualChangeEvent.getRowPosition();
columnNumber = visualChangeEvent.getColumnPosition();
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, selectedRow,
bodyLayer.getBodyDataLayer());
T newObject = dataProvider.getRowObject(transformedRow);
notifyModificationListeners(newObject, getSelectedItem());
}
if (event instanceof CellSelectionEvent) {
CellSelectionEvent cellEvent = (CellSelectionEvent) event;
rowNumber = cellEvent.getRowPosition();
columnNumber = cellEvent.getColumnPosition();
launchGridSelectionUpdated();
}
if (rowNumber > -1) {
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, rowNumber,
bodyLayer.getBodyDataLayer());
T ob = dataProvider.getRowObject(transformedRow);
notifyItemSelectionlisteners(ob);
}
if (columnNumber > -1) {
int transformedColumn = LayerUtil.convertColumnPosition(compositeGridLayer, columnNumber,
bodyLayer.getBodyDataLayer());
selectedColumnPlaceholder.clear();
selectedColumnPlaceholder.add(columnDefinitions.get(transformedColumn));
}
if (rowNumber != -1 && columnNumber != -1) {
// First start of the selection process
ListDataProvider<T> dataProvider = (ListDataProvider<T>) bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, rowNumber,
bodyLayer.getBodyDataLayer());
setSelectedItem((T) Utils.cloneObject(dataProvider.getRowObject(transformedRow)));
selectedRow = rowNumber;
}
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.configure();
// natTable.setTheme(new ModernNatTableThemeConfiguration());
ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new CustomGroupByThemeExtension());
natTable.setTheme(modernTheme);
freezeColumns();
registerCopyHandlers(getSelectionLayer(), columnHeaderDataLayer);
natTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if (e.button == MouseEventMatcher.RIGHT_BUTTON) {
int column = compositeGridLayer.getColumnPositionByX(e.x);
int row = bodyLayer.getBodyDataLayer().getRowPositionByY(e.y);
row = compositeGridLayer.getRowPositionByY(e.y);
ListDataProvider<T> dataProvider = bodyLayer.getBodyDataProvider();
int transformedRow = LayerUtil.convertRowPosition(compositeGridLayer, row,
bodyLayer.getBodyDataLayer());
if (transformedRow != -1) {
T item = dataProvider.getRowObject(transformedRow);
notifyRightClickListeners(e.x, e.y, item, getColumnDefinition(column));
}
}
}
});
attachToolTip();
}
如果您不想要 header 行中的行号,请不要使用 GridLayer
。您还可以使用 CompositeLayer
并且只有一个列 header 和一个 body。
NatTable 示例应用程序中有几个示例可以说明这一点。