问题
I am implementing a plug-in in eclipse, which reads a set of values from a database and displays it in a TableViewer inside a ViewPart. The TableViewer uses an ArrayContentProvider as shown
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
...
viewer.setContentProvider(new ArrayContentProvider());
I also have a handler which has access to the TableViewer instance for importing data from an XML file. When the importer imports data, I create a ModelProvider instance which generates a list of objects whose data is displayed by TableViewer.
The handler then sets the input:
viewer.setInput(new ModelProvider(Data.getDocs()).getTableRows());
When I test this application, with this ViewPart already open (and the TableViewer being empty) and invoke the handler for importing data, the data is imported succesfully but the TableViewer shows only a single row and the scroll bar. Only when I drag the ViewPart to some other location on the Workbench, then all the rows are shown.
I have even tried:
viewer.setInput(new ModelProvider(Data.getDocs()).getTableRows());
viewer.refresh();
and
viewer.setInput(null);
viewer.setInput(new ModelProvider(Data.getDocs()).getTableRows());
viewer.refresh();
but nothing works. How do I make all the rows display as soon as new input is set?
回答1:
I don't think you have a table problem, but a layout problem.
If your viewer's parent has a GridLayout, then do:
viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
OR you can just set a FillLayout on the parent, and that's it.
(if you think I misunderstood your question, please post a screenshot)
来源:https://stackoverflow.com/questions/32345882/tableviewer-shrinks-to-a-single-row-with-a-scroll-bar-when-new-input-is-set