Get the size of a Draw2d Label Figure

if I have a class with a private Label = new Label(""); in it and in some method i write:

private void setText(String text)
{
    this.label.setText(text);
    System.out.println("label size = " + this.label.getSize(0,0));
}

it will always print "label size = Dimension(0,0)". why is this? how can I obtain the size occupied by the label after setting its text? I also tried other solutions (here and method getTextBounds() as suggested in here ) but i either obtain again Dimension(0,0) or a NullPointerException, respectively.

do you have any suggestion? thanx :)

Answers


this.label.getPreferredSize() is what you're looking for. It returns the space your label would like to occupy. But at this point the label doesn't know yet what font to use, hence the NullPointerException. Once your figure tree has been set e.g. as the content of a FigureCanvas the font should be available. Alternatively, you could explicitly set a font before calling getPreferredSize().

To add a rounded rectangle around your label, like you requested in your comment, you could do the following:

RoundedRectangle rr = new RoundedRectangle();
rr.setBorder(new MarginBorder(4));
rr.setLayoutManager(new StackLayout());
rr.add(new Label("label text"));

Need Your Help

Drag and Drop UITableViewCell which contains custom UIView in cell.content

ios swift uitableview uiview

I am newbie on this, as titled, would like to implement custom drag & drop in UITableView, while each UITableViewCell content (cell.content?) will contains a custom UIView. E.g.

How can i find an element with a custom attribut that equals a specific value?

jquery loops custom-attributes each

I am trying to use a .each loop to find specific elements in a list that when a custom attribute, "pid", is not equal to 0 i can perform an action. How can i go about finding each element? so far i...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.