Search code examples
javaswingjpaneljscrollpanejtextarea

ScrollBar in JTextArea


I want to create a scroll bar in the textarea but If I set the JPanel Layout to null, the scrollbar won't show!

I tried

JScrollPane scrollbar1 = 
  new JScrollPane(
    ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

But didn't work because of the null layout.

Here is my current code:

import javax.swing.*;

import java.awt.*;
public class app extends JFrame {

    public static void main(String[] args)
    {
        new app();
    }

    public app()
    {
        this.setSize(400,400);
        this.setLocation(0,0);
        this.setResizable(false);
        this.setTitle("Application");           
        JPanel painel = new JPanel(null);           
        // Creating the Input
        JTextField tf1 = new JTextField("Some random text", 15);            
        tf1.setBounds(5,5,this.getWidth()-120,20);
        tf1.setColumns(10);
        tf1.setText("Omg");         
        painel.add(tf1);            
        // Creating the button          
        JButton button1 = new JButton("Send");          
        button1.setBounds(290, 5, 100, 19);         
        painel.add(button1);            
        // Creating the TextArea            
        JTextArea ta1 = new JTextArea(15, 20);
        JScrollPane scr = new JScrollPane();
        ta1.setBounds(5, 35, 385, 330);
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);         
        painel.add(ta1);
        this.add(painel);
        this.setVisible(true);
    }
}

I want to make it work correctly. If someone can help me, leave a comment below please!


Solution

  • I have corrected all the problems following is the working code. Please read comments for the changes.

    import javax.swing.*;
    
    import java.awt.*;
    
    public class app extends JFrame {
    
        public static void main(String[] args) {
            new app();
        }
    
        public app() {
            this.setSize(400, 400);
            this.setLocation(0, 0);
            this.setResizable(false);
            this.setTitle("Application");
            JPanel painel = new JPanel(null);
            // Creating the Input
            JTextField tf1 = new JTextField("Some random text", 15);
            tf1.setBounds(5, 5, this.getWidth() - 120, 20);
            tf1.setColumns(10);
            tf1.setText("Omg");
    
            // resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            painel.add(tf1);
            // Creating the button
            JButton button1 = new JButton("Send");
            button1.setBounds(290, 5, 100, 19);
            painel.add(button1);
            // Creating the TextArea
            JTextArea ta1 = new JTextArea(15, 20);
            JScrollPane scr = new JScrollPane(ta1,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane 
            ta1.setBounds(5, 35, 385, 330);
            ta1.setLineWrap(true);
            ta1.setWrapStyleWord(true);
            scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
            painel.add(scr);// Add you scroll pane to container
            this.add(painel);
            this.setVisible(true);
        }
    }
    

    EDIT. Please read tutorial from oracle on Java. And start using appropriate layout manager... Hope it helps