Save state when application closes [message #246362] |
Thu, 27 May 2004 12:35 |
Eclipse User |
|
|
|
Originally posted by: christian.hauser.dvbern.ch
Hello everyone
How do I save the state of some GUI controls in my standalone JFace
application when the user closes the window by clicking on the X in the
upper right corner?
Here is an example of what I'm trying. I'd like to call saveSettings()
when the user wants to exit the application. However, it is impossible
to access the table widget (resulting in a NPE). This is possible with
plain SWT. Could anyone enlighten me, please?
public class ClosingJFace extends ApplicationWindow {
private Table table;
public ClosingJFace() {
super(null);
}
protected Control createContents(Composite parent) {
getShell().setText("MyApp");
getShell().addListener(SWT.Dispose, new Listener() {
public void handleEvent(Event event) {
System.out.println("Disposing Shell...");
saveSettings();
}
});
getShell().addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
System.out.println("Closing Shell...");
//saveSettings();
}
});
Composite composite = new Composite(parent, SWT.NONE);
Table table = new Table(composite, SWT.MULTI | SWT.BORDER |
SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
String[] titles = {"Resource", "Folder", "Location"};
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NULL);
column.setText(titles[i]);
}
int count = 50;
for (int i = 0; i < count; i++) {
TableItem item = new TableItem(table, SWT.NULL);
item.setText(0, "almost everywhere");
item.setText(1, "some.folder");
item.setText(2, "line " + i + " in nowhere");
}
for (int i = 0; i < titles.length; i++) {
table.getColumn(i).pack();
}
table.setSize(table.computeSize(SWT.DEFAULT, 150));
return composite;
}
public void saveSettings() {
DialogSettings settings = new DialogSettings("MyApp");
for (int i = 0; i < table.getColumnCount(); i++) {
settings.put(table.getColumn(i).getText() + ".ColumnWidth",
table.getColumn(i).getWidth());
System.out.println("setting " + i + " added...");
}
}
public static void main(String[] args) {
ClosingJFace app = new ClosingJFace();
app.setBlockOnOpen(true);
app.open();
Display.getCurrent().dispose();
}
}
Any hint on that is highly appreciated.
Regards,
Christian
|
|
|
Powered by
FUDForum. Page generated in 0.02471 seconds