11

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.

Lakshya Goyal
  • 360
  • 1
  • 6
  • 24
Joe Lewis
  • 948
  • 5
  • 18
  • 34

6 Answers6

21

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
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • I get problems with the layout of scrPane when I try to add elements, how could I do something similiar to the original code? – Joe Lewis May 29 '12 at 14:39
  • 2
    @JoeLewis Instead of doing what you do with the content pane, do it on the container described in Eng.Fouad's answer and simply add the scrollpane to the content pane (leave the default layout, BorderLayout, as the layout manager of the content pane) – Guillaume Polet May 29 '12 at 14:45
  • @Guillaume Polet ok perfect Eng.Fouad's code is partially working as I edited in the original post, it works but it isn't still scrollable... – Joe Lewis May 29 '12 at 14:48
  • @JoeLewis I posted and answer based on your code and Eng.Fouad proposal. – Guillaume Polet May 29 '12 at 14:50
8

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);

    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • My hero... xD Now I just need to make it vertically scrollable, since it's just horizontal... :P – Joe Lewis May 29 '12 at 14:52
  • @JoeLewis You can set the vertical and horizontal scroll bar policies to adjust this things. Consider reading the [ScrollPane tutorial](http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html) and also its [JavaDoc](http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html) – Guillaume Polet May 29 '12 at 14:57
  • My problem is, that iniConnectors() retrieves a JPanel and I need to make scrollable that Panel, since it's the most important, however I can't do it even adding initConnector to another JScrollPane and then do container.add("the new JScrollPane", etc...); – Joe Lewis May 29 '12 at 17:23
  • @JoeLewis I doubt that you need nested ScrollPane here, but I can't know for sure without seeing all your code. If you only want scrolling to apply to your JPanel initConnector, then keep your original code but use the following: `getContentPane().add(new JScrollPane(initConnectors()),...` instead of `getContentPane().add(initConnectors(),...` – Guillaume Polet May 29 '12 at 18:10
6

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);
Filipe Brito
  • 5,329
  • 5
  • 32
  • 42
2

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.

gherson
  • 169
  • 2
  • 9
  • 1
    Thank you to show the JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED and JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED inside JScrollPane's constructor. In my [answer](http://stackoverflow.com/a/32872829/4744263) I'm using setVerticalScrollBarPolicy and setHorizontalScrollBarPolicy method instead. Thank you for this possibility. – Filipe Brito Oct 10 '15 at 02:17
1

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.

craigmiller160
  • 5,751
  • 9
  • 41
  • 75
0

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:

Logard
  • 1,494
  • 1
  • 13
  • 27
  • 5
    Adding elements to the scrollpane is usually not what you want. Usually, you pass the container panel to the constructor of JScrollPane or you use `setViewportView` – Guillaume Polet May 29 '12 at 14:33