0

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rodolfo
  • 573
  • 2
  • 8
  • 18
  • Have you tried without the `textArea.setLineWrap(true)` ? Wrapping text will avoid the component to get larger (horizontally). Although, I don't understand _"two empty sides"_. – xav Mar 21 '14 at 21:42
  • This tutorial should definitely help: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html – Nico Mar 21 '14 at 21:45
  • For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). A MCTaRE needs to be runnable, so that code would need a `main(String[])` to put it on-screen. – Andrew Thompson Mar 22 '14 at 02:00
  • *" I would like it to behave as in Microsoft Word where you have two empty sides and in the middle you have the textArea"* What does that look like? Create a screenshot and upload it to an image share site. Provide the link. – Andrew Thompson Mar 22 '14 at 02:11
  • Any progress on that screenshot? – Andrew Thompson Mar 23 '14 at 00:12

2 Answers2

2

Something like this?

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class TextAreaInScrollPane {

    JPanel gui = new JPanel(new BorderLayout());

    TextAreaInScrollPane() {
        // adjust columns/rows for different size
        JTextArea ta = new JTextArea(10,20); 
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);  // nicer
        JScrollPane jsp = new JScrollPane(
                ta, 
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        gui.add(jsp);
        
        // this is purely to show the bounds of the gui panel
        gui.setBackground(Color.RED);
        // adjust to need
        gui.setBorder(new EmptyBorder(5, 15, 15, 5));
    }
    
    public JComponent getGUI() {
        return gui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                TextAreaInScrollPane taisp = new TextAreaInScrollPane();

                JOptionPane.showMessageDialog(null, taisp.getGUI());
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

Tips

  1. There is no real reason to extend JScrollPane here. Just use an instance of one.
  2. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
  3. For better help sooner, post a MCTaRE (Minimal Complete Tested and Readable Example). Above is an example of a MCTaRE. It can be copy/pasted into an IDE or editor, compiled and run as-is.
  4. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER is better since we have the JTextArea set to line wrap.
  5. See also the comments in the MCTaRE for further tips.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Remove below line from your code:

textArea.setPreferredSize(new Dimension(400, 400));

This line is stopping textArea to grow.

Read here about Java - JPanel with margins and JTextArea inside.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Use `ScrollBarPolicy` as Needed in place of Always. – Braj Mar 21 '14 at 22:17
  • You can set the background color of spacer `Label` also. – Braj Mar 21 '14 at 22:18
  • `spacer1.setPreferredSize(new Dimension(50, 10));` There is no need for spacers, just use an empty border. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 22 '14 at 02:22