How to add scrollbar to a Scrollpane?

0

I have a panel, and I will have just fields and buttons on it. I thought about using JScrollPane to not make the screen so large. However, I'm not able to add this scrollbar.

How do I add a scroll bar inside the p panel, in the sample code? I just put a field to simplify the code.

package scroll;

import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class TelaSroll extends JFrame {

    private final JTextField vazio = new JTextField();
    private JButton bt = new JButton("Exemplo");

    public static void main(String[] args) {
        TelaSroll tela = new TelaSroll();
    }

    public TelaSroll() {
        setSize(450, 345);
        add(telaPainel());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public JComponent telaPainel() {
        JPanel painel = new JPanel();// Painel principal, nele eu adiciono outros paines que organizam a tela

        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(200, 200));
        p.add(vazio);
        vazio.setPreferredSize(new Dimension(100, 50));

        JScrollPane srcPainel = new JScrollPane(p);
        painel.add(srcPainel);
        p.add(bt);
        return painel;
    }
}
    
asked by anonymous 02.05.2017 / 04:45

1 answer

1

In order for the scroll bar to work, you can not set a fixed size for the panel that will add to JScrollPane , otherwise during screen construction, this size will limit components within JScrollPane . Instead, set a preferred size for JScrollPane , so that the bar appears automatically when needed:

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class TelaSroll extends JFrame {

    private final JTextField vazio = new JTextField();
    private JButton bt = new JButton("Exemplo");

    public static void main(String[] args) {
        TelaSroll tela = new TelaSroll();
    }

    public TelaSroll() {
        setSize(450, 345);
        add(telaPainel());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public JComponent telaPainel() {
        JPanel painel = new JPanel();// Painel principal, nele eu adiciono outros paines que organizam a tela

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        //p.setPreferredSize(new Dimension(200, 200));
        p.add(vazio);
        vazio.setPreferredSize(new Dimension(100, 50));

        JScrollPane srcPainel = new JScrollPane(p);
        srcPainel.setPreferredSize(new Dimension(200, 200));
        painel.add(srcPainel);
        p.add(bt);
        for(int i = 1; i <= 10; i++){
            p.add(new JButton("botao-teste " + i));
        }
        return painel;
    }
}

In the code above, I used BoxLayout to demonstrate the vertical bar, but if the horizontal size is exceeded, the horizontal bar will also appear in the component.

    
02.05.2017 / 13:24