0

I just want to know how to add a scroll bar to my text area. The text that is inputted by the reader goes on for more than the area goes and a scroll bar doesn't automatically come up.

Here is my entire code:

import java.awt.EventQueue;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class HGU3N extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                HGU3N frame = new HGU3N();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public HGU3N() throws IOException {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 895, 493);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTextArea textArea = new JTextArea();
    FileReader reader = new FileReader("Notes\\HumanGeoUnit3Notes.txt");
    textArea.read(reader,"Notes\\HumanGeoUnit3Notes.txt");
    textArea.setBounds(10, 11, 859, 432);
    textArea.setLineWrap(true);
    textArea.setEditable(false);
    contentPane.add(textArea);
}

}

  • Read the section from the Swing tutorial on [How to Use Text Areas](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html). Also read the section on `Layout Manager`. You should NOT be using a null layout. Keep a link to the tutorial handy for all the Swing basics. – camickr Nov 27 '16 at 02:31
  • `contentPane.setLayout(null);` A great way to stuff up the ability of a scroll pane to do its job. 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Nov 27 '16 at 04:53
  • .. 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Nov 27 '16 at 04:54

0 Answers0