How to add scrollbar in Jpanel?

Asked

Viewed 1,748 times

0

I would like to add a scroll bar to a Jpanel that uses Absolute Layout.

    package Default;
    import javax.swing.JFrame;
    import javax.swing.border.EmptyBorder;

    import java.awt.Dimension;

    import javax.swing.JButton;
    import javax.swing.JScrollPane;

    public class teste extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) 
    {
        teste frame = new teste();
        frame.setVisible(true);
    }

    private static int qtButton = 0 ;

    public teste() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(10, 11, 464, 439);
        contentPane.add(panel);

        for(int i = 0; i < 15; i++)
        {
            gerarButton(panel);
        }   

    }
    public static void gerarButton(JScrollPane panel)
    {
        JButton NewButton = new JButton("New button");
        NewButton.setBounds(10, 11 + (34 * qtButton), 444, 23);
        panel.add(NewButton);

        qtButton ++;

        panel.repaint();
    }
}

And here’s the way it is

inserir a descrição da imagem aqui

How could I do that ??

Note: the buttons inside are "infinite"

  • Use Jscrollpane instead of Jpanel.

  • @Diegof I used and it still didn’t work

  • Seven a size for the JScrollPane, using setPreferredSize.

  • @Diegof I would like an example in the code I put in the question, could do it ??

1 answer

2

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, you will have to reposition all of them manually.

In the presented code, the problem is solved in an alternative and quite simple way: using GridLayout. This layout organizes the components of a container in the form of a grid, where you define how many rows and columns it should have. In your case, the number of lines is undefined, so just pass 0 in the first parameter of the constructor(representing the number of lines) and leave on account of the layout.

And for the scroll to work properly, you need to create a dashboard with the layout informed, and add this dashboard to the JScrollPane, and do not add the buttons directly, so the scrollpane will not know which component it will be based on the size to display the scroll.

I made some changes to your code by removing the absolute layout, since mixing relative and absolute layouts will not bring the expected result:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JScrollPane;

public class GridLayoutButtonsTest extends JFrame {

    private JPanel contentPane;
    private int qtButton = 0; 
    
    public static void main(String[] args) 
    {
        GridLayoutButtonsTest frame = new GridLayoutButtonsTest();
        frame.setVisible(true);
    }
    
    public GridLayoutButtonsTest() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(500, 500));
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        
        JScrollPane scroll = new JScrollPane();
        scroll.setPreferredSize(new Dimension(464, 439));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 10, 10));
        
        for(int i = 0; i < 15; i++)
        {
            panel.add(gerarButton());
        }
        scroll.setViewportView(panel);
        contentPane.add(scroll);
        
        pack();
    }
    
    public JButton gerarButton() {
        
        qtButton++;
        JButton NewButton = new JButton(String.valueOf(qtButton));
        return NewButton;
    }
}

Upshot:

inserir a descrição da imagem aqui

More usage information regarding this layout can be found on Official guide to the oracle.

Browser other questions tagged

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