|
|
|
Re: size och swt labels [message #490432 is a reply to message #489751] |
Thu, 08 October 2009 15:55 |
larsk Messages: 22 Registered: October 2009 Location: Sweden |
Junior Member |
|
|
I tried this now, and it seems to resize depending on the content.
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();
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03301 seconds