I'm doing some graphics processing and HTML is a perfect choice for styling the displayed content. I'm trying to reuse swing's built in html support, and it works perfectly if I hard code the height passed to View.paint, but I can't figure out how to determine how tall the bounds of the rendered content would be at runtime given a specific width.
Graphics2D g = ... JLabel label = new JLabel("blah blah blah..."); View view = BasicHTML.createView(label, label.getText()); int minHeight = .... // Calculation magic goes here Rectangle htmlSize = new Rectangle(0, 0, 50, minHeight); g.setClip(htmlSize); view.paint(g, htmlSize);
If I ask the JLabel direction with getPreferredSize() it doesn't consider wrapping at all. If I try using a JEditorPane it returns a larger, but fixed size rectangle.
The height can't be calculated until the width is known. When dealing with Swing components I think you need to do something like:
Dimension size = component.getPreferredSize();
Or maybe you can use the concepts present in this posting: