Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » size och swt labels
size och swt labels [message #489751] Mon, 05 October 2009 18:57 Go to next message
larsk is currently offline larskFriend
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
Is there a way to make a swt label to change the size of it self depending on the font, textsize and the length of the text connected to the label? I know that this is possible to a Swing Jlabel, but is this possible to a swt label?
Re: size och swt labels [message #489944 is a reply to message #489751] Tue, 06 October 2009 14:43 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
If you are not using a layout in the Label's parent then use Control.pack()
to set its size to its preferred size based on its content.

If you are using a layout in the Label's parent then re-layout the parent
(Composite.layout()).

Grant


<larsk7@gmail.com> wrote in message news:hadfih$5b3$1@build.eclipse.org...
> Is there a way to make a swt label to change the size of it self depending
on the font, textsize and the length of the text connected to the label? I
know that this is possible to a Swing Jlabel, but is this possible to a swt
label?
Re: size och swt labels [message #490208 is a reply to message #489944] Wed, 07 October 2009 20:23 Go to previous messageGo to next message
larsk is currently offline larskFriend
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
I havent tried this yet but it looks great. Thanks!


Grant Gayed wrote on Tue, 06 October 2009 10:43
If you are not using a layout in the Label's parent then use Control.pack()
to set its size to its preferred size based on its content.

If you are using a layout in the Label's parent then re-layout the parent
(Composite.layout()).

Grant


<larsk7@gmail.com> wrote in message news:hadfih$5b3$1@build.eclipse.org...
> Is there a way to make a swt label to change the size of it self depending
on the font, textsize and the length of the text connected to the label? I
know that this is possible to a Swing Jlabel, but is this possible to a swt
label?

Re: size och swt labels [message #490432 is a reply to message #489751] Thu, 08 October 2009 15:55 Go to previous messageGo to next message
larsk is currently offline larskFriend
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
I tried this now, and it seems to resize depending on the content. Smile
But. Is it possible to get the size that the label is gonna get directly after that the size has been changed? I wrote a test application where I want the correct size to be written in the other label without being forced to push the button one more time which I have to do now.
Is this possible? I just want to know how big the label is gonna get when it is drawed on the screen.
I haven't tried to used a layout yet, can that make it easier to get the size?
Thanks in advance!



package swtlabeltest;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Main
{
    public static void main(String[] args)
    {
        final Display display = new Display();

        final Shell shell = new Shell(display);

        final Button button = new Button(shell, SWT.PUSH);
        button.setBounds(340, 150, 140, 40);
        button.setText("Change Size");

        final Text text = new Text(shell, SWT.BORDER);
        text.setBounds(100, 150, 140, 40);
        text.setText("12");

        final Label label = new Label(shell, SWT.MEDIUM);
        label.setLocation(540, 150);
        label.setBackground(new Color(display, 127, 178, 127));

        final Label labelOutput = new Label(shell, SWT.MEDIUM);
        labelOutput.setBounds(100, 350, 240, 100);
        labelOutput.setBackground(new Color(display, 127, 178, 127));

        button.addSelectionListener(new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent event)
            {
                label.setText("LabelSize: " + label.getSize().x + " " + label.getSize().y);

                Font fonten = new Font(display, new FontData("Arial", new Integer(text.getText()).intValue(), SWT.BOLD));

                label.pack();

                label.setFont(fonten);

                labelOutput.setText("Size: " + label.getSize() + "\r\nBounds: " + label.getBounds() + "");

                label.pack();
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent event)
            {
                
            }
        });

        shell.open();

        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
Re: size och swt labels [message #490497 is a reply to message #489751] Thu, 08 October 2009 20:23 Go to previous messageGo to next message
larsk is currently offline larskFriend
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
My mistake!
I seem to have written some errors in my code. I wrote the output to soon.
My code is now as follows and it works!:

            @Override
            public void widgetSelected(SelectionEvent event)
            {
                Font font = new Font(display, new FontData("Arial", new Integer(text2.getText()).intValue(), SWT.BOLD));
                
                label.setFont(fonten);

                label.pack();

                labelOutput.setText("size: " + label2.getSize() + "\r\nborderWidth: " + label2.getBorderWidth() + "\r\nbounds: " + label2.getBounds() + "");
            }
Re: size och swt labels [message #491247 is a reply to message #490497] Tue, 13 October 2009 18:46 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
You can use Control.computeSize(SWT.DEFAULT,SWT.DEFAULT) to get the
preferred size of a Control based on its content without actually changing
its bounds to this.

Grant


<larsk7@gmail.com> wrote in message news:halhn9$n22$1@build.eclipse.org...
> My mistake!
> I seem to have written some errors in my code. I wrote the output to soon.
> My code is now as follows and it works!:
>
>
> @Override
> public void widgetSelected(SelectionEvent event)
> {
> Font font = new Font(display, new FontData("Arial", new
Integer(text2.getText()).intValue(), SWT.BOLD));
>
> label.setFont(fonten);
>
> label.pack();
>
> labelOutput.setText("size: " + label2.getSize() +
"\r\nborderWidth: " + label2.getBorderWidth() + "\r\nbounds: " +
label2.getBounds() + "");
> }
Previous Topic:problem whit load image
Next Topic:Re: [BUG?] Performance problem with TreeViewers under Windows XP
Goto Forum:
  


Current Time: Mon Aug 26 00:09:18 GMT 2019

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

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

Back to the top