Java Tutorial/SWT/Table Sort
Sort a table by column
<source lang="java">
/*******************************************************************************
* 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: sort a table by column * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.2 */
import java.text.Collator; import java.util.Locale; 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.TableColumn; import org.eclipse.swt.widgets.TableItem; public class TableSortByColumn {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER); table.setHeaderVisible(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"); TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "a", "3" }); item = new TableItem(table, SWT.NONE); item.setText(new String[] { "b", "2" }); item = new TableItem(table, SWT.NONE); item.setText(new String[] { "c", "1" }); column1.setWidth(100); column2.setWidth(100); Listener sortListener = new Listener() { public void handleEvent(Event e) { TableItem[] items = table.getItems(); Collator collator = Collator.getInstance(Locale.getDefault()); TableColumn column = (TableColumn) e.widget; int index = column == column1 ? 0 : 1; for (int i = 1; i < items.length; i++) { String value1 = items[i].getText(index); for (int j = 0; j < i; j++) { String value2 = items[j].getText(index); if (collator.rupare(value1, value2) < 0) { String[] values = { items[i].getText(0), items[i].getText(1) }; items[i].dispose(); TableItem item = new TableItem(table, SWT.NONE, j); item.setText(values); items = table.getItems(); break; } } } table.setSortColumn(column); } }; column1.addListener(SWT.Selection, sortListener); column2.addListener(SWT.Selection, sortListener); table.setSortColumn(column1); table.setSortDirection(SWT.UP); shell.setSize(shell.ruputeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Sort table column in two direction with custom Comparator
<source lang="java">
/*******************************************************************************
* 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; import java.util.Arrays; import java.util.ruparator; 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.TableColumn; import org.eclipse.swt.widgets.TableItem; /*
* Show a sort indicator in the column header * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.2 */
public class TableSortTwoDirection {
public static void main(String[] args) { // initialize data with keys and random values int size = 100; Random random = new Random(); final int[][] data = new int[size][]; for (int i = 0; i < data.length; i++) { data[i] = new int[] { i, random.nextInt() }; } // create a virtual table to display data Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.VIRTUAL); table.setHeaderVisible(true); table.setLinesVisible(true); table.setItemCount(size); final TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Key"); column1.setWidth(200); final TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Value"); column2.setWidth(200); table.addListener(SWT.SetData, new Listener() { public void handleEvent(Event e) { TableItem item = (TableItem) e.item; int index = table.indexOf(item); int[] datum = data[index]; item.setText(new String[] { Integer.toString(datum[0]), Integer.toString(datum[1]) }); } }); // Add sort indicator and sort data when column selected Listener sortListener = new Listener() { public void handleEvent(Event e) { // determine new sort column and direction TableColumn sortColumn = table.getSortColumn(); TableColumn currentColumn = (TableColumn) e.widget; int dir = table.getSortDirection(); if (sortColumn == currentColumn) { dir = dir == SWT.UP ? SWT.DOWN : SWT.UP; } else { table.setSortColumn(currentColumn); dir = SWT.UP; } // sort the data based on column and direction final int index = currentColumn == column1 ? 0 : 1; final int direction = dir; Arrays.sort(data, new Comparator() { public int compare(Object arg0, Object arg1) { int[] a = (int[]) arg0; int[] b = (int[]) arg1; if (a[index] == b[index]) return 0; if (direction == SWT.UP) { return a[index] < b[index] ? -1 : 1; } return a[index] < b[index] ? 1 : -1; } }); // update data displayed in table table.setSortDirection(dir); table.clearAll(); } }; column1.addListener(SWT.Selection, sortListener); column2.addListener(SWT.Selection, sortListener); table.setSortColumn(column1); table.setSortDirection(SWT.UP); shell.setSize(shell.ruputeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>