2009年5月11日

Eclipse SWT notifyListeners


//-- Main.java --
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class Main extends Composite {

private List list = null;
private Button button = null;

public static void main(String[] args) {

Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(new Point(300, 100));
new Main(shell, SWT.NONE);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

public Main(Composite parent, int style) {
super(parent, style);
initialize();
listNotifyListeener();
}

private void initialize() {
FillLayout fillLayout = new FillLayout();
fillLayout.type = org.eclipse.swt.SWT.VERTICAL;
list = new List(this, SWT.NONE);
list.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
List source = (List)e.getSource();
boolean enabled = source.getSelectionCount() == 1;
button.setEnabled(enabled);
if (enabled) {
button.setText("Click " + source.getSelection()[0]);
} else {
button.setText("Select the item");
}
}
});
list.add("a");
list.add("b");
button = new Button(this, SWT.NONE);
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
list.deselectAll();
listNotifyListeener();

}
});
this.setLayout(fillLayout);
setSize(new Point(300, 100));
}

private void listNotifyListeener() {
Event e = new Event();
list.notifyListeners(SWT.Selection, e);
}

}

沒有留言:

網誌存檔