Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [platform-swt-dev] SWT 101

Hi Joe

>>>>> Except that if you run the code as it stands, the output label never
gets
>>>>> updated unless you resize the window.  The only way I've found to
correct
>>>>> this behavior is to either call pack() every time (which is really
ugly - go
>>>>> ahead, try it!  Watch what happens to the input field), or to use the
>>>>> commented line which initializes the label to a bunch of blanks
(thereby
>>>>> forcing some default width perhaps?).

The reason for this behaviour is not really that strange. Setting the text
does not change the size of the label or of its container. You can clearly
see it in the code that handles setting the text for a label:

public void setText (String string) {
	/* not interesting */
	TCHAR buffer = new TCHAR (getCodePage (), string, true);
	OS.SetWindowText (handle, buffer);
}

When you debug it, you can see that buffer contains your text and is sent to
the window. So why don't you see it? Well, if you carefully look at the
text, you can see that the size of your label is not affected. If the
initial width of your label is zero, it will remain zero. Then you think
that you will help to send "pack()" to your label? No, because that changes
the size of your label, but not of your shell. Since your shell maintains
its width, your label still doesn't grow. You've guessed it. The way to
solve it is to send a "pack()" to the label's container, that is, its
window.

  private void doFind() {
    String desc = getDescription(f1.getText());
    if (desc == null)
    {
      f1.setForeground(white);
      f1.setBackground(red);
      f2.setText("Not Found");
      f2.setForeground(red);
    }
    else
    {
      f1.setForeground(null);
      f1.setBackground(null);
      f2.setText(desc);
      f2.setForeground(null);
    }
    shell.pack();
  }

But then, you will notice something else annoying. You actually don't want
the window to change the window's size every time the user presses "find".
It's really ugly. Given the layout management you have chosen, the label
size will be zero, because you create it empty. So what you really need to
do, is to set the minimumsize of the label. Then you don't need all these
things.

Since the problem would be solved by giving the label the same size as the
textbox above it, you can solve the problem by filling out the griddata
object for the label:

		GridData layoutData=new GridData();
		layoutData.grabExcessHorizontalSpace=true;
		layoutData.horizontalAlignment=GridData.FILL;

In such case, you don't need pack(), nor update(), nor anything else.

You've just got bitten by the intricacies of advanced layout management in
SWT. I think that's normal when you get started with it. As soon as you
understand it, however, you'll probably get the impression that it is
natural.

Greetings
Erik



Back to the top