Java Tutorial/SWT/TableItem

Материал из Java эксперт
Перейти к: навигация, поиск

Add Icon to all table cells

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Table example snippet: Images on the right hand side of a TableItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableCellIconAdd {
  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);
      item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c" });
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
      public void handleEvent(Event event) {
        switch (event.type) {
        case SWT.MeasureItem: {
          Rectangle rect = image.getBounds();
          event.width += rect.width;
          event.height = Math.max(event.height, rect.height + 2);
          break;
        }
        case SWT.PaintItem: {
          int x = event.x + event.width;
          Rectangle rect = image.getBounds();
          int offset = Math.max(0, (event.height - rect.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();
  }
}





Adding Image to Table Column

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableColumnImageAdding {
  public static void main(String[] args) {
    final Display display = new Display();
    
    Image checkedImage = new Image(display,"yourFile.gif");
    Image uncheckedImage = new Image(display,"yourFile.gif");
    
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    TableColumn column3 = new TableColumn(table, SWT.NONE);
    
    TableItem item1 = new TableItem(table, SWT.NONE);
    item1.setText(new String[] { "first item", "Image 1", "Image 2" });
    item1.setImage(1, uncheckedImage);
    item1.setImage(2, uncheckedImage);
    column1.pack();
    column2.pack();
    column3.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    checkedImage.dispose();
    uncheckedImage.dispose();
    display.dispose();
  }
}





Adding Rows

The TableItem class represents rows in the table. The parent of a TableItem is the containing Table.

Its constructor:



TableItem(Table parent, int style)
TableItem(Table parent, int style, int index)



  1. Using the second constructor inserts the row at the specified zero-based index.
  2. Passing an index out of range throws an IllegalArgumentException.
  3. No styles apply for TableItem, so you should always pass SWT.NONE.


Draw different foreground colors and fonts for text in a TableItem

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Table example snippet: draw different foreground colors for text in a TableItem.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableItemForegroundFont {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Table: Change style multiple times in cell");
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    for (int i = 0; i < 10; i++) {
      new TableItem(table, SWT.NONE);
    }
    final TextLayout textLayout = new TextLayout(display);
    textLayout.setText("SWT: Standard Widget Toolkit");
    Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD);
    Font font2 = new Font(display, "Tahoma", 10, SWT.NORMAL);
    Font font3 = new Font(display, "Tahoma", 14, SWT.ITALIC);
    TextStyle style1 = new TextStyle(font1, display.getSystemColor(SWT.COLOR_BLUE), null);
    TextStyle style2 = new TextStyle(font2, display.getSystemColor(SWT.COLOR_MAGENTA), null);
    TextStyle style3 = new TextStyle(font3, display.getSystemColor(SWT.COLOR_RED), null);
    textLayout.setStyle(style1, 0, 0);
    textLayout.setStyle(style1, 5, 12);
    textLayout.setStyle(style2, 1, 1);
    textLayout.setStyle(style2, 14, 19);
    textLayout.setStyle(style3, 2, 2);
    textLayout.setStyle(style3, 21, 27);
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    table.addListener(SWT.PaintItem, new Listener() {
      public void handleEvent(Event event) {
        textLayout.draw(event.gc, event.x, event.y);
      }
    });
    final Rectangle textLayoutBounds = textLayout.getBounds();
    table.addListener(SWT.MeasureItem, new Listener() {
      public void handleEvent(Event e) {
        e.width = textLayoutBounds.width + 2;
        e.height = textLayoutBounds.height + 2;
      }
    });
    shell.setSize(400, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    font1.dispose();
    font2.dispose();
    font3.dispose();
    textLayout.dispose();
    display.dispose();
  }
}





Enlarge Table Cell

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * Table example snippet: Images on the right hand side of a TableItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableCellEnlarge {
  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);
      item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c" });
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
      public void handleEvent(Event event) {
        switch (event.type) {
        case SWT.MeasureItem: {
          Rectangle rect = image.getBounds();
          event.width += rect.width;
          event.height = Math.max(event.height, rect.height + 2);
          break;
        }
        case SWT.PaintItem: {
          int x = event.x + event.width;
          Rectangle rect = image.getBounds();
          int offset = Math.max(0, (event.height - rect.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();
  }
}





Multiline Table cell

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/* 
 * example snippet: Multiple lines per TableItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class MultipleLinePerTableItem {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TableItem");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    int columnCount = 4;
    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);
      item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c",
          "item " + i + " d" });
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
      public void handleEvent(Event event) {
        switch (event.type) {
        case SWT.MeasureItem: {
          TableItem item = (TableItem) event.item;
          String text = getText(item, event.index);
          Point size = event.gc.textExtent(text);
          event.width = size.x;
          event.height = Math.max(event.height, size.y);
          break;
        }
        case SWT.PaintItem: {
          TableItem item = (TableItem) event.item;
          String text = getText(item, event.index);
          Point size = event.gc.textExtent(text);
          int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
          event.gc.drawText(text, event.x, event.y + offset2, true);
          break;
        }
        case SWT.EraseItem: {
          event.detail &= ~SWT.FOREGROUND;
          break;
        }
        }
      }
      String getText(TableItem item, int column) {
        String text = item.getText(column);
        if (column != 0) {
          int index = table.indexOf(item);
          if ((index + column) % 3 == 1) {
            text += "\nnew line";
          }
          if ((index + column) % 3 == 2) {
            text += "\nnew line\nnew line";
          }
        }
        return text;
      }
    };
    table.addListener(SWT.MeasureItem, paintListener);
    table.addListener(SWT.PaintItem, paintListener);
    table.addListener(SWT.EraseItem, paintListener);
    for (int i = 0; i < columnCount; i++) {
      table.getColumn(i).pack();
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Set Cell Background color

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class CellBackground {
  public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color gray = display.getSystemColor(SWT.COLOR_GRAY);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.BORDER);
    table.setBackground(gray);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    TableColumn column3 = new TableColumn(table, SWT.NONE);
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] {"normal","blue background","red background"});
    item.setBackground(1, blue);
    item.setBackground(2, red);

    column1.pack();
    column2.pack();
    column3.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Set Cell Foreground

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class CellForeground {
  public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color gray = display.getSystemColor(SWT.COLOR_GRAY);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.BORDER);
    table.setBackground(gray);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    TableColumn column3 = new TableColumn(table, SWT.NONE);
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] {"normal","blue foreground","red foreground"});
    item.setForeground(1, blue);
    item.setForeground(2, red);

    column1.pack();
    column2.pack();
    column3.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Set Table Background and TableItem Foreground

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class TableBackgroundTableItemForeground{
 
public static void main(String[] args) {
  Display display = new Display();
  
  Color red = display.getSystemColor(SWT.COLOR_RED);
  Color gray = display.getSystemColor(SWT.COLOR_GRAY);
  
  Shell shell = new Shell(display);
  shell.setLayout(new FillLayout());
  
  Table table = new Table(shell, SWT.BORDER);
  table.setBackground(gray);
  TableColumn column1 = new TableColumn(table, SWT.NONE);
  TableColumn column2 = new TableColumn(table, SWT.NONE);
  TableColumn column3 = new TableColumn(table, SWT.NONE);
  
  TableItem item = new TableItem(table, SWT.NONE);
  item.setText(new String[] {"entire","row","red foreground"});
  item.setForeground(red);
  
  
  column1.pack();
  column2.pack();
  column3.pack();
  
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
      display.sleep();
  }
  display.dispose();
}
}





Set TableItem Background

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableItemBackground {
  public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color gray = display.getSystemColor(SWT.COLOR_GRAY);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.BORDER);
    table.setBackground(gray);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    TableColumn column3 = new TableColumn(table, SWT.NONE);
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "entire", "row", "red background" });
    item.setBackground(red);
    column1.pack();
    column2.pack();
    column3.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Update table item text

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Table example snippet: update table item text
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableItemUpdateText {
  static char content = "a";
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 240);
    Table table = new Table(shell, SWT.NONE);
    table.setBounds(10, 10, 160, 160);
    final TableItem[] items = new TableItem[4];
    for (int i = 0; i < 4; i++) {
      new TableColumn(table, SWT.NONE).setWidth(40);
    }
    for (int i = 0; i < 4; i++) {
      items[i] = new TableItem(table, SWT.NONE);
      populateItem(items[i]);
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Change");
    button.pack();
    button.setLocation(10, 180);
    button.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        for (int i = 0; i < 4; i++) {
          populateItem(items[i]);
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  static void populateItem(TableItem item) {
    String stringContent = String.valueOf(content);
    item.setText(new String[] { stringContent, stringContent, stringContent, stringContent });
    content++;
    if (content > "z")
      content = "a";
  }
}