Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to update the width of a label when its value change(I'm looking for the best practice to update the width of a label according to its content)
How to update the width of a label when its value change [message #514133] Fri, 12 February 2010 14:40 Go to next message
H.ORTIZ  is currently offline H.ORTIZ Friend
Messages: 22
Registered: July 2009
Junior Member
Hi,

When a value displayed is a label changes and needs more pixels to display (for example the value changes from 1234 to 12345678), which is the best way to trigger its "redraw" ?

I think my problem will be clearer with the following snippet: when I click on the button "Set value to 12345678", the label value changes but how to refresh it avoiding a total refresh of the shell ?

Thanks,
Helene


public class LabelWidthChanged {

public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());


final Label label = new Label(shell, SWT.LEFT );
label.setText("1234");
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.CENTER;
gridData.verticalAlignment = SWT.CENTER;
gridData.grabExcessVerticalSpace = true;
label.setLayoutData(gridData);


Button button = new Button(shell, SWT.PUSH);
button.setText("Set value to 12345678");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
label.setText("12345678");
shell.layout(); // <--- Is it the right manner ?
}
});

shell.setSize(400, 250);
shell.open();

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

display.dispose();
}
}
Re: How to update the width of a label when its value change [message #514369 is a reply to message #514133] Mon, 15 February 2010 06:18 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
label.pack() would be most appropriate...


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: How to update the width of a label when its value change [message #514760 is a reply to message #514369] Tue, 16 February 2010 14:46 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Note that label.pack() will size the Label in the snippet correctly but it
will not be centered, since this is performed by the parent's layout.
Calling layout() on the parent is the best thing to do whenever a layout is
being used, and pack() on the control itself when a layout is not being
used.

Grant


"vijay" <vijay.rajonline@gmail.com> wrote in message
news:hlaov5$se7$1@build.eclipse.org...
> label.pack() would be most appropriate...
>
Re: How to update the width of a label when its value change [message #516304 is a reply to message #514760] Tue, 23 February 2010 15:04 Go to previous message
H.ORTIZ  is currently offline H.ORTIZ Friend
Messages: 22
Registered: July 2009
Junior Member
ok ! Thanks a lot to both of you for your answers,
Helene
Previous Topic:Wiggly Mouse Problem
Next Topic:Question on asyncexec and updating UI Elements
Goto Forum:
  


Current Time: Sat Dec 07 17:07:13 GMT 2024

Powered by FUDForum. Page generated in 0.03563 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top