Java Tutorial/SWT/Table
Содержание
- 1 Create a table (lazy)
- 2 Create a table with 000 items (lazy)
- 3 Create a table with 000 items (lazy, page size 64)
- 4 Create a table with columns, headers, lines
- 5 Create a table without columns and headers
- 6 Create a virtual table and add ntries to it every 500 ms
- 7 Fill a table in the Thread
- 8 Get TableItem Index in a Table
- 9 Insert TableItem by Index
- 10 new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION| SWT.CHECK)
- 11 Resize columns as table resizes
- 12 Set table selected item and remove it from table
- 13 Set table selection to scroll a table
- 14 Set top index to scroll a table
- 15 Tables
- 16 Table Styles
- 17 Table With CheckBox Cell
Create a table (lazy)
/*******************************************************************************
* 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
*******************************************************************************/
/*
* example snippet: create a table (lazy)
*
* 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.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.TableItem;
public class TableCreateLazy {
public static void main(String[] args) {
final Display display = new Display();
final Image image = new Image(display, 16, 16);
GC gc = new GC(image);
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
gc.fillRectangle(image.getBounds());
gc.dispose();
final Shell shell = new Shell(display);
shell.setText("Lazy Table");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 20000; i++) {
if (table.isDisposed())
return;
final int[] index = new int[] { i };
display.syncExec(new Runnable() {
public void run() {
if (table.isDisposed())
return;
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Table Item " + index[0]);
item.setImage(image);
}
});
}
}
};
thread.start();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
image.dispose();
display.dispose();
}
}
Create a table with 000 items (lazy)
/*******************************************************************************
* 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;
/*
* Virtual Table example snippet: create a table with 1,000,000 items (lazy)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
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 TableCreateLazy {
static final int COUNT = 1000000;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
int index = table.indexOf(item);
item.setText("Item " + index);
System.out.println(item.getText());
}
});
table.setLayoutData(new RowData(200, 200));
long t1 = System.currentTimeMillis();
table.setItemCount(COUNT);
long t2 = System.currentTimeMillis();
System.out.println("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)");
shell.layout();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create a table with 000 items (lazy, page size 64)
/*******************************************************************************
* 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;
/*
* Virtual Table example snippet: create a table with 1,000,000 items (lazy, page size 64)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
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 TableWithPageSize {
static final int PAGE_SIZE = 64;
static final int COUNT = 100000;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
int index = table.indexOf(item);
int start = index / PAGE_SIZE * PAGE_SIZE;
int end = Math.min(start + PAGE_SIZE, table.getItemCount());
for (int i = start; i < end; i++) {
item = table.getItem(i);
item.setText("Item " + i);
}
}
});
table.setLayoutData(new RowData(200, 200));
long t1 = System.currentTimeMillis();
table.setItemCount(COUNT);
long t2 = System.currentTimeMillis();
System.out.println("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms) [page=" + PAGE_SIZE
+ "]");
shell.layout();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create a table with columns, headers, lines
/*******************************************************************************
* 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: create a table (columns, headers, lines)
*
* 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.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 TableWithColumnHeader {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
String[] titles = { " ", "C", "!", "Description", "Resource", "In Folder", "Location" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
}
for (int i = 0; i < 100; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "x");
item.setText(1, "y");
item.setText(2, "!");
item.setText(3, "this stuff behaves the way I expect");
item.setText(4, "almost everywhere");
item.setText(5, "some.folder");
item.setText(6, "line " + i + " in nowhere");
}
for (int i=0; i<titles.length; i++) {
table.getColumn (i).pack ();
}
table.setSize(table.ruputeSize(SWT.DEFAULT, 200));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create a table without columns and headers
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableWithoutColumnHeader {
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, 0);
item.setText("Item " + i);
}
table.setSize(180, 200);
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create a virtual table and add ntries to it every 500 ms
/*******************************************************************************
* 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.Arrays;
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 TableCreate1000Entries {
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) {
TableItem item = (TableItem) e.item;
int index = table.indexOf(item);
item.setText("Item " + data[index]);
}
});
Thread thread = new Thread() {
public void run() {
int count = 0;
Random random = new Random();
while (count++ < 500) {
if (table.isDisposed())
return;
// add 10 random numbers to array and sort
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();
}
Arrays.sort(data);
display.syncExec(new Runnable() {
public void run() {
if (table.isDisposed())
return;
table.setItemCount(data.length);
table.clearAll();
}
});
try {
Thread.sleep(500);
} catch (Throwable t) {
}
}
}
};
thread.start();
shell.open();
while (!shell.isDisposed() || thread.isAlive()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Fill a table in the Thread
/*******************************************************************************
* 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;
/*
* example snippet: create a table (lazy)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.SWT;
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.TableItem;
public class TableFillThread {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Lazy Table");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 20000; i++) {
final int[] index = new int[] { i };
display.syncExec(new Runnable() {
public void run() {
if (table.isDisposed())
return;
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Table Item " + index[0]);
}
});
}
}
};
thread.start();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Get TableItem Index in a Table
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableItemGetIndex {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
for (int i = 0; i < 12; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
TableItem item = new TableItem(table, SWT.NONE, 5);
item.setText(" New Item ");
System.out.println(table.indexOf(item));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Insert TableItem by Index
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableItemInsertByIndex {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
for (int i = 0; i < 12; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
TableItem item = new TableItem(table, SWT.NONE, 5);
item.setText(" New Item ");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION| SWT.CHECK)
import org.eclipse.swt.SWT;
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 TableMultiCheckSelectionBorder {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION| SWT.CHECK);
table.setLinesVisible(true);
table.setHeaderVisible(true);
String[] titles = { " ", "C", "!", "Description", "Resource", "In Folder", "Location" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
}
for (int i = 0; i < 10; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "x");
item.setText(1, "y");
item.setText(2, "!");
item.setText(3, "dddddddddddddddddddd");
item.setText(4, "rrrrrrrrrrrrrrrrrrr");
item.setText(5, "some.folder");
item.setText(6, "line " + i + " in nowhere");
}
for (int i=0; i<titles.length; i++) {
table.getColumn (i).pack ();
}
table.setSize(table.ruputeSize(SWT.DEFAULT, 200));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Resize columns as table resizes
/*******************************************************************************
* 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: resize columns as table resizes
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ruposite;
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 TableColumnResizeTableResize {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Composite comp = new Composite(shell, SWT.NONE);
final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
for (int i = 0; i < 10; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "item 0" + i, "item 1" + i });
}
comp.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = comp.getClientArea();
Point preferredSize = table.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = area.width - 2 * table.getBorderWidth();
if (preferredSize.y > area.height + table.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = table.getVerticalBar().getSize();
width -= vBarSize.x;
}
Point oldSize = table.getSize();
if (oldSize.x > area.width) {
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
column1.setWidth(width / 3);
column2.setWidth(width - column1.getWidth());
table.setSize(area.width, area.height);
} else {
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
table.setSize(area.width, area.height);
column1.setWidth(width / 3);
column2.setWidth(width - column1.getWidth());
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set table selected item and remove it from table
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class TableRowSelectionRemove{
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);
table.setSize (200, 200);
for (int i=0; i<128; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText ("Item " + i);
}
table.setSelection(2);
table.remove (table.getSelectionIndices ());
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Set table selection to scroll a table
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableScrollSelection {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
for (int i = 0; i < 128; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
table.setSelection(95);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set top index to scroll a table
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableScrollIndex {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setSize(200, 200);
for (int i = 0; i < 128; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
table.setTopIndex(95);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Tables
- Tables are useful for handling tabular data.
- The org.eclipse.swt.widgets.Table class represents a table.
- The TableColumn class represents a column in a table
- The TableItem class represents a row in a table.
To create a table, you follow these basic steps:
- Construct a Table instance and set its property.
- Define columns by using TableColumn objects.
- Add rows by creating TableItem objects.
Table Styles
StyleDescriptionSWT.SINGLEOnly one table row may be selected at a time. This is the default.SWT.MULTIMultiple table rows may be selected, usually by holding down a key on the keyboard (typically the Ctrl key) while clicking the table row.SWT.CHECKPlaces a checkbox at the beginning of each table row. Note that the checked state of the checkbox is independent from the selected state of the table row.SWT.FULL_SELECTIONHighlights the entire row, rather than just the first column of the row, when the row is selected. The default is to highlight only the first column.SWT.HIDE_SELECTIONRemoves the highlight from the selected row (if any) when the window containing the table isn"t the foreground window. The default is to keep the row highlighted whether or not the parent window is the foreground window.
Table With CheckBox Cell
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class TableCheckBoxCell {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Table table = new Table(shell, SWT.CHECK | 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);
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}