Place an image in a JFace table. The cell causes the image to break in the first column.

So, I have a problem when I add an image to any column of the JFace table, the first column behaves the same as it has an image, and the text is indented by the size of this image.

Here is a screenshot illustrating my point with the code needed to create it. In any case, to stop this, because it really gets on my wick?

Hi,

Glen x

enter image description here

package widgets; import java.util.ArrayList; import java.util.List; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.StyledCellLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.jface.viewers.ViewerCell; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ComponentTest { private static Image image; public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, true)); TableViewer viewer1 = getViewer(shell, true); TableViewer viewer2 = getViewer(shell, false); List<String> rows = new ArrayList<String>(); rows.add("Row 1"); rows.add("Row 2"); viewer1.setInput(rows); viewer2.setInput(rows); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } private static TableViewer getViewer(final Shell shell, boolean addImage) { TableViewer viewer = new TableViewer(shell, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NONE); viewer.setContentProvider(ArrayContentProvider.getInstance()); viewer.getTable().setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true)); TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE); col.getColumn().setWidth(100); col.getColumn().setText("Text Column"); col.setLabelProvider(new StyledCellLabelProvider() { @Override public void update(ViewerCell cell) { cell.setText((String) cell.getElement()); } }); col = new TableViewerColumn(viewer, SWT.NONE); col.getColumn().setWidth(100); col.getColumn().setText("Second Text Column"); col.setLabelProvider(new StyledCellLabelProvider() { @Override public void update(ViewerCell cell) { cell.setText((String) cell.getElement()); } }); if (addImage) { col = new TableViewerColumn(viewer, SWT.NONE); col.getColumn().setWidth(100); col.getColumn().setText("Image Column"); col.setLabelProvider(new StyledCellLabelProvider() { @Override public void update(ViewerCell cell) { cell.setImage(getImage(shell.getDisplay())); } }); } viewer.getTable().setHeaderVisible(true); return viewer; } // make a little green square private static Image getImage(Display display) { if (image == null) { PaletteData palette = new PaletteData(0xFF, 0xFF00, 0xFF0000); ImageData imageData = new ImageData(16, 16, 24, palette); for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { imageData.setPixel(x, y, 0xFF00); } } ; image = new Image(display, imageData); } return image; } } 
+6
source share
3 answers

This is a rather annoying bug when using Windows. You can use a dirty fix by skipping the first column (without using it) and setting its width to zero.

As far as I remember correctly, this will lead to small crashes when using MacOS.

+2
source

I had the same problem and worked on it using StyledCellLabelProvider with owner drawing and overriding the drawing method to paint the image. The fact is that you should not set the image of the cell of the viewer, because this will give an error. I sent the sample code to the Eclipse error report.

+2
source

Table TableItem: 301: here I see a problem with the SWT code.

 if (code == 0) return new RECT (); if (!getImage) { RECT iconRect = new RECT (); iconRect.left = OS.LVIR_ICON; parent.ignoreCustomDraw = true; code = OS.SendMessage (hwnd, OS. LVM_GETITEMRECT, row, iconRect); parent.ignoreCustomDraw = false; if (code != 0) rect.left = iconRect.right; //****problem code = OS.SendMessage (hwnd, OS. LVM_GETITEMRECT, row, iconRect); 

for the first viewer of the table with the image, here is code 1, why the drawing of the text began with iconRect right coordinate. for the second viwer table without image, the code is zero. therefore, it always starts with actual boundaries.

If you really want to fix it in CellStyleStyledCellLabelProvider , I would suggest you override the drawing method.

+1
source

Source: https://habr.com/ru/post/926515/


All Articles