I am trying to insert a JTextArea
into a JScrollPanel
, I would like it to behave as in Microsoft Word where you have two empty sides and in the middle you have the textArea, in the code below the scroll panel does not seem to grow as you insert more text the two empty sides do not exists. What am I doing wrong?
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import java.awt.Dimension;
public class WorkArea extends JScrollPane{
public WorkArea(){
/* Scroll Panel settings*/
setBackground(new Color(60,60,60));
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
getViewport().setOpaque(false);
setBorder(null);
/* Text Area */
JTextArea textArea = new JTextArea("Hello World!");
textArea.setLineWrap(true);
textArea.setPreferredSize(new Dimension(400, 400));
/* Adding the textarea to the scrollPanel */
setViewportView(textArea);
}
}