4

I'm trying to display a JTextArea within a JScrollPane, but I just get an empty frame when I run my (simplified) program:

import java.awt.Container;
import java.awt.Dimension;    
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class ScrollPaneTest extends JFrame {
    private Container myCP; 
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setSize(500, 500);
        setLocation(100, 100);
        myCP = this.getContentPane();
        myCP.setLayout(null);

        resultsTA = new JTextArea("Blah blah");
        scrollPane = new JScrollPane(resultsTA,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        scrollPane.setLocation(100, 300);
        myCP.add(scrollPane);

        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new ScrollPaneTest();
    }
}

I use the null LayoutManager to be consistent with the textbook I'm teaching from.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
  • 3
    if your textbook insists on null LayoutManager more often than to teach the manual positioning and sizing once (and by that showing huch much work it is, as you experience :-) throw it into the bin - it's useless ;-) A so-called null-layout is a no-no-never – kleopatra Apr 07 '11 at 07:53
  • The textbook is by a senior member of my department and good friend. :-) I'm just teaching the course for her this once. – Ellen Spertus Apr 07 '11 at 13:50

2 Answers2

7

This will work:

public class ScrollPaneTest extends JFrame {
    private Container myCP; 
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setSize(500, 500);
        setLocation(100, 100);
        myCP = this.getContentPane();
        myCP.setLayout(null);

        resultsTA = new JTextArea("Blah blah");
        resultsTA.setBounds(10, 10, 150, 30);

        scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        scrollPane.setBounds(0, 0, 500, 500);

        myCP.add(scrollPane);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new ScrollPaneTest();
    }
}

If you are using null layout then you must specify bounds.


Edit

setBounds() method coveres the task of setLocation() method.

for example, setBounds(x,y,w,h);

first 2 will set the x/y location of that component with respect to its container. second 2(w/h) will set the size of that component.

in other words:-

  1. setBounds(int x, int y, int witdh, int height) – Sets the component’s size and location
  2. setLocation(int x, int y) – Sets the component’s location
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
1

I have to agree with kleopatra's comment on this one.

Here is a variant of Harry Joy's code that uses layouts. It appears on-screen almost the same size as the original GUI but is resizable. It will also adapt easily to different PLAFs, default font sizes etc. (though it might end up a different size on-screen), whereas something with a null layout will not.

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

public class ScrollPaneTest extends JFrame {
    private Container myCP;
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100, 100);
        myCP = this.getContentPane();

        resultsTA = new JTextArea("Blah blah", 28, 43);

        scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        myCP.add(scrollPane);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new ScrollPaneTest();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433