Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » How do I add a multi-column row to a TableViewer?
How do I add a multi-column row to a TableViewer? [message #1842865] Sat, 03 July 2021 22:27 Go to next message
Steve Vestal is currently offline Steve VestalFriend
Messages: 37
Registered: March 2013
Member
I am trying to get a simple example working to start with, two columns with each row an array of 2 strings. Below is the result of studying a number of examples and the API javadoc. The TableItem consisting of {"display", "<expression>"} exists in the table but does not appear in the view. How is a row added to a TableView?

@Override
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.FULL_SELECTION);
Table viewerTable = viewer.getTable();
viewerTable.setLayoutData(new GridData(GridData.FILL_BOTH));

TableViewerColumn kindCol = new TableViewerColumn(viewer, SWT.LEFT);
kindCol.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
// This is never called
}});
TableViewerColumn textCol = new TableViewerColumn(viewer, SWT.RIGHT);
textCol.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
// This is never called
}});
// viewerTable.getColumnCount() == 2 at this point

String[] rowTextArray = new String[] {"display", "<expression>"};
TableItem tableRow = new TableItem(viewerTable, SWT.NONE);
tableRow.setData(rowTextArray);
// tableRow.getText() is always a single String, maybe a row label?
// viewerTable.getItems().length == 1
// viewerTable.getItem(0).getData() == rowTextArray

// Various attempts of this kind result in NPEs deep in the framework.
// Why does TableViewer#setInput require a parameter? Others don't.
// viewer.setContentProvider(new IStructuredContentProvider() {
// @Override
// public Object[] getElements(Object inputElement) {
// return rowText;
//// return listRowText;
//// return new TableItem[] {tableRow};
//// return viewerTable.getItems();
//// }});
// viewer.setInput(rowText);
// viewer.setInput(allRows);
// viewer.setInput(listRowText);
// viewer.setInput(tableRow);

}
Re: How do I add a multi-column row to a TableViewer? [message #1842868 is a reply to message #1842865] Sun, 04 July 2021 08:33 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
Perhaps looking at the JFace Snippets helps. For your case I'd suggest to start with Snippet003.

Basically: set a label provider, and set a content provider. Then set your model as input: viewer.setInput(rowTextArray);. With an array as model, you'd use ArrayContentProvider.getInstance() as content provider.
Re: How do I add a multi-column row to a TableViewer? [message #1842870 is a reply to message #1842868] Sun, 04 July 2021 15:52 Go to previous message
Steve Vestal is currently offline Steve VestalFriend
Messages: 37
Registered: March 2013
Member
Thanks! That put me on track. I did need to nest rowTextArray,

String[] rowTextArray = new String[] {"display", "<expression>"};
Object[] tableWithOneRow = new Object[] {rowTextArray};
viewer.setInput(tableWithOneRow);

Next step is to associate cell editors with each column. The Javadoc for ArrayContentProvider says it "handles the case where the viewer input is an unchanging array or collection of elements." I'm assuming that means if the structure-of-arrays stay the same then I can replace an element in rowTextArray and just do a refresh.
Previous Topic:Application Android whit Server GlassFish
Next Topic:Cant run Karel the robot or other kinds of programs in Eclipse
Goto Forum:
  


Current Time: Fri Apr 19 22:32:47 GMT 2024

Powered by FUDForum. Page generated in 0.07382 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top