Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set jTextArea to have height that matches the size of a text it contains (to avoid scrollbars)

This problem looks trivial, but I can't find the solution.

When I create a form it contains a JTextArea. I want to put large constant text in it. If the text is say 1000 lines long, I want my JTextArea to be 1000 lines high (to be large enough to display the entire text without scrollbars). JTextArea is inside a panel which can have scrollbar so it is not a problem when it gets too large (there are two JTextArea in that panel.. something like in a diff tool). Does anybody knows how can I achieve this? Thanks.

like image 867
celicni Avatar asked Jun 20 '10 22:06

celicni


People also ask

How do I stop JTextArea from expanding?

You can use JTextArea#setLineWrap(true) by default is set to false. Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped.

What is the difference between JTextField and JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.

Is JTextArea editable?

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

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.


3 Answers

The BorderLayout will handle the scrollbar out of the box if you simply put your JTextAreain a JScrollPane before you add it to your JPanel. FlowLayout, on the other hand, does not. It will not display the scroll bar unless, as @Xorty intimates, you call setPreferedSize() on your JScrollPane and give it the dimension that you would like.

like image 83
akf Avatar answered Nov 14 '22 22:11

akf


You can also use something like this (limited width, height depending on text, useful when showing info messages):

  public JTextArea createTextAreaFitToText(String message, Dimension minimalSize){

        JTextArea aMessagePanel = new JTextArea();
        aMessagePanel.setText(message);

        /*for modelToView to work, the text area has to be sized. It doesn't matter if it's visible or not.*/
        aMessagePanel.setPreferredSize(minimalSize);
        aMessagePanel.setSize(minimalSize);            

        Rectangle r = aMessagePanel.modelToView(aMessagePanel.getDocument().getLength()); 

        Dimension d = new Dimension(minimalSize.width, r.y + r.height);
        aMessagePanel.setPreferredSize(d);
        return aMessagePanel;

}
like image 33
Taisin Avatar answered Nov 14 '22 22:11

Taisin


To increase or decrease the height of JTextArea. When a text is entered, call for getPreferredSize() of JTextArea- it'll give you the size needed to display the whole text. After that use setPrefferedSize() of JScrollPane to set the size of JTextArea

like image 35
sushant goel Avatar answered Nov 14 '22 21:11

sushant goel