Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JPanel inside JScrollPane?

I have a JFrame, in this JFrame I have a JPanel that I draw on, this Panel can be any size and so I placed it into a JScrollpane to let me scroll when the panel is larger than the window screen size.

Unfortunately does not work as I expected:

  1. Making the JFrame window smaller than the JPanel size does not show scroll bars

  2. The JScrollPane size now seems locked to the size of the JPanel I have added to it, where as before it resized to the bounds of it's JFrame window (it still kinda does this but only vertically now?!)

  3. The JPanel seems to assume the size of the JScrollpane regardless of what I set for preferred size

I am sure I'm doing something stupid, if someone could point out what I would be most grateful!

JPanel imageCanvas = new JPanel(); // 'Canvas' to draw on
JScrollPane scrollPane = new JScrollPane();
    
// set size of 'canvas'
imageCanvas.setMinimumSize(new Dimension(100,100));

// Scroll pane smaller then the size of the canvas so we should get scroll bars right?
scrollPane.setMinimumSize(new Dimension(50,50)); 
    
// Add a border to 'canvas'
imageCanvas.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
scrollPane.setViewportView(imageCanvas);
like image 277
FiniteRed Avatar asked Feb 22 '12 10:02

FiniteRed


People also ask

Can I add JScrollPane to JPanel?

getContentPane(). add(scrollPanel); This code will work in general to add JScrollPane to JPanel. Adjust bounds of frame, panel and scrollpane according to your requirements but ensure that the bounds of JScrollPane are within the bounds of the frame otherwise the scrollpane will not be visible.

How do you get components in JScrollPane?

One way: JViewport viewport = scrollPane. getViewport(); Component[] components = viewport. getComponents();

How do I make my JPanel scrollable?

The important methods of a JPanel class are getAccessibleContext(), getUI(), updateUI() and paramString(). We can also implement a JPanel with vertical and horizontal scrolls by adding the panel object to JScrollPane.

What is the difference between JScrollPane and JScrollBar?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.


2 Answers

// suggest a size of 'canvas'
_ImageCanvas.setPreferredSize(new Dimension(100,100));

// Scroll pane smaller then the size of the canvas so we should get scroll bars right?
_ScrollPane.setPreferredSize(new Dimension(50,50)); 

// ..later 
_Frame.pack();
like image 33
Andrew Thompson Avatar answered Sep 17 '22 14:09

Andrew Thompson


setPreferredSize() is the trick, setMinimumSize() and even setSize() on the component will be ignored by JScrollPane. Here's a working example using a red border.

import java.awt.*;

import javax.swing.*;

public class Scroller extends JFrame {

    public Scroller() throws HeadlessException {
        final JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setPreferredSize(new Dimension(800, 600));

        final JScrollPane scroll = new JScrollPane(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Scroller().setVisible(true);
            }
        });
    }
}
like image 104
Spyder Avatar answered Sep 18 '22 14:09

Spyder