Putting an image in to a JFace table Cell is causing gap for image to appear in first column

泄露秘密 提交于 2019-12-17 19:47:26

问题


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

Here's a screenshot illustrating my point with the code needed to produce it. Is there anyway to stop this from happening because it's really getting on my wick?

Regards,

Glen x

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;
    }
}

回答1:


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

As far as I remember correctly, this will introduce some minor glitches when using MacOS.




回答2:


I had the same problem and worked around it by using a StyledCellLabelProvider with owner draw and overriding the paint method to paint the image. The point is that you should not set the image of the viewer cell because this will give the bug. I posted example code to the Eclipse bug report.




回答3:


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

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 table viewer with image, here code is 1 that why drawing text started iconRect right coordinate. for the second table viwer with no image, code is zero. so it always starts from the actual bounds.

If you are really keen on fix it at CellStyleStyledCellLabelProvider i would suggest you to override paint method there.



来源:https://stackoverflow.com/questions/12641354/putting-an-image-in-to-a-jface-table-cell-is-causing-gap-for-image-to-appear-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!