-1

I am a beginer at programing and i wanted to add a scroll panel to a JTextArea so i tried to research tutorials online. i followed the examples but its not working can someone plz tell me what i am doing wrong. thank you so much

    public View(Model model) {
    this.model = model;
    setBounds(100,50, 800, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container c =  getContentPane();
    addDisplay(c);
    addButtons(c);
    addTxt(c);

}

private void addDisplay(Container c){
    JPanel p = new JPanel();
    addTxt2(p);
    addTxt(p);
    add(p, "North");

}

    private void addTxt(JPanel p){
        txt = new JTextArea(15, 35);
        txt.setBackground(Color.BLACK);
        txt.setForeground(Color.WHITE);
        txt.setEditable(true);  

        JScrollPane scroll= new JScrollPane (txt);
        p.add(scroll); 


}

2 Answers2

1

Always invoke revalidate and repaint after adding any components to a JPanel

p.add(scroll); 
p.revalidate();
p.repaint();

From the use of setBounds, it appears that there is no layout manager in use. Don't use absolute positioning (null layout). By default, components have a size of 0 x 0 so will not appear unless their size is set. A layout manager should be used here instead.

Post an SSCCE for better help sooner

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • i tried that but its still the same. the txt area is still just getting longer. – user2410760 May 22 '13 at 18:26
  • 1
    You're not using `null` layout by any chance? If so then components need to be sizes set explicitly. That's why its always better to use a layout manager – Reimeus May 22 '13 at 18:29
  • @user2410760 again repeating Please paste the SSCCE for better help, important is info if is added to already visible contianer or – mKorbel May 22 '13 at 18:49
0

You have to set the bounds of your scroll setBounds(int, int, int, int) and define the area of your JTextArea

Here's an example:

public class ScrollingTextArea extends JFrame {

    JTextArea txt = new JTextArea();
    JScrollPane scrolltxt = new JScrollPane(txt);

    public ScrollingTextArea() {

        setLayout(null);

        scrolltxt.setBounds(3, 3, 300, 200);
        add(scrolltxt);     
    }


    public static void main(String[] args) {

        ScrollingTextArea sta = new ScrollingTextArea();
        sta.setSize(313,233);
        sta.setTitle("Scrolling JTextArea with JScrollPane");
        sta.show();     
    }

}

I've found it here

SaintLike
  • 9,119
  • 11
  • 39
  • 69