2017-01-11 58 views
0

如何將圖像添加到表格列的右側,並將文本保留在左側。下面 是代碼如何使用java中的SWT在表格列和表格單元的右側添加圖像

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"); 

表列標題左側「目錄」,現在我想在另一端(表的右側端)添加圖像。 我應該可以在表格單元的右側添加每行圖片。

+0

我想你忘了,包括圖像。另外,你可以包含一些代碼作爲迄今爲止嘗試的一個起點嗎? – avojak

+0

@avojak我無法添加圖片,因爲它需要10個聲望。 – Lisha

+1

@Lisha將它上傳到某處並將鏈接發佈到此處。 – Baz

回答

0

我稍微修飾this snippet得到它做你問什麼:

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

是這樣的:

enter image description here

+0

這爲我工作,但圖像只有當我設置文本的行,即表項目,現在我不想設置文本到表單元格,但只是想在所有單元的右端顯示圖像。還有一個問題......表格的水平跨度已設置爲2,但圖像緊挨着文本的最後一個字母,而不是我希望圖像出現在單元格的末尾。我如何將相同的圖像添加到表列(標題)。 – Lisha

+0

@Lisha好的,我更新了答案。至於表格欄標題:你最好是在你將這個答案加入後作爲一個單獨的問題。 – Baz

+0

這是行不通的,只有當我將文字設置到表格項目 – Lisha

相關問題