Hans Schwimmer
Hans Schwimmer

Reputation: 139

Why there is no JScrollPanel in my JTextArea?

I am trying to add JScrollPane to my JTextArea and it doesn't work. Could someone tell me what is wrong? I would like to have only the horizontal one

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Program extends JFrame{

    JButton button1, button2;
    JTextArea textArea1;
    JScrollPane scroller;
    public static String directory, nameOfFile, finalPath;

    public static void main(String[] args){

        new Program();

    }

    public Program(){

        this.setSize(500,500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("swinging");

        JPanel thePanel = new JPanel();
        thePanel.setLayout(null);
        thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        button1 = new JButton("Wybierz plik:");
        button2 = new JButton("Dodaj");

        final JTextField text = new JTextField(24);
        textArea1 = new JTextArea();
        textArea1.setEditable(true);                                        
        textArea1.setLineWrap(true);
        textArea1.setPreferredSize(new Dimension(490,200));

        scroller = new JScrollPane(textArea1);
         scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        thePanel.add(button1);
        thePanel.add(text);
        thePanel.add(button2);

        thePanel.add(textArea1);
        thePanel.add(scroller);     
        this.add(thePanel);
        this.setVisible(true);          
    }
}

All I see after adding it is some small square below the textarea. Thank you for feedback

Upvotes: 1

Views: 61

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Your problem was due to your adding the JScrollPane and the JTextArea both to the thePanel JPanel, and so you see both: a JTextArea without JScrollPanes and an empty JScrollPane.

  • Don't add add the textArea itself to the JScrollPane and also to a JPanel, since you can only add a component to one container. Instead, add it to one component, the JScrollPane (actually you're adding it to its viewport view, but if you pass it into the JScrollPane's constructor, then you're doing this), and then add that JScrollPane to something else.
  • Also, NEVER set a text component's preferred size. You constrain the JTextArea so that it will never expand as more text is added, and so you'll never see scrollbars and not see text beyond this size. Set the visible columns and rows instead. e.g., textArea1 = new JTextArea(rows, columns);

Note that this doesn't make much sense:

thePanel.setLayout(null);
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

I'm not sure what you are trying to do here since 1) you want to set a container's layout only once, and 2) in general you will want to avoid use of null layouts.

For example:

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

public class MyProgram extends JPanel {
    private static final int T_FIELD_COLS = 20;
    private static final int TXT_AREA_ROWS = 15;
    private static final int TXT_AREA_COLS = 20;
    private JButton button1 = new JButton("Button 1");
    private JButton button2 = new JButton("Button 2");
    private JTextField textField = new JTextField(T_FIELD_COLS);
    private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);

    public MyProgram() {
        // Create a JPanel to hold your top line of components
        JPanel topPanel = new JPanel();

        int gap = 3;
        topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));

        // set this JPanel's layout. Here I use BoxLayout.
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
        topPanel.add(button1);
        topPanel.add(Box.createHorizontalStrut(gap));
        topPanel.add(textField);
        topPanel.add(Box.createHorizontalStrut(gap));
        topPanel.add(button2);

        // so the JTextArea will wrap words
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        // add the JTextArea to the JScrollPane's viewport:
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        // set the layout of the main JPanel. 
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
    }

    private static void createAndShowGui() {
        MyProgram mainPanel = new MyProgram();

        JFrame frame = new JFrame("My Program");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack(); // don't set the JFrame's size, preferred size or bounds
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // start your program on the event thread
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Upvotes: 5

Madushan Perera
Madushan Perera

Reputation: 2598

Try this :

textArea1 = new JTextArea();

textArea1.setColumns(20);

textArea1.setRows(5);

scroller.setViewportView(textArea1);

Upvotes: 0

Related Questions