Minimal SWT Console App [message #1235324] |
Fri, 24 January 2014 04:01 |
Chris Gage Messages: 74 Registered: July 2009 |
Member |
|
|
I'm trying to convert a batch command line program so it has a minimal SWT GUI, and this is my first SWT program. I have however been writing Java since 1996 and Eclipse since 2001 (WSAD4) and was team lead for IBM's first ever Java-based product. Now blissfully retired. This present application is a labor of love, and I possibly might open-source it if I can get some issues like this sorted out.
So I looked everywhere for an example of a "console" application in SWT, but could not find one. Eventually I learned about asyncExec and Runnables and a few other necessities, and came up with the following, but it clearly isn't correct. It does the job, with the Worker class representing the main body of my code, but the GUI window is locked while the application is writing to it. Only after the application is finished running can I move or size the window. At the end of the run the window does contain the correct data, but it might run for 5 or 10 minutes, and during that time I might want to move its window.
I'm sure I have missed something fundamental. I know I can move Eclipse's window while it is doing things in the background, so I need to replicate that ability (without thousands of loc).
Comments and suggestions appreciated.
public class Console2 {
Display display;
Shell shell;
Text text;
private void process() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setLocation(new Point(80, 80));
text = new Text(shell, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);
shell.open();
Display.getDefault().asyncExec(new Runnable() {
public void run() {
new Worker(text).work();
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
class Worker {
Text text;
public Worker(Text t) {
text = t;
};
void work() {
for (int i = 0; i < 10000; i++) {
if (!text.isDisposed()) {
text.append("hello " + i + "\n");
}
}
}
}
public static void main(String[] args) {
new Console2().process();
}
}
[Updated on: Fri, 24 January 2014 04:03] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02931 seconds