5

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
celicni
  • 420
  • 3
  • 7
  • 16
  • Well, I've tried to find some option in NetBeans to set it to automatically expand, and I've tried to find a method to programatically set this behavior, but without success (setSize methods don't change the size..) – celicni Jun 21 '10 at 00:06

4 Answers4

6

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.

akf
  • 38,619
  • 8
  • 86
  • 96
4

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;

}
Taisin
  • 491
  • 2
  • 6
1

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

sushant goel
  • 821
  • 6
  • 8
0

Well, first of all : it's JTextArea not jTextArea.

Now - put JTextArea into JScrollPane. When a text is entered, call for getPreferedSize() of JScrollPane - it'll give you precise size needed to display whole text. Also, I never use other LayoutManager than 'Free Design' from NetBeans Swing builder - so I am not sure how other LayoutManagers will behave there.

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • Thanks. But first thing, not depending on the JTextArea/JScrollPane height, jScrollPane1.getPreferredSize().getHeight() returns 91.0. What does that mean? And second thing, what whould I do with jScrollPane1.getPreferredSize()? if I pass it into jTextArea().setSize() or jTextArea.setMinimumSize, there is no effect. If I pass it into jTextArea.setPreferredSize(), the effect is that the scollbar is gone, but JTextArea has not expanded to fit the text (therefore I can't access the entire text). – celicni Jun 20 '10 at 23:30