Ich hab eine Shell, darin ein GridLayout und darin ein Label sowie Progressbar.
Jetzt wird der Text des lblProgress zur Laufzeit grösser als der vorgesehene .widthHint = 350 und das Grid müsste daher komplett neu Layoutet werden.
Ein "shlProgress.layout(true);" oder "shlProgress.layout()" bringt aber nichts, alles bleibt auf alter Grösse.
Wo ist mein Denkfehler?
Kompletter Source zum spielen ist unten.
Bernd
Code:
shlProgress = new Shell(SWT.BORDER | SWT.TITLE | SWT.PRIMARY_MODAL);
shlProgress.setSize(318, 130);
shlProgress.setMinimumSize(new Point(100, 50));
shlProgress.setText("Progress");
GridLayout gl_shlProgress = new GridLayout(1, false);
gl_shlProgress.marginRight = 25;
gl_shlProgress.marginBottom = 25;
gl_shlProgress.marginTop = 25;
gl_shlProgress.marginLeft = 25;
shlProgress.setLayout(gl_shlProgress);
// ###
lblProgress = new Label(shlProgress, SWT.NONE);
GridData gd_lblProgress = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
gd_lblProgress.widthHint = 350;
lblProgress.setLayoutData(gd_lblProgress);
// ###
pbProgress = new ProgressBar(shlProgress, SWT.NONE);
pbProgress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Jetzt wird der Text des lblProgress zur Laufzeit grösser als der vorgesehene .widthHint = 350 und das Grid müsste daher komplett neu Layoutet werden.
Ein "shlProgress.layout(true);" oder "shlProgress.layout()" bringt aber nichts, alles bleibt auf alter Grösse.
Wo ist mein Denkfehler?
Kompletter Source zum spielen ist unten.
Bernd
Code:
package de.zwanzigeins.tagtaggo.widgets;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.graphics.*;
public class ProgressMessageBox {
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
ProgressMessageBox window = new ProgressMessageBox(Display.getDefault().getActiveShell());
window.setText("ich bin eine überschrift");
window.setMessage("message hier");
window.open();
String s = "lala";
for (int i = 0; i < 100; i++) {
Thread.sleep(100);
window.setMessage("Zähler " + i + " " + s);
window.setProgress(0, i, 100);
s = s + "gugu!";
}
window.close();
} catch (Exception e) {
e.printStackTrace();
}
}
protected Shell shlProgress;
protected Label lblProgress;
protected ProgressBar pbProgress;
private String strTitle = "";
private String strMsg = "";
private int intLwb = 0;
private int intVal = 0;
private int intUpb = 0;
private final Shell shlParent;
public ProgressMessageBox(Shell parent) {
shlParent = parent;
}
public void close() {
if (shlProgress != null) {
shlProgress.close();
run_dispatcher();
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlProgress = new Shell(SWT.BORDER | SWT.TITLE | SWT.PRIMARY_MODAL);
shlProgress.setSize(318, 130);
shlProgress.setMinimumSize(new Point(100, 50));
shlProgress.setText("Progress");
GridLayout gl_shlProgress = new GridLayout(1, false);
gl_shlProgress.marginRight = 25;
gl_shlProgress.marginBottom = 25;
gl_shlProgress.marginTop = 25;
gl_shlProgress.marginLeft = 25;
shlProgress.setLayout(gl_shlProgress);
// ###
lblProgress = new Label(shlProgress, SWT.NONE);
GridData gd_lblProgress = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
gd_lblProgress.widthHint = 350;
lblProgress.setLayoutData(gd_lblProgress);
// ###
pbProgress = new ProgressBar(shlProgress, SWT.NONE);
pbProgress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
// ... selbst
pbProgress.setVisible(false);
shlProgress.addListener(SWT.Close, new Listener() {
public void handleEvent(Event e) {
e.doit = false; // disable close
}
});
}
/**
* Open the window.
*/
public void open() {
createContents();
shlProgress.open();
shlProgress.pack();
// ... selbst
if (shlParent != null) {
Point pp = shlParent.getLocation();
Rectangle pr = shlParent.getBounds();
shlProgress.setLocation(pp.x + pr.width / 2 - shlProgress.getBounds().width / 2, pp.y + pr.height / 2 - shlProgress.getBounds().height / 2);
}
setText(strTitle);
setMessage(strMsg);
setProgress(intLwb, intVal, intUpb);
}
private void run_dispatcher() {
while (shlProgress.getDisplay().readAndDispatch()) {}
}
public void setMessage(String str) {
strMsg = str;
if (shlProgress != null) {
lblProgress.setText(strMsg);
shlProgress.layout();
run_dispatcher();
}
}
public void setProgress(int lwb, int val, int upb) {
intLwb = lwb;
intUpb = upb;
intVal = val;
if (shlProgress != null) {
pbProgress.setMinimum(intLwb);
pbProgress.setMaximum(intUpb);
pbProgress.setSelection(intVal);
if (intVal != 0 || intLwb != 0 || intUpb != 0) pbProgress.setVisible(true);
run_dispatcher();
}
}
public void setText(String str) {
strTitle = str;
if (shlProgress != null) {
shlProgress.setText(strTitle);
run_dispatcher();
}
}
}