How to add scrollbar in JPanel?

0

I'd like to add a scrollbar 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 how it goes

HowcouldIdothis??

Note:thebuttonsinsideare"infinite"

    
asked by anonymous 16.05.2016 / 23:31

1 answer

1
  

Avoid using absolute layout unless you are in dire need and know the consequences , because absolute layout makes it difficult to maintain the screen and make your application look different depending on the monitor and resolution being executed.

     

There are several layouts managers for you to you do not have to worry about manual positioning or organization of components. Not to mention that the use of layouts makes your code easier to maintain than inserting a lot of setbounds , and in case you need to change the position of any component, you will have to reposition them all manually.

In the code presented, the problem is solved in an alternative, rather 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 rows is undefined, so just pass 0 in the first parameter of the constructor ( which represents the number of lines ) and leave it up to the layout.

And for the scroll to work correctly, you need to create a panel with the layout informed, and add this panel to JScrollPane , and do not add the buttons directly, because then the scrollpane will not know in which component it will be based in size to display the scroll.

I made some changes to your code, 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;
    }
}

Result:

Moreusageinformationregardingthislayoutcanbefoundin Official oracle guide .

    
16.07.2017 / 18:07