Java Tutorial/SWT/Table Event
Содержание
- 1 Add Default Table Selection Listener and Get the Selected TableItem
- 2 Add Table Selection Listener and Get Selected TableItem
- 3 Find a table cell from mouse down (works for any table style)
- 4 Get TableItem from Mouse position
- 5 TableColumn Move Listener
- 6 Table events: MeasureItem, PaintItem and EraseItem
- 7 Table measure item listener
- 8 Table paint item listener
- 9 Table Selection Event
- 10 Table SetData Event
Add Default Table Selection Listener and Get the Selected TableItem
import org.eclipse.swt.SWT;
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 TableDefaultSelectionListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
for (int i = 0; i < 16; i++) {
TableItem item = new TableItem(table, 0);
item.setText("Item " + i);
}
table.setBounds(0, 0, 100, 100);
table.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
String string = "";
TableItem[] selection = table.getSelection();
for (int i = 0; i < selection.length; i++)
string += selection[i] + " ";
System.out.println("DefaultSelection={" + string + "}");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Add Table Selection Listener and Get Selected TableItem
import org.eclipse.swt.SWT;
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 TableSelectionListenerSelectedTableItem {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
for (int i = 0; i < 16; i++) {
TableItem item = new TableItem(table, 0);
item.setText("Item " + i);
}
table.setBounds(0, 0, 100, 100);
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
String string = "";
TableItem[] selection = table.getSelection();
for (int i = 0; i < selection.length; i++)
string += selection[i] + " ";
System.out.println("Selection={" + string + "}");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Find a table cell from mouse down (works for any table style)
/*******************************************************************************
* 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: find a table cell from mouse down (works for any table style)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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 TableCellUnderMouse {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final int rowCount = 64, columnCount = 4;
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
}
for (int i = 0; i < rowCount; i++) {
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < columnCount; j++) {
item.setText(j, "Item " + i + "-" + j);
}
}
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
Point size = table.ruputeSize(SWT.DEFAULT, 200);
table.setSize(size);
shell.pack();
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
while (index < table.getItemCount()) {
boolean visible = false;
TableItem item = table.getItem(index);
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
System.out.println("Item " + index + "-" + i);
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Get TableItem from Mouse position
/*******************************************************************************
* 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: find a table cell from mouse down (SWT.FULL_SELECTION)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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 TableItemMouse {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final int rowCount = 64, columnCount = 4;
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i);
}
for (int i = 0; i < rowCount; i++) {
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < columnCount; j++) {
item.setText(j, "Item " + i + "-" + j);
}
}
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
Point size = table.ruputeSize(SWT.DEFAULT, 200);
table.setSize(size);
shell.pack();
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
TableItem item = table.getItem(pt);
if (item == null)
return;
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
int index = table.indexOf(item);
System.out.println("Item " + index + "-" + i);
}
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
TableColumn Move Listener
/*******************************************************************************
* Copyright (c) 2000, 2005 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;
/*
* Allow user to reorder columns and reorder columns programmatically.
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
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 TableColumnMoveListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.HORIZONTAL));
final Table table = new Table(shell, SWT.BORDER | SWT.CHECK);
table.setLayoutData(new RowData(-1, 300));
table.setHeaderVisible(true);
TableColumn column = new TableColumn(table, SWT.LEFT);
column.setText("Column 0");
column = new TableColumn(table, SWT.CENTER);
column.setText("Column 1");
column = new TableColumn(table, SWT.CENTER);
column.setText("Column 2");
column = new TableColumn(table, SWT.CENTER);
column.setText("Column 3");
column = new TableColumn(table, SWT.CENTER);
column.setText("Column 4");
for (int i = 0; i < 100; i++) {
TableItem item = new TableItem(table, SWT.NONE);
String[] text = new String[] { i + " 0", i + " 1", i + " 2", i + " 3", i + " 4" };
item.setText(text);
}
Listener listener = new Listener() {
public void handleEvent(Event e) {
System.out.println("Move " + e.widget);
}
};
TableColumn[] columns = table.getColumns();
for (int i = 0; i < columns.length; i++) {
columns[i].pack();
columns[i].setMoveable(true);
columns[i].addListener(SWT.Move, listener);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Table events: MeasureItem, PaintItem and EraseItem
/*******************************************************************************
* 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 TableEventMeasureItemPaintItemEraseItem {
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();
}
}
Table measure item listener
/*******************************************************************************
* 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 TableMeasureItemListener {
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();
}
}
Table paint item listener
/*******************************************************************************
* 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 TablePaintItemListener {
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();
}
}
Table Selection Event
import org.eclipse.swt.SWT;
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 TableSelectionEvent {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
for (int i = 0; i < 12; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
table.setSize(100, 100);
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
String string = event.detail == SWT.CHECK ? "Checked" : "Selected";
System.out.println(event.item + " " + string);
}
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Table SetData Event
/*******************************************************************************
* Copyright (c) 2000, 2005 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;
/*
* Create a virtual table and add 1000 entries to it every 500 ms.
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
import java.util.Random;
import org.eclipse.swt.SWT;
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 TableSetDataEvent {
static int[] data = new int[0];
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event e) {
System.out.println(e);
TableItem item = (TableItem) e.item;
int index = table.indexOf(item);
item.setText("Item " + data[index]);
}
});
int count = 0;
Random random = new Random();
while (count++ < 50) {
int grow = 10;
int[] newData = new int[data.length + grow];
System.arraycopy(data, 0, newData, 0, data.length);
int index = data.length;
data = newData;
for (int j = 0; j < grow; j++) {
data[index++] = random.nextInt();
}
table.setItemCount(data.length);
table.clearAll();
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}