2

Can anybody tell me what is the problem in following program? I want to fit JScrollPane on JtextArea but when I add it then JTextArea is not visible.

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

class Area extends JFrame
{
    private JTextArea ta;
    private JTextField tf;
    JScrollPane jp;

    public Area()
    {
       super("Text Area");
       tf=new JTextField();
       tf.setBounds(100,350,300,30);
       add(tf);
       ta=new JTextArea();
       ta.setBounds(100,100,300,200);
       jp= new JScrollPane(ta);
       add(jp);
       setLayout(null);
       setSize(500,500);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public static void main(String...s)
   {
      new Area();
   }
}
kiheru
  • 6,588
  • 25
  • 31
AR7
  • 61
  • 2
  • 2
  • 7
  • `Don't use a null layout!` - Swing was designed to be used with layout managers for too many reasons to list here. – camickr Oct 06 '13 at 18:44

3 Answers3

11

I see several problems:

  • Don't use a null layout; do use a real layout.

  • The default layout of JFrame is BorderLayout; the default position is CENTER; only one component can occupy a position at a time; the example below uses NORTH & CENTER.

  • Use the appropriate constructor parameters to size the text components initially.

  • The scrollbar will appear automatically whenever the scrollpane is smaller than the enclosed component; resize the frame to see the effect.

  • As shown here, the frame's size is made smaller for effect.

  • See also Initial Threads.

image

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

/** @see https://stackoverflow.com/a/19215436/230513 */
public class Area extends JFrame {

    public Area() {
        super("Text Area");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField tf = new JTextField(12);
        add(tf, BorderLayout.NORTH);
        JTextArea ta = new JTextArea(24, 12);
        JScrollPane jp = new JScrollPane(ta);
        add(jp, BorderLayout.CENTER);
        pack();
        // arbitrary size to make vertical scrollbar appear
        setSize(240, 240);
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Area();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Try this:

public Area()
{
   super("Text Area");
   tf=new JTextField();
   tf.setBounds(100,350,300,30);
   add(tf);
   ta=new JTextArea();
   jp= new JScrollPane(ta);
   jp.setBounds(5, 5, 100, 100);
   add(jp);
   setLayout(null);
   setSize(500,500);
   setVisible(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

You have to use setBounds on JScrollPane, not on JTextArea

Sylwek
  • 856
  • 1
  • 9
  • 24
  • Oh, thank you very much. I was searching about and this "You have to use setBounds on JScrollPane, not on JTextArea" solved my problem. – Commentator Dec 05 '16 at 04:57
0

sounds like its added but its not shown because of the policy try this:

  jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Foo Bar User
  • 2,401
  • 3
  • 20
  • 26
  • It won't work. He has to use `setBounds` method on `jp`, not on `JTextArea`, because when he adds `textArea` to `scrollPane`, `scrollPane` is parent – Sylwek Oct 06 '13 at 18:26
  • 2
    @SylwekDerkacz actualy i run his code and theres not text area there. because of setLayoutO(null), to be honest i never used setBounds because i disagree with setBounds anyway. – Foo Bar User Oct 06 '13 at 18:32