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;
16 public DescriptionArea(int rows) {
20 public DescriptionArea(int rows, float size) {
21 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
22 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
24 editorPane = new JEditorPane("text/html", "");
25 Font font = editorPane.getFont();
26 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
27 editorPane.setEditable(false);
29 // Calculate correct height
30 editorPane.setText("abc");
31 Dimension oneline = editorPane.getPreferredSize();
32 editorPane.setText("abc<br>def");
33 Dimension twolines = editorPane.getPreferredSize();
34 editorPane.setText("");
36 int lineheight = twolines.height - oneline.height;
37 int extraheight = oneline.height - lineheight;
39 Dimension dim = editorPane.getPreferredSize();
40 dim.height = lineheight * rows + extraheight + 2;
41 this.setPreferredSize(dim);
43 this.setViewportView(editorPane);
46 public void setText(String txt) {
47 editorPane.setText(txt);
48 editorPane.revalidate();
49 SwingUtilities.invokeLater(new Runnable() {
53 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
57 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));