 | // グラフ表示 this.getTable().addListener(SWT.PaintItem, new Listener(){
public void handleEvent(Event event) { // 3列目をグラフ表示 if (event.index == 2) { // グラフィックコンテキスト取得 GC gc = event.gc; // 現在の前景色、背景色を記憶 Color foreground = gc.getForeground(); Color background = gc.getBackground(); // 3列目の値を取得 TableItem item = (TableItem) event.item; int quantity = Integer.parseInt(item.getText(2)); // 横幅指定 Table table = (Table) event.widget; TableColumn column = table.getColumn(event.index); int width = (column.getWidth() - 1) * quantity / 100; // グラフ描画 gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_YELLOW)); gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_RED)); gc.fillGradientRectangle(event.x, event.y, width, event.height, false); // グラフの枠を描画 gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_YELLOW)); Rectangle rect = new Rectangle(event.x, event.y, width - 1, event.height - 1); gc.drawRectangle(rect); gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND)); String label = item.getText(1) + " %"; Point labelSize = gc.textExtent(label); int offset = Math.max(0, (event.height - labelSize.y) / 2); gc.drawText(label, event.x+2, event.y + offset, true); // 前景色、背景色を元に戻す gc.setForeground(foreground); gc.setBackground(background); } } });
|  |