John Black
John Black

Reputation: 305

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!

Upvotes: 3

Views: 13209

Answers (4)

Jabir
Jabir

Reputation: 2886

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

Upvotes: 3

trashgod
trashgod

Reputation: 205875

Here's a basic example of many of @mKorbels points. Note how the default layout of JPanel(), FlowLayout(), uses the preferred size of its components. The call to f.setSize() is optional to force the scrollbar to appear.

image

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

public class App {

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

    public App() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Application");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                JTextField tf1 = new JTextField("Some random text", 15);
                tf1.setColumns(10);
                tf1.setText("Omg");
                panel.add(tf1);
                JButton button1 = new JButton("Send");
                panel.add(button1);
                JTextArea ta = new JTextArea(15, 20);
                JScrollPane scr = new JScrollPane(ta);
                scr.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                ta.setLineWrap(true);
                ta.setWrapStyleWord(true);
                f.add(panel, BorderLayout.NORTH);
                f.add(scr, BorderLayout.CENTER);
                f.pack();
                Dimension d = scr.getPreferredSize();
                f.setSize(d.width, d.height);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

If someone can help me, leave a comment below please!

  • why please you are you going to smash with your head wall, JScrollPane is designated for Dynamic, Resizable LayoutManager, AbsoluteLayout can broken this its basic properties

  • starting from top

    1. public class app extends JFrame {

      • public class App { ---> Java Naming Conventions
      • and not extends anything, create JFrame as local variable
    2. new app(); ---> se Oracle tutorial Initial Thread

    3. create another JPanel, put there JTextField and JButton

    4. did you overlay something tf1.setBounds(5,5,this.getWidth()-120,20);

    5. NullLayout doesn't works correctly without using Insets

    6. change built_in FlowLayout for JPanel painel = new JPanel(null); to BorderLayout, there put JScrollPane with JTextArea to CENTER area

    7. you can to put JScrollPane with JTextArea to JFrames CENTER area directly and another JPanel with JTextField and JButton to SOUTH or NORTH, JFrame has BorderLayout implemented in API

    8. JScrollPane showing JScrollbars only in the case that its Dimension is smaller than JComponent placed there

    9. use JFrame.pack() instead of setSize, this line should be before setVisible

Upvotes: 2

Josh M
Josh M

Reputation: 11947

You have to pass in your JTextArea to your JScrollPane constructor, and then add your JScrollPane object to your Container as opposed to just the JTextArea. So it would look something like this:

JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);

Upvotes: 2

Related Questions