0

I'm trying to add few panel to JScrollpane and in turn to frame, but i'm not able to scroll. Here is the code.

first_panel =  new JPanel();
first_panel.setBackground(Color.red);
second_panel =  new JPanel();
second_panel.setBackground(Color.blue);
third_panel =  new JPanel();
third_panel.setBackground(Color.green);
fourth_panel =  new JPanel();
fourth_panel.setBackground(Color.red);
fifith_panel =  new JPanel();
fifith_panel.setBackground(Color.blue);
six_panel =  new JPanel();
six_panel.setBackground(Color.green);
final_panel =  new JPanel();
final_panel.setLayout(null);
final_panel.setBackground(Color.gray);
final_panel.add(first_panel);
final_panel.add(second_panel);
final_panel.add(third_panel);
final_panel.add(fourth_panel);
final_panel.add(fifith_panel);
final_panel.add(six_panel);

first_panel.setBounds(10,10,200,100);
second_panel.setBounds(10,10,200,200);
third_panel.setBounds(10,10,200,300);
fourth_panel.setBounds(10,10,200,400);
fifith_panel.setBounds(10,10,200,500);
six_panel.setBounds(10,10,200,600);
panel_scroll = new JScrollPane(final_panel);
panel_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);    
final_panel.setBounds(0,0,400,400);
// scroll_panel =  new JPanel();
// scroll_panel.setLayout(null);
// scroll_panel.add(panel_scroll);
// panel_scroll.setBounds(0,0,400,400);
frame.getContentPane().add(panel_scroll);

Can someone helpme with this. Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 5
    don't do any manual sizing/locating, instead use a suitable LayoutManager – kleopatra Jul 08 '13 at 10:21
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. They are not conducive to exact placement. To organize the components for a robust GUI, use layout managers, or combinations of them, along with layout padding & borders for white space. – Andrew Thompson Jul 08 '13 at 10:58
  • Provide ASCII art of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Jul 08 '13 at 10:59

1 Answers1

3
  • use standard LayoutManagers instead of AbsoluteLayout, then Swing GUI will be continuously or proportionally resize with JFrame

  • use GridLayout() for final_panel in the case that all JPanels can have got the same size on the screen

  • override getPreferredSize for JPanel, for inital and correct PreferredSize on the screen (greater dimension for final_panel than JScrollPane)

  • is required to override ScrollPanel.setPreferredScrollableViewportSize(new Dimension(int, int));, then to call JFrame.pack() for inital and correct PreferredSize on the screen

  • is required to override JScrollPane.getVerticalScrollBar().setUnitIncrement(int); for natural scrolling

mKorbel
  • 109,525
  • 20
  • 134
  • 319