How to add image on the right side of the table column and table cell using SWT in java

梦想的初衷 提交于 2019-12-25 09:17:16

问题


How do I add image to the right side of the table column retaining the Text to the left. below is the code

TableColumn tc = new TableColumn(this.table_for_list, SWT.LEFT);
tc.setText("List");

for (int i = 0, n = this.table_for_list.getColumnCount(); i < n; i++) {
    this.table_for_list.getColumn(i).pack();
}        

TableItem item = new TableItem(this.table_for_list, SWT.NONE);
item.setText(0, "List 1");

TableItem item2 = new TableItem(this.table_for_list, SWT.NONE);
item2.setText(0, "List 2");

Table Column Header has "List" on the left side, now I want to add images on the other end (Right side end of the table). I should be able to add in each row images on the right side of the table cell.


回答1:


I slightly modifier this snippet to get it to do what you asked for:

public static void main(String[] args)
{
    Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
    Shell shell = new Shell(display);
    shell.setText("Images on the right side of the TableItem");
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    int columnCount = 3;
    for (int i = 0; i < columnCount; i++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 8;
    for (int i = 0; i < itemCount; i++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    Listener paintListener = event ->
    {
        switch (event.type)
        {
            case SWT.MeasureItem:
            {
                Rectangle rect1 = image.getBounds();
                event.width += rect1.width;
                event.height = Math.max(event.height, rect1.height + 2);
                break;
            }
            case SWT.PaintItem:
            {
                TableItem item = (TableItem) event.item;
                Rectangle rect2 = image.getBounds();
                Rectangle bounds = item.getBounds(event.index);
                int x = bounds.x + bounds.width - rect2.width;
                int offset = Math.max(0, (event.height - rect2.height) / 2);
                event.gc.drawImage(image, x, event.y + offset);
                break;
            }
        }
    };
    table.addListener(SWT.MeasureItem, paintListener);
    table.addListener(SWT.PaintItem, paintListener);

    for (int i = 0; i < columnCount; i++)
    {
        table.getColumn(i).pack();
    }
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch()) display.sleep();
    }
    if (image != null) image.dispose();
    display.dispose();
}

Looks like this:



来源:https://stackoverflow.com/questions/41583127/how-to-add-image-on-the-right-side-of-the-table-column-and-table-cell-using-swt

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