Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Showing the system out (from logger) into a text.(This works if I don't move, resize, iconified or switching between windows.)
Showing the system out (from logger) into a text. [message #875988] |
Wed, 23 May 2012 17:28 |
Chung Ley Messages: 5 Registered: May 2010 |
Junior Member |
|
|
Hi,
I have a SWT UI that collects inputs from the user and then execute some jobs for them when they click on the Go button.
To help the user, I create a modal dialog (shell) with a multi-line text and redirect the system.out and System.err so that they will show up into this text object. I was able to find help with some code snippet on that.
The code seems to work where the text component will get the updates from the logger. However, if I try to re-size, move, or switching between the windows before the job is completed, the entire shell will be "blank" until the job is finished AND then shell will show up with ALL the text into the Text area. My guess is that the thread that I am creating is "blocking" the regular UI update thread; but I don't know any workaround for it. Or there is something wrong that I am doing... Can someone help?>
Here is a snippet of the code itself...
goButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String argumentList = validateInputs();
if (argumentList == null) {
return;
}
dialogShell = new Shell(shell, SWT.PRIMARY_MODAL | SWT.SHEET
| SWT.RESIZE);
GridData gridData;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
dialogShell.setLayout(gridLayout);
final Text logText = new Text(dialogShell, SWT.MULTI | SWT.BORDER
| SWT.READ_ONLY | SWT.V_SCROLL);
logText.setLayoutData(new GridData(GridData.FILL,
GridData.FILL, true, true));
origOutPS = System.out;
origErrPS = System.err;
final Button closeButton = new Button(dialogShell, SWT.PUSH);
gridData = new GridData(GridData.END, GridData.CENTER, false,
false);
closeButton.setLayoutData(gridData);
closeButton.setText("Close");
closeButton.setEnabled(false);
closeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogShell.dispose();
}
});
dialogShell.setDefaultButton(closeButton);
dialogShell.setImage(imageGF);
dialogShell.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
// TODO
}
});
dialogShell.setSize(550, 250);
dialogShell.setText("Processing....");
dialogShell.addShellListener(new ShellAdapter() {
public void shellActivated(ShellEvent event) {
myThread = new Thread(new Runnable() {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
OutputStream out = new OutputStream() {
public void write(int b)
throws IOException {
logText.append(String
.valueOf((char) b));
}
public void write(byte[] b,
int off, int len) {
logText.append(new String(b,
off, len));
}
};
PrintStream ps = new PrintStream(out,
true);
System.setOut(ps);
System.setErr(ps);
try {
myHugeDummyJob(argumentList.split(","));
closeButton.setEnabled(true);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
});
}
});
myThread.start();
}
});
dialogShell.open();
// Setting the Out and Err stream back to the original...
System.setOut(origOutPS);
System.setErr(origErrPS);
}
});
|
|
| |
Re: Showing the system out (from logger) into a text. [message #876451 is a reply to message #876345] |
Thu, 24 May 2012 16:03 |
Chung Ley Messages: 5 Registered: May 2010 |
Junior Member |
|
|
Thank you, Brian...
What you suggested made sense, but I might not have fully understand or implement it correctly. After I made the change, the log messages from the logger goes back to the screen instead of the component logText, and the last message was invalid thread access.
Here is the code with the change (I didn't bother with the closeButton yet).
goButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String argumentList = validateInputs();
if (argumentList == null) {
return;
}
dialogShell = new Shell(shell, SWT.PRIMARY_MODAL | SWT.SHEET
| SWT.RESIZE);
GridData gridData;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
dialogShell.setLayout(gridLayout);
final Text logText = new Text(dialogShell, SWT.MULTI | SWT.BORDER
| SWT.READ_ONLY | SWT.V_SCROLL);
logText.setLayoutData(new GridData(GridData.FILL,
GridData.FILL, true, true));
origOutPS = System.out;
origErrPS = System.err;
final Button closeButton = new Button(dialogShell, SWT.PUSH);
gridData = new GridData(GridData.END, GridData.CENTER, false,
false);
closeButton.setLayoutData(gridData);
closeButton.setText("Close");
closeButton.setEnabled(false);
closeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogShell.dispose();
}
});
dialogShell.setDefaultButton(closeButton);
dialogShell.setImage(imageGF);
dialogShell.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
// TODO
}
});
dialogShell.setSize(550, 250);
dialogShell.setText("Processing....");
dialogShell.addShellListener(new ShellAdapter() {
public void shellActivated(ShellEvent event) {
myThread = new Thread(new Runnable() {
public void run() {
OutputStream out = new OutputStream() {
public void write(int b)
throws IOException {
Display.getDefault().asyncExec(new Runnable() {
finat int b_ = b;
public void run() {
logText.append(String
.valueOf((char) b_));
}
});
}
public void write(byte[] b,
int off, int len) {
Display.getDefault().asyncExec(new Runnable() {
final byte[] b_ = b;
final int off_ = off;
final int len_ = len;
public void run() {
logText.append(new String(b_,
off_, len_));
}
});
}
};
PrintStream ps = new PrintStream(out,
true);
System.setOut(ps);
System.setErr(ps);
try {
myHugeDummyJob(argumentList.split(","));
closeButton.setEnabled(true);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
});
myThread.start();
}
});
dialogShell.open();
// Setting the Out and Err stream back to the original...
System.setOut(origOutPS);
System.setErr(origErrPS);
}
});
|
|
|
Goto Forum:
Current Time: Thu Sep 26 06:10:51 GMT 2024
Powered by FUDForum. Page generated in 0.04130 seconds
|