I am trying to wrap text inside a label depending on the parent composite's width, and automatically re-align the position of subsequent UI elements. I'm using GridLayout to do this. In test/preview mode, everything looks fine, but at runtime, text is always displayed in a single row, and is not wrapped.
What am I doing wrong? How can my code achieve the same behavior the test/preview mode creates?
The following self-contained code fragment is an application reproducing the problem.
Kind Regards
Jan
- Code: Select all
package de.cuc.test;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* Demonstration for label wrapping issue.
*
* This works as intended in SWT Designer Editor and Test/Preview mode - i.e.
* when the window is resized, the wrapping and layout changes to accomodate for
* the new widget sizes.
*
* However, when this is run as an application, text is displayed as a single
* line.
*
* How can I avoid this, and get the Test/Preview mode behavior for applications
* as well?
*
*/
public class ResizeLabelWrapTestGridLayout1 extends ApplicationWindow {
public ResizeLabelWrapTestGridLayout1() {
super(null);
createActions();
addToolBar(SWT.FLAT | SWT.WRAP);
addMenuBar();
addStatusLine();
}
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
final Composite composite = new Composite(container, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(new GridLayout());
final Label label = new Label(composite, SWT.NONE);
label.setText("Headline");
final Label label1 = new Label(composite, SWT.WRAP);
label1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
label1
.setText("This should be wrapped. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. Yadda yadda. ");
final Composite composite_1 = new Composite(container, SWT.NONE);
composite_1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite_1.setLayout(new GridLayout());
final Label label2 = new Label(composite_1, SWT.WRAP);
label2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
label2
.setText("This should appear directly below the wrapped text and should automatically be re-aligned when the other label's height changes.");
return container;
}
private void createActions() {
}
protected MenuManager createMenuManager() {
MenuManager result = new MenuManager("menu");
return result;
}
protected ToolBarManager createToolBarManager(int style) {
ToolBarManager toolBarManager = new ToolBarManager(style);
return toolBarManager;
}
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
statusLineManager.setMessage(null, "Test.");
return statusLineManager;
}
public static void main(String args[]) {
try {
ResizeLabelWrapTestGridLayout1 window = new ResizeLabelWrapTestGridLayout1();
window.setBlockOnOpen(true);
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("Test");
}
protected Point getInitialSize() {
return new Point(500, 375);
}
}