Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text's position in a JTextArea

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?

enter image description here

(apologies if my handwriting is unreadable)

like image 674
cvbattum Avatar asked May 28 '13 19:05

cvbattum


People also ask

Is JTextArea editable?

The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.

How do I add text to JTextArea?

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.

How do I read text file into JTextArea?

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.

What is the default found for JTextArea?

Constructs a new TextArea. A default model is set, the initial string is null, and rows/columns are set to 0.


1 Answers

You could anchor a JTextArea to the BorderLayout.PAGE_END location of a container and allow the text to scroll up.

Amazing Base JTextArea Application

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;
            }
        });
    }
}
like image 175
Reimeus Avatar answered Sep 30 '22 10:09

Reimeus