N0way1998
N0way1998

Reputation: 11

Adding JScrollPane to JPanel with another panels inside

i been working on some bigger project lately but couldn't figure it out why JScrollPane wouldn't work. I have never used it before and I read many solved problems about it on stackOverflow and other programming forums but non of the code were looking similar to mine to help me implement my method. this is new project i made to make it short and show some examples.

enter image description here

Red colour is main panel that will contain another panel/JScrollPane inside that will be colour black and i would like to make this Jpanel with colour black to be scrollable and hold any number of that white JPanels that might be from 0 to a 100+

public class ScrollablePane {

private JFrame frame;
private JPanel panelCopy;
private JPanel panel;
private JPanel container;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ScrollablePane window = new ScrollablePane();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ScrollablePane() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
        
    panel = new JPanel();
    panel.setBackground(Color.RED);
    panel.setBounds(0, 0, 434, 261);
    frame.getContentPane().add(panel);
    panel.setLayout(null);
    
    container = new JPanel();
    container.setBackground(Color.BLACK);
    container.setBounds(10, 10, 414, 241);
    container.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    panel.add(container);
    
    for(int i = 0; i < 20; i++) {
        if(i > 0) {
            panelCopy = new JPanel();
            panelCopy.setPreferredSize(new Dimension(400, 40));     
            container.add(panelCopy);
        }       
    }   
}

}

Upvotes: 0

Views: 520

Answers (1)

camickr
camickr

Reputation: 324167

  1. if you want to use a JScrollPane, then your code actually needs to use a JScrollPane. The code you posted doesn't even create a JScrollPane.
  1. If you want the panels to display vertically then don't use a FlowLayout. The FlowLayout is a horizontal layout. You could use a BoxLayout or a GridBagLayout.
  1. Why do you create the "panel" variable and add it the the content pane? The content pane of the frame already is a JPanel that uses a BorderLayout. There is no need to add another panel

  2. Don't use a null layout!!! Swing was designed to be used with layout managers. Scrolling won't work if the panel added to the scroll pane uses a null layout.

So in your case the basic logic might be something like:

Box container = Box.createVerticalBox();
// add you child panels to the container. 

JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(container, BorderLayout.PAGE_START);

JScrollPane scrollPane = new JScrollPane(wrapper);

frame.add(scrollPane, BorderLayout.CENTER);

Note the "wrapper" panel is used to prevent the panels from expanding in size when the scroll pane is larger then the preferred size of the "container" panel.

Try:

//JScrollPane scrollPane = new JScrollPane(wrapper);
JScrollPane scrollPane = new JScrollPane(container);

to see the different result.

Upvotes: 2

Related Questions