Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 321492

Summary: [Forms] Section.setText(..) has no visible effect
Product: [Eclipse Project] Platform Reporter: Henno Vermeulen <strider80>
Component: User AssistanceAssignee: platform-ua-inbox <platform-ua-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: cgold, remy.suen
Version: 3.5.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Henno Vermeulen CLA 2010-08-02 06:44:05 EDT
Build Identifier: 20090920-1017

When a section is displayed on the screen, calling setText(..) has no visible effect. As a workaround you can call layout() after you change the text. 

(I am trying to do databinding on the section text so it shows the number of elements in a table inside it. Calling SWTObservables.observeText(section) is illegal, but PojoObservables.observeValue(section, "text") should work for my needs, but it doesn't work because of this problem).


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * This snippet shows that changing the text in the Section does not update the
 * text in the UI, but it does after calling {@link Section#layout()} (or
 * implicitly by clicking the expander).
 * 
 * @author Henno Vermeulen
 */
public class SnippetCompositeWithSection extends Composite {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Section section;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public SnippetCompositeWithSection(Composite parent, int style) {
		super(parent, style);
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		setLayout(new GridLayout());

		section = toolkit.createSection(this, Section.EXPANDED
				| Section.TWISTIE | Section.TITLE_BAR);
		section
				.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
		toolkit.paintBordersFor(section);
		section.setText("Testtext");

		Button btnChangeText = toolkit.createButton(section, "Change text",
				SWT.NONE);
		btnChangeText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println("Changing Section text, current text = "
						+ section.getText());
				section.setText(section.getText() + "!");
				// Uncomment this for a workaround:
				// section.layout();
				System.out.println("Changed Section text = "
						+ section.getText());
			}
		});
		section.setClient(btnChangeText);
	}

	public static void main(String[] args) {
		final Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		new SnippetCompositeWithSection(shell, SWT.NONE);
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

	}
}


Reproducible: Always

Steps to Reproduce:
1. Show a UI with a section with a title text.
2. Call setText on it with a new title text.
3. The new text does not show although section.getText() returns the new text.
4. (Or run my SnippetCompositeWithSection, press the button, check what happens, then press the expander and see the text change)
Comment 1 Henno Vermeulen CLA 2010-08-02 06:45:35 EDT
Note that the problem is probably with it's super class ExpandableComposite.
Comment 2 Remy Suen CLA 2010-08-02 07:15:24 EDT
Forms is owned by UA.
Comment 3 Chris Goldthorpe CLA 2011-02-02 18:37:37 EST
The problem occurs when the new text is larger than the old text - the layout is not recomputed to allocate space for the longer label. It seems that ExpandableComposite.setText() needs to recompute the layout when it runs into the situation in the example although it should not be calling layout() for every call to setText().
Comment 4 Chris Goldthorpe CLA 2011-04-20 19:07:07 EDT

*** This bug has been marked as a duplicate of bug 168745 ***