How to add scrollbar to a Scrollpane?

Asked

Viewed 503 times

0

I have a panel, and I will have enough fields and buttons on it. I thought to use a JScrollPane not to make the screen so big. However, I’m not able to add this scroll bar.

As I add a scroll bar inside the panel p, in the example code? I put only one 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;
    }
}

1 answer

1


For the scroll bar to work, you cannot set a fixed size for the panel that will add to the JScrollPane, otherwise, during the construction of the screen, this size is who will limit the components inside the JScrollPane. Instead, set a preferred size for the JScrollPane, so that the bar automatically appears 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 in case the horizontal size is exceeded, the horizontal bar will also appear on the component.

  • Using another layout manager it uga a little, that’s why I’m not setting the scroll position (vertical/horizontal)?

  • 1

    @G.Araujo what do you mean, "Hold on"? I tested the way the code was initially (without the boxlayout), and what happened was to create a horizontal scroll, since every Jpanel has by default Flowlayout, which organizes components in the form of a horizontal line. I used boxlayout to exemplify the vertical scroll, but it works for both.

  • worked out here, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.