lilimalin
发贴: 1
积分: 0
|
于 2006-09-07 14:53
import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CLabel; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.internal.win32.OS; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.ScrollBar; 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 dbtable{ public static void main (String [] args) { int rowCount = 40; int columnCount = 14; final Display display = new Display (); Shell shell = new Shell (display); shell.setLayout(new FillLayout()); Composite parent = new Composite(shell, SWT.BORDER); final GridLayout gridLayout = new GridLayout(2,false); gridLayout.marginWidth = gridLayout.marginHeight = gridLayout.horizontalSpacing = 0; // gridLayout.numColumns = 2; gridLayout.marginWidth = 0; parent.setLayout(gridLayout);
final Composite composite = new Composite(parent, SWT.NONE); final GridLayout gridLayout_1 = new GridLayout(); gridLayout_1.horizontalSpacing = 0; gridLayout_1.marginWidth = 0; gridLayout_1.marginHeight = 0; composite.setLayout(gridLayout_1); final GridData gridData = new GridData(SWT.LEFT, SWT.FILL, false, true); gridData.widthHint = 26; composite.setLayoutData(gridData);
final Composite composite_1 = new Composite(parent, SWT.NONE); composite_1.setLayout(new FillLayout()); composite_1.setLayoutData(new GridData(GridData.FILL_BOTH)); final Table leftTable = new Table(composite, SWT.MULTI | SWT.FULL_SELECTION); final GridData gridData_1 = new GridData(SWT.LEFT, SWT.FILL, false, true); gridData_1.widthHint = 25; leftTable.setLayoutData(gridData_1); leftTable.setHeaderVisible(true); leftTable.setLinesVisible(true); final Table rightTable = new Table(composite_1, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); rightTable.setHeaderVisible(true); rightTable.setLinesVisible(true); // Create columns TableColumn column0 = new TableColumn(leftTable, SWT.NONE); column0.setText(""); column0.setWidth(0); TableColumn column1 = new TableColumn(leftTable, SWT.NONE); column1.setResizable(false); column1.setText(""); column1.setWidth(25); for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(rightTable, SWT.NONE); column.setText("Value "+i); column.setWidth(200); } // Create rows TableEditor te[] = new TableEditor[rowCount]; Image image = new Image(display, 1, 17); for (int i = 0; i < rowCount; i++) { TableItem item = new TableItem(leftTable, SWT.NONE); item.setChecked(true); te = new TableEditor(leftTable); te.horizontalAlignment = SWT.LEFT; te.grabHorizontal = true; final CLabel lbl = new CLabel(leftTable, SWT.SHADOW_OUT); lbl.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); lbl.setAlignment(SWT.CENTER); lbl.setEnabled(false); te.setEditor(lbl, item, 1); item.setImage(image); // "/icons/01.gif").getImageData())); //ImageDescriptor.createFromURL(); //loadImage(); item = new TableItem(rightTable, SWT.NONE); item.setImage(image); for (int j = 0; j < columnCount; j++) { item.setText(j, "Item "+i+" value @ "+j); } } // Make selection the same in both tables leftTable.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { rightTable.setSelection(leftTable.getSelectionIndices()); } }); // On Windows, the selection is gray if the table does not have focus. // To make both tables appear in focus, draw teh selection background here. // This part only works on version 3.2 or later. Listener eraseListener = new Listener() { public void handleEvent(Event event) { if((event.detail & SWT.SELECTED) != 0) { GC gc = event.gc; Rectangle rect = event.getBounds(); gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT)); gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRectangle(rect); event.detail &= ~SWT.SELECTED; } } }; Label spacer = new Label(parent, SWT.NONE); GridData spacerData = new GridData(); leftTable.addListener(SWT.EraseItem, eraseListener); // Make vertical scrollbars scroll together ScrollBar vBarLeft = leftTable.getVerticalBar(); vBarLeft.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { rightTable.setTopIndex(leftTable.getTopIndex()); } }); rightTable.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { leftTable.setSelection(rightTable.getSelectionIndices()); } }); rightTable.addListener(SWT.EraseItem, eraseListener); ScrollBar vBarRight = rightTable.getVerticalBar(); vBarRight.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { leftTable.setTopIndex(rightTable.getTopIndex()); } }); // Horizontal bar on second table takes up a little extra space. // To keep vertical scroll bars in sink, force table1 to end above // horizontal scrollbar ScrollBar hBarRight = rightTable.getHorizontalBar(); spacerData.heightHint = hBarRight.getSize().y; spacer.setVisible(false); parent.setBackground(leftTable.getBackground()); shell.setSize(600, 400); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }
|