0

I have added one JPanel to the JScrollPane. But the main panel is not scrolling. My problem is like this:

JPanel MainPanel = new JPanel();
MainPanel.setBounds(width/2,height/10,width/5,height/5);
MainPanel.setLayout(null);

JScrollPane scrollPane= new JScrollPane(MainPanel);
scrollPane.setBounds(width/2,height/10,width/5,height/5);
//Added scrollPane to MainFrame Panel
MainFrame.add(scrollPane);

//Added four JPanel to MainPanel
JPanel subPanel1 = new JPanel();
subPanel.setbounds(10,20,50,50);
MainPanel.add(subPanel);

JPanel subPanel2 = new JPanel();
subPanel2.setbounds(50,60,50,50);
MainPanel.add(subPanel2);

JPanel subPanel3 = new JPanel();
subPanel3.setbounds(50,100,50,50);
MainPanel.add(subPanel3);

JPanel subPanel4 = new JPanel();
subPanel4.setbounds(50,60,50,50);
MainPanel.add(subPanel4);

I didn't see the subPanel3 and subPanel4. Now I'm able to see the vertical scroll bar but it is not working.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Harshad201
  • 53
  • 2
  • 8

1 Answers1

0

I recommend you to use layouts instead of setBounds(). GridBagLayout, GridLayout, FlowLayout, BoxLayout and BorderLayout will be useful. Documentation is here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

And append this at the bottom of the code:

scrollPane.getViewport().setView(MainPanel);
stftnk
  • 21
  • 3
  • 1
    `scrollPane.getViewport().setView(MainPanel);` is not necessary, given `JScrollPane scrollPane= new JScrollPane(MainPanel);` (the panel is passed in the constructor and will be added to the viewport). – Andrew Thompson Apr 01 '19 at 10:29