1

I looked on many questions and websites but I can not find the answer. I have a JPanel. I would like to add a scroll bar, so I thought I would use a Jscrollpane.

public class TheFrame extends JFrame {

public ThePanel canvas;


public TheFrame() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    setLayout(new BorderLayout());


   //-------------------------------------

    JScrollPane scroll = new JScrollPane(canvas);
    scroll.setViewportBorder(new LineBorder(Color.RED));
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(scroll, BorderLayout.SOUTH);

   //-------------------------------------------------


    canvas = new ThePanel();

    setSize(700, 400);

    this.add(canvas, BorderLayout.CENTER);

    setVisible(true); 
}

At the moment, the scroll is just appearing at the bottom. The border shows that it is only a small area at the bottom. I am trying to put the Jpanel into a Jscrollpane. So the border is around the whole application area. ThePanel extends JPanel. Thank you for any assistance.

2 Answers 2

1
JScrollPane scroll = new JScrollPane(canvas);
add(scroll, BorderLayout.SOUTH);
canvas = new ThePanel();
this.add(canvas, BorderLayout.CENTER);

A couple of problems:

  1. the canvas variable is null when you create the scrollpane to nothing is added to the scrollpane

  2. a component can only have a single parent so when you add the canvas to the "CENTER" you remove it from the scrollpane.

The structure of the code should be:

canvas = new ThePanel();
JScrollPane scrollPane = new JScrollPane( canvas );
add(scrollPane, BorderLayout.CENTER);
setVisible( true );

That is, you add the canvas to the scrollpane and the scrollpane to the frame.

2
  • Thank you for the reply. It still seems that the JPanel and JScrollbar are seperate.... I need to add the canvas into the JscrollBar.
    – robert M
    Apr 30, 2015 at 15:27
  • I need to add the canvas into the JscrollBar - no you don't. I gave you the code you need. The scrollbars will appear automatically when the preferred size of the panel you add to the scrollpane is greater than the scrollpane. If you don't add any component to your panel then there is no reason for the scrollbar to appear. If you are doing custom painting then you need to override the getPreferredSize() method of your class to return the size of the panel.
    – camickr
    Apr 30, 2015 at 18:14
0

Add canvas to scroll, and add scroll to this. JScrollPane wraps the component, it doesn't magically add itself to the component.

Example:

JFrame frame = new JFrame();
JPanel pane = new JPanel();
JScrollPane scroller = new JScrollPane(pane);
frame.add(BorderLayout.CENTER, scroller);
scroller.setWheelScrollingEnabled(true);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.setVisible(true);
4
  • Thanks for the reply. okay, I added scroll.add(canvas); but this causes the dreaded nullpointerexception....
    – robert M
    Apr 30, 2015 at 15:14
  • @robertM, I added scroll.add(canvas); No, you never add a component directly to the scrollpane. You add the component to the viewport of the scrollpane, or create the scrollpane with the component as the parameter and the scrollpane will add it to the viewport for you.
    – camickr
    Apr 30, 2015 at 15:16
  • Could you give me an example, I am confused by what you mean. Thank you.
    – robert M
    Apr 30, 2015 at 15:54
  • @robertM I added an example. It actually works as it is, it doesn't really make much sense though
    – Distjubo
    Apr 30, 2015 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.