1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
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 Exemple {
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);
comp.setLayout(new GridLayout(2, false));
final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
construitColonnes(table, 0);
construitColonnes(table, 1);
final ControlAdapter ca = new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = comp.getClientArea();
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
ScrollBar vBar = table.getVerticalBar();
int width = area.width - 10;
if (size.y > area.height + table.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = vBar.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
computeColumnSize(width);
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);
computeColumnSize(width);
}
}
private void computeColumnSize(int width) {
int totalSize = 20;
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
if (i == n - 1) {
table.getColumn(i).setWidth(width - totalSize);
} else {
table.getColumn(i).setWidth(width / n);
}
totalSize += table.getColumn(i).getWidth();
}
}
};
comp.addControlListener(ca);
Button add = new Button(comp, SWT.PUSH);
add.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, true, false));
add.setText("Ajouter une colonne");
add.addSelectionListener(new SelectionAdapter() {
/**
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
@Override
public void widgetSelected(SelectionEvent arg0) {
table.getParent().setRedraw(false);
construitColonnes(table, table.getColumnCount());
ca.controlResized(null);
table.getParent().layout();
table.getParent().setRedraw(true);
}
});
Button remove = new Button(comp, SWT.PUSH);
remove.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));
remove.setText("Supprimer une colonne");
remove.addSelectionListener(new SelectionAdapter() {
/**
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
@Override
public void widgetSelected(SelectionEvent arg0) {
if (table.getColumnCount() == 1) {
return;
}
table.getParent().setRedraw(false);
table.getColumn(table.getColumnCount()-1).dispose();
ca.controlResized(null);
table.getParent().layout();
table.getParent().setRedraw(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void construitColonnes(Table table, int j) {
final TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + j);
for (int i = 0; i < 10; i++) {
TableItem item;
if (j==0) {
item = new TableItem(table, SWT.NONE);
} else {
item = table.getItem(i);
}
item.setText(j, "item " + j + i);
}
}
} |
Partager