1 package net.sf.openrocket.gui.components;
4 import java.awt.Dimension;
6 import java.awt.Rectangle;
8 import javax.swing.JEditorPane;
9 import javax.swing.JPanel;
10 import javax.swing.JScrollPane;
11 import javax.swing.ScrollPaneConstants;
12 import javax.swing.SwingUtilities;
14 public class DescriptionArea extends JScrollPane {
16 private final JEditorPane editorPane;
19 public DescriptionArea(int rows) {
23 public DescriptionArea(int rows, float size) {
27 public DescriptionArea(String text, int rows, float size) {
28 this(text, rows, size, true);
32 * Constructor with all options.
34 * @param text the text for the description area.
35 * @param rows the number of rows to set
36 * @param size the relative font size in points (positive or negative)
37 * @param opaque if <code>false</code> the background color will be set to the background color
38 * of a default JPanel (simulation non-opaque)
40 public DescriptionArea(String text, int rows, float size, boolean opaque) {
41 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
42 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
44 editorPane = new JEditorPane("text/html", "");
45 Font font = editorPane.getFont();
46 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
47 editorPane.setEditable(false);
50 Color bg = new JPanel().getBackground();
51 editorPane.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
55 // Calculate correct height
56 editorPane.setText("abc");
57 Dimension oneline = editorPane.getPreferredSize();
58 editorPane.setText("abc<br>def");
59 Dimension twolines = editorPane.getPreferredSize();
60 editorPane.setText("");
62 int lineheight = twolines.height - oneline.height;
63 int extraheight = oneline.height - lineheight;
65 Dimension dim = editorPane.getPreferredSize();
66 dim.height = lineheight * rows + extraheight + 2;
67 this.setPreferredSize(dim);
69 this.setViewportView(editorPane);
73 public void setText(String txt) {
74 editorPane.setText(txt);
75 editorPane.revalidate();
76 SwingUtilities.invokeLater(new Runnable() {
80 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
84 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));