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;
20 * Construct a description area with the specified number of rows, default description font size,
23 * @param rows the number of rows
25 public DescriptionArea(int rows) {
30 * Construct a description area with the specified number of rows and size, being opaque.
32 * @param rows the number of rows.
33 * @param size the font size difference compared to the default font size.
35 public DescriptionArea(int rows, float size) {
40 * Construct an opaque description area with the specified number of rows, size and text, being opaque.
42 * @param text the initial text.
43 * @param rows the number of rows.
44 * @param size the font size difference compared to the default font size.
46 public DescriptionArea(String text, int rows, float size) {
47 this(text, rows, size, true);
51 * Constructor with all options.
53 * @param text the text for the description area.
54 * @param rows the number of rows to set
55 * @param size the relative font size in points (positive or negative)
56 * @param opaque if <code>false</code> the background color will be set to the background color
57 * of a default JPanel (simulation non-opaque)
59 public DescriptionArea(String text, int rows, float size, boolean opaque) {
60 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
61 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
63 editorPane = new JEditorPane("text/html", "");
64 Font font = editorPane.getFont();
65 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
66 editorPane.setEditable(false);
69 Color bg = new JPanel().getBackground();
70 editorPane.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
74 // Calculate correct height
75 editorPane.setText("abc");
76 Dimension oneline = editorPane.getPreferredSize();
77 editorPane.setText("abc<br>def");
78 Dimension twolines = editorPane.getPreferredSize();
79 editorPane.setText("");
81 int lineheight = twolines.height - oneline.height;
82 int extraheight = oneline.height - lineheight;
84 Dimension dim = editorPane.getPreferredSize();
85 dim.height = lineheight * rows + extraheight + 2;
86 this.setPreferredSize(dim);
88 this.setViewportView(editorPane);
92 public void setText(String txt) {
93 editorPane.setText(txt);
94 editorPane.revalidate();
95 SwingUtilities.invokeLater(new Runnable() {
99 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
103 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));