Normally, in a JTextArea, the text starts in the upper left corner. I want it to be in the lower left corner. How can you do this?
(apologies if my handwriting is unreadable)
The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.
To append text to the end of the JTextArea document we can use the append(String str) method. This method does nothing if the document is null or the string is null or empty.
Using the read(Reader in, Object desc) method inherited from the JTextComponent allow us to populate a JTextArea with text content from a file. This example will show you how to do it. In this example we use an InputStreamReader to read a file from our application resource.
Constructs a new TextArea. A default model is set, the initial string is null, and rows/columns are set to 0.
You could anchor a JTextArea
to the BorderLayout.PAGE_END
location of a container and allow the text to scroll up.
public class BaseTextAreaDemo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Base JTextArea App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel textAreaPanel = getBaseTextArea();
JScrollPane scrollPane = new JScrollPane(textAreaPanel) {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 230);
}
};
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getBaseTextArea() {
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.append("bla bla bla\n");
textArea.append("new text here");
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(textArea.getBackground());
panel.setBorder(textArea.getBorder());
textArea.setBorder(null);
panel.add(textArea, BorderLayout.PAGE_END);
return panel;
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With