如何在Java中使用SWT在表列和表单元格的右侧添加图像 [英] How to add image on the right side of the table column and table cell using SWT in java

查看:176
本文介绍了如何在Java中使用SWT在表列和表单元格的右侧添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表格列的右侧添加图像,将文本保留在左侧. 下面是代码

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.

推荐答案

我稍微修改了

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

看起来像这样:

这篇关于如何在Java中使用SWT在表列和表单元格的右侧添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆