Hey guys,
The problem: I want to change where the text inside a JEditorPane is rendered. The default behavior is for text to start at the top of a JTextComponent, with whitespace filling in the rest of the JTextComponent underneath the text. I want to reverse this, so that the whitespace is displayed on the top, and the text stays at the bottom through resizing the window and appending more text.
The big picture: My end goal looks and behaves a bit like a chat room window. Text can be appended to the JTextComponent, but until the text fills up the Component (and a JScrollPane kicks in), the text should be displayed at the bottom of the JTextComponent, with whitespace at the top instead of the bottom..
What didn't work: I thought of simply padding the beginning of the text with a bunch of newlines, but that doesn't work right while resizing (not without calculating how many newlines I need every time the window is resized). Say I start out in a large window with a single line of text and a bunch of newlines at the beginning of that line. Then when I make the window smaller, the text will not stay at the bottom, it will instead go off the bottom of the window or JScrollPane. I've also tried looking around JEditorPane's UI API, and fiddling with some html (left in the SSCCE for your amusement).
The SSCCE:
import javax.swing.*;
public class TextTest {
public TextTest() {
JFrame frame = new JFrame("Text Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane textComponent = new JEditorPane();
textComponent.setContentType("text/html");
textComponent.setText(
"<P style=vertical-align:\"bottom\">" //
+ "The whitespace should be above this line ^^^"
+ "<br/><br/><br/><br/><br/>"
+ "Instead of below this line vvv</P>"
);
frame.add(textComponent);
frame.setSize(500, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
new TextTest();
}
}
As always, many thanks,
Kevin