Joe Lewis
Joe Lewis

Reputation: 978

How to make a JFrame scrollable in Java?

I have this code in which I am trying to fit a scrollable Panel (JPanel) but I don't get it. Here is my code:

public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
    super("JConnector demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    getContentPane().setLayout(new GridBagLayout());
    init();

    getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
                         new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
    getContentPane().add(initConnectors(),
                         new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    getContentPane().add(props,
                         new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
    setSize(800, 600);
    setLocationRelativeTo(null);

}

Thanks in advance.

I edit to add a code that partially seems to work...

public Sniffer_GUI() {
    super("JConnector demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel container = new JPanel();
    JScrollPane scrPane = new JScrollPane(container);
    add(scrPane);
    scrPane.setLayout(new ScrollPaneLayout());
    init();

    add(initConnectors());

    setSize(800, 600);
    setLocationRelativeTo(null);

}

But it isn't still scrollable, at least it makes its function inside a JScrollPane, is a good step.

Upvotes: 12

Views: 78147

Answers (6)

Philip Brito
Philip Brito

Reputation: 5609

Maybe this will help...

JFrame frame = new JFrame();
JPanel panel = new JPanel();

// add something to you panel...
// panel.add(...);

// add the panel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane(panel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

// Then, add the jScrollPane to your frame
frame.getContentPane().add(jScrollPane);

Upvotes: 6

craigmiller160
craigmiller160

Reputation: 6283

Just chiming in, and feel free to correct me if I'm off base, but using a JScrollPane like that has an unintended consequence of requiring more frequent window resizing.

For example, I had a program where I set up a JFrame-wide scrollpane like that. I also had a JTextArea in a tab in the frame that was the same size as the content pane. This textArea was also in its own scrollpane (this was more of a screwing around project than anything else). When I loaded content from a file to deposit in the textArea, it triggered the scrollbars around the text area.

The result was that my, let's call it the innerScrollPane, was now larger than the JFrame because of the scrollbars, which hadn't been visible before. This then triggered what I'm now going to call the outerScrollPane, to display its scrollbars, which then covered up the inner scrollbars.

This was easily resolved by adding an extra window.pack() argument to the end of my file opening method, but I just wanted to throw this out there. The scrollbars can potentially cover up content in the window if you're not careful. But... well there are a million ways to prevent this problem, so it's not a huge deal. Just something to be aware of.

Upvotes: 1

gherson
gherson

Reputation: 195

To make a component of a JFrame scrollable, wrap the component in a JScrollPane:

    JScrollPane myJScrollPane = new JScrollPane(myJLabel,
         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

and replace mentions of myJLabel with myJScrollPane. Worked for me.

Upvotes: 2

Guillaume Polet
Guillaume Polet

Reputation: 47637

To extend @Eng.Fouad answer:

public class Sniffer_GUI extends JFrame {
    Canvas c = new Canvas();
    ConnectorPropertiesPanel props;
    public Sniffer_GUI() {
        super("JConnector demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel container = new JPanel();
        JScrollPane scrPane = new JScrollPane(container);
        getContentPane().add(scrPane);
        container.setLayout(new GridBagLayout());
        init();

        container.add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
                             new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        container.add(initConnectors(),
                             new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        container .add(props,
                             new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
        setSize(800, 600);
        setLocationRelativeTo(null);

    }
}

Upvotes: 8

Eng.Fouad
Eng.Fouad

Reputation: 117675

Make a JPanel scrollable and use it as a container, something like this:

JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane); // similar to getContentPane().add(scrPane);
// Now, you can add whatever you want to the container

Upvotes: 23

Logard
Logard

Reputation: 1513

Try this:

JScrollPane sp = new JScrollPane();
this.add(sp).
sp.add( *GUI elements for your applications.*)

Something like that should work for you. Take a look at this as well:

Upvotes: 0

Related Questions