1 package net.sf.openrocket.gui.components;
3 import java.awt.Dimension;
5 import java.awt.Rectangle;
7 import javax.swing.JEditorPane;
8 import javax.swing.JScrollPane;
9 import javax.swing.ScrollPaneConstants;
10 import javax.swing.SwingUtilities;
12 public class DescriptionArea extends JScrollPane {
14 private final JEditorPane editorPane;
17 public DescriptionArea(int rows) {
20 public DescriptionArea(int rows, float size) {
24 public DescriptionArea(String text, int rows, float size) {
25 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
26 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
28 editorPane = new JEditorPane("text/html", "");
29 Font font = editorPane.getFont();
30 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
31 editorPane.setEditable(false);
33 // Calculate correct height
34 editorPane.setText("abc");
35 Dimension oneline = editorPane.getPreferredSize();
36 editorPane.setText("abc<br>def");
37 Dimension twolines = editorPane.getPreferredSize();
38 editorPane.setText("");
40 int lineheight = twolines.height - oneline.height;
41 int extraheight = oneline.height - lineheight;
43 Dimension dim = editorPane.getPreferredSize();
44 dim.height = lineheight * rows + extraheight + 2;
45 this.setPreferredSize(dim);
47 this.setViewportView(editorPane);
51 public void setText(String txt) {
52 editorPane.setText(txt);
53 editorPane.revalidate();
54 SwingUtilities.invokeLater(new Runnable() {
58 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
62 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));