thomasdclark
thomasdclark

Reputation: 484

Changing the size of a JTextArea

I am trying to create a simple GUI with two JTextAreas on top of each other. I have been able to accomplish this, but I would like the bottom JTextArea to be about half the size of the top, and have not been able to figure out how to accomplish this. I have tried changing the number of rows in the JTextArea, but that has not worked. Here is my code:

public final class View extends JFrame implements ViewInterface {

/**
 * Controller object.
 */
private Controller controller;

/**
 * Constants
 */
private static final int LINES_IN_TOP_TEXT = 4,
        LINE_LENGTHS_IN_TOP_TEXT = 8, LINES_IN_BOTTOM_TEXT = 1,
        LINE_LENGTHS_IN_BOTTOM_TEXT = 8, ROWS_IN_THIS_GRID = 2,
        COLUMNS_IN_THIS_GRID = 1;

/**
 * Text areas.
 */
private final JTextArea topText;

private final JTextArea bottomText;

/**
 * Default constructor.
 */
public View() {

    /*
     * Call JFrame superclass with title
     */
    super("Two JTextAreas");

    /*
     * Create widgets
     */
    this.topText = new JTextArea("", LINES_IN_TOP_TEXT,
            LINE_LENGTHS_IN_TOP_TEXT);

    this.bottomText = new JTextArea("", LINES_IN_BOTTOM_TEXT,
            LINE_LENGTHS_IN_BOTTOM_TEXT);

    /*
     * Customize font of top text
     */
    Font topFont = new Font("TimeRoman", Font.BOLD, 30);
    this.topText.setFont(topFont);
    this.topText.setForeground(Color.WHITE);
    this.topText.setBackground(Color.RED);

    /*
     * Customize font of bottom text
     */
    Font bottomFont = new Font("TimeRoman", Font.BOLD, 12);
    this.bottomText.setFont(bottomFont);
    this.bottomText.setForeground(Color.WHITE);
    this.bottomText.setBackground(Color.RED);

    /*
     * Text areas should wrap lines, and outputText should be read-only
     */
    this.topText.setEditable(false);
    this.topText.setLineWrap(true);
    this.topText.setWrapStyleWord(true);
    this.bottomText.setEditable(false);
    this.bottomText.setLineWrap(true);
    this.bottomText.setWrapStyleWord(true);

    /*
     * Create scroll panes for the text areas
     */
    JScrollPane topTextScrollPane = new JScrollPane(this.topText);
    JScrollPane bottomTextScrollPane = new JScrollPane(this.bottomText);

    /*
     * Organize main window using grid layout
     */
    this.setLayout(new GridLayout(ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID));

    /*
     * Add scroll panes and button panel to main window
     */
    this.add(topTextScrollPane);
    this.add(bottomTextScrollPane);

    /*
     * Start the main application window
     */
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}
}

Anyone know how I can fix this? Thanks!

Upvotes: 2

Views: 472

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Set the JTextArea rows or columns to the preferred size you desire. If one is to have have the height as the other, than give it half as many rows.

Also, your layout, the GridLayout, will ignore the row count and will instead make all components it holds the same size, essentially the largest size to accommodate all components. Instead use a better behaved layout manager such as a BoxLayout. e.g.,

  // this.setLayout(new GridLayout(ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID));
  setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));

Upvotes: 3

Andie2302
Andie2302

Reputation: 4897

You can use Java Layout Manager:

Or use setSize and setPreferedSize

Upvotes: 2

Related Questions