0

I am trying to put a text area onto a dialog box using Java Swing. I have a problem of setting the size of this JTextArea. The width of the text area is always equal to the whole width of the window and stretches with the window if I resize it.

private void arrangeComponents() {
    JTextArea textArea = new JTextArea();
    JPanel outerPanel = new JPanel();
    outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.PAGE_AXIS));

    JScrollPane scrollPane = new JScrollPane(textArea);
    outerPanel.add(scrollPane, BorderLayout.CENTER);

    Container contentPane = getContentPane();
    contentPane.add(outerPanel, BorderLayout.CENTER);
}

I want the JTextArea to be horizontally aligned to the centre of the window and does not change its size.

What did I do wrong?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kevin
  • 5,972
  • 17
  • 63
  • 87

5 Answers5

7

Use the JTextArea(int rows, int columns) constructor that specifies rows and columns, as shown here, and don't neglect to pack() the enclosing Window.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2
outerPanel.add(scrollPane, BorderLayout.CENTER);

A BoxLayout doesn't take constraints, so the BorderLayout.CENTER is unnecessary.

The problem is that a BoxLayout respects the maximum size of the component which for a scrollpane is set very large.

Instead of using a BoxLayout, just use a panel with a FlowLayout.

Run the example below to see what you are currently doing. Then comment out the setLayout(...) statement and run again. By default the panel uses a FlowLayout so you will get what you want.

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
          setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));

          JTextArea textArea = new JTextArea(5, 30);
          JScrollPane scrollPane = new JScrollPane(textArea);
          //scrollPane.setMaximumSize( scrollPane.getPreferredSize() );

          add(scrollPane);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Or if you really want to keep the BoxLayout then leave keep the setLayout(...) statement and then set the maximum size equal to the preffered size. Many people will say you should never invoke a "setXXX()" method directly and instead you should override the setMaximumSize() method of the scrollpane to just return the preferred size.

Note, when testing these two solutions make sure you make the window smaller than the scrollpane to see how each layout works differently.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

i found this from a simple coding site. This code sample may be useful for you.

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JTextAreaTest {
    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("JTextArea Test");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String text = "A JTextArea object represents a multiline area for displaying text. "
                + "You can change the number of lines that can be displayed at a time, "
                + "as well as the number of columns. You can wrap lines and words too. "
                + "You can also put your JTextArea in a JScrollPane to make it scrollable.";
        JTextArea textAreal = new JTextArea(text, 5, 10);
        textAreal.setPreferredSize(new Dimension(100, 100));
        JTextArea textArea2 = new JTextArea(text, 5, 10);
        textArea2.setPreferredSize(new Dimension(100, 100));
        JScrollPane scrollPane = new JScrollPane(textArea2,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        textAreal.setLineWrap(true);
        textArea2.setLineWrap(true);
        frame.add(textAreal);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }
}
hakki
  • 6,181
  • 6
  • 62
  • 106
  • +1 for a side-by-side comparison [sscce](http://sscce.org/). My example needs a scroll pane, and yours could use a little `invokeLater()`. :-) – trashgod Feb 16 '13 at 19:24
  • 2
    -1 for using setPreferredSize() which are just a bunch on random numbers that don't take into account the font which is use to determine the line height of each row. The setPreferredSize() method just overrides the row, columns values you tried to specify when you created the text area. – camickr Feb 16 '13 at 19:30
  • @camickr raises a crucial point that I'd overlooked; more [here](http://stackoverflow.com/q/7229226/230513). Also, beware of [*non-resizable containers*](http://stackoverflow.com/a/12532237/230513), and see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Feb 16 '13 at 19:35
1

Just call that method for ur text area: setLineWrap(true);

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
0

If JTextArea is initializated JTextArea text = new JTextArea(int rows, int columns)

you just call the method text.setLineWrap(true)

then text'size is fixed.