Ivan Kanchev
Ivan Kanchev

Reputation: 33

Java JTextArea & JScrollPane not working

i am writing simple code with GUI that should have one text area which should be scrollable. So far so good. I created my frame and the text area and i can write in it ok. Next I created my ScrollPane and added the TextArea in it, then added the ScrollPane to the frame but nothing shows. Here is the code i have at this point:

    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 325, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    //textArea
    JTextArea textArea = new JTextArea();
    textArea.setEnabled(true);
    textArea.setEditable(true);
    JScrollPane scroll = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textArea.setBounds(10, 101, 272, 149);
    textArea.setWrapStyleWord(true);
    frame.getContentPane().add(scroll);

Upvotes: 1

Views: 1257

Answers (3)

Uhel
Uhel

Reputation: 3

I'm still learning however by looking at this case.

I have couple of issues:

  • There is issue with setting bounds of textArea

  • Layout of frame/container should not be set to null.

I have removed this sentence, and I tried this code, it displays desired textArea.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/*
* Tester class
*/
public class GuiTester extends JFrame{

public static void main(String[] args) {
    // create new instance of JFrame 
    GuiTester s = new GuiTester();
    // set the frame to be visible
    s.setVisible(true);
}
/**
 * Tester constructor calling method which initialise all widgets.
 */
GuiTester() {
    //
    invokeWidget();
}

/*
 * This code is yours, just removed setting up the values of container and did that straight on the frame.
 */
void invokeWidget() {
    setBounds(100, 100, 325, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    JTextArea textArea = new JTextArea();
    textArea.setEnabled(true);
    textArea.setEditable(true);
    JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    textArea.setWrapStyleWord(true);
    add(scroll);
}

}

This is the line that was causing issues, as well as setting layout Manager to null.

// textArea.setBounds(10, 101, 272, 149);

I hope I helped, and if I'm wrong please correct me as well.

Upvotes: 0

ahoxha
ahoxha

Reputation: 1938

You have to set the bounds to the component that is being added to the content pane of the frame. In this case, it should be: scroll.setBounds(10,101,271,149).

However, I strongly recommend to not use null layout. Use a layout manager of your choice, BorderLayout for instance. In this case you don't have to worry about the bounds, it will fit the frame size (it will resize when you change the size of the frame). Here's your example, tweaked a little bit:

import java.awt.BorderLayout;

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

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 325, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());

        // textArea
        JTextArea textArea = new JTextArea();
        textArea.setEnabled(true);
        textArea.setEditable(true);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        textArea.setWrapStyleWord(true);
        frame.getContentPane().add(scroll, BorderLayout.CENTER);

        frame.setVisible(true);
    }
}

Upvotes: 1

XtremeBaumer
XtremeBaumer

Reputation: 6435

change

 frame.getContentPane().setLayout(null);

to

 frame.getContentPane().setLayout(new BorderLayout());

and you are done

Upvotes: 3

Related Questions