tags:

views:

52

answers:

2

Hi,

Is it possible to calculate the bounds of a htmlized String in Java Swing?

I tried using the BasicHtml.createHTMLView, but kept getting the same prefSpan; irrespective of the component font.

Regards,
Pavan

A: 

Is this going to be displayed in a JLabel?

Try SwingUtilities.layoutCompoundLabel(...)

Keilly
unfortunately ..it will be shown on a Jbutton
pavan
A: 

I was able to use BasicHtml.createHTMLView() to measure the changing preferred size of an HTML string on a JButton as follows:

for (int size = 1; size <= 7; size++)
{
    final String text = "<HTML><FONT SIZE='" + size + "'>Foo</FONT></HTML>";
    final JButton button = new JButton(text);
    final View htmlView = BasicHTML.createHTMLView(button, text);
    System.out.println("Preferred span: X=" + 
                       htmlView.getPreferredSpan(View.X_AXIS) +
                       ", Y=" + htmlView.getPreferredSpan(View.Y_AXIS));
}

This produced the following output:

Preferred span: X=15.0, Y=10.0
Preferred span: X=18.0, Y=14.0
Preferred span: X=20.0, Y=16.0
Preferred span: X=27.0, Y=19.0
Preferred span: X=33.0, Y=24.0
Preferred span: X=45.0, Y=32.0
Preferred span: X=66.0, Y=47.0

Am I missing something here? I'm not sure what the difference is between the case described in your question and the example I give here.

Joe Carnahan