Vivekanand Kumar
Vivekanand Kumar

Reputation: 3

How to add scrollbar automatic in jscrollpane?

I try to program a GUI like this. When a button clicked every time a new button is created and placed at specific position but after adding some buttons in jscrollpane, scrollbar not activated, so I unable to see all created buttons.

My code is here:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test{

    private JFrame frame;
    private JPanel panel1,panel2;
    private JScrollPane pane;
    private JButton button;
    int i = 1, y = 10;

    public Test()
    {
        panel2 = new JPanel(null);
        panel2.setBounds(0,0,280,300);

        button = new JButton("Add Button");
        button.setBounds(90,10,120,30);

        pane = new JScrollPane();
        pane.setBounds(10,50,280,300);

        panel1 = new JPanel(null);
        panel1.setPreferredSize(new Dimension(300,400));
        panel1.setBackground(Color.WHITE);

        frame = new JFrame("Test");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel1);
        frame.pack();

        panel1.add(pane);
        panel1.add(button);

        pane.add(panel2);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
                i += 1;
                y += 35;
            }
        });
    }
    public static void main(String[] args) {
        new Test();
    }
}

Upvotes: 0

Views: 562

Answers (1)

camickr
camickr

Reputation: 324078

Don't use a null layout. Don't use setBounds().

The scrollbars will only appear automatically when the preferred size of the panel is greater that the size of the scroll pane.

It is the job of the layout manager to:

  1. set the location of a component
  2. set the size of a component
  3. calculate the preferred size of the panel.

So the solution is to use the appropriate layout manager on your panel.

So for example you can use a BoxLayout:

//panel2 = new JPanel(null);
panel2 = new JPanel();
panel2.setLayout( new BoxLayout(panel2, BoxLayout.Y_AXIS) );

And then when you add components to a visible frame you need to revalidate() the panel to invoke the layout manager:

//panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
panel2.add(new JButton("Button "+i));
panel2.revalidate();

There is no need for panel1. Just add the components to the frame:

//panel1.add(pane);
//panel1.add(button);
frame.add(button, BorderLayout.PAGE_START);
frame.add(pane, BorderLayout.CENTER);

But there are other issues:

pane = new JScrollPane();

You actually need to add the panel to the scroll pane. So the code should be:

pane = new JScrollPane(panel2);

Since a component can only have a single parent, you need to remove:

pane.add(panel2);

Since the panel2 has been added to the scroll pane.

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel1);
    frame.pack();

The above logic is wrong.

You should only invoked pack() and setVisible( true ) AFTER all the component have been added to the frame.

So most of the code posted is wrong.

Start by reading the section from the Swing turtorial on Layout Managers. Download the working demo code and learn how to better structure your code. The modify the code for your specific example.

Upvotes: 1

Related Questions