0

I have a panel with layout (for example, BorderLayout) and a JScrollPane on its center. JScrollPane has content inside it (a JPanel)

The thing is, that when this JScrollPane resizes, I do not want its content to resize. For example, if layout increases the JScrollPane, I want its content to be as small it was (and occupy only part of the pane), but it resizes to fit pane.

I also need an opportunity to reduce content inside the pane and increase it manually (there is no problems with increasing, they are in reducing).

So, how can I achieve content size independency? Of course, I need to save scrolling features, if content will be bigger than JScrollPane.

Here is a simple example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestScroll extends JFrame {

public TestScroll() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(500, 500);
    init();
}

private void init() {
    setLayout(new BorderLayout());

    // Inner panel
    final JPanel innerPanel = new JPanel();
    innerPanel.setOpaque(true);
    innerPanel.setBackground(Color.BLUE);
    innerPanel.setPreferredSize(new Dimension(200, 200));

    // Scroll
    final JScrollPane scrollPane = new JScrollPane(innerPanel);
    add(scrollPane, BorderLayout.CENTER);

    // Buttons
    JPanel buttonPanel = new JPanel();
    JButton extendButton = new JButton("Extend inner panel");
    extendButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            innerPanel.setPreferredSize(new Dimension(innerPanel.getWidth() * 2,
                    innerPanel.getHeight() * 2));
            innerPanel.revalidate();
        }
    });
    buttonPanel.add(extendButton);

    JButton reduceButton = new JButton("Reduce inner panel");
    reduceButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            innerPanel.setPreferredSize(new Dimension(innerPanel.getWidth() / 2,
                    innerPanel.getHeight() / 2));
            innerPanel.revalidate();
        }
    });
    buttonPanel.add(reduceButton);

    add(buttonPanel, BorderLayout.NORTH);

    setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TestScroll();
        }
    });
}

}

So, problem statement:

  1. I do not want inner panel to stretch to the pane (but pane can be resized by outer layout, so the panel must just keep its size).
  2. I want to be able to reduce inner panel manually so it can occupy only a part of the scroll pane.
  3. And, of course, I want to save scrolling functionality when inner panel is larger than scroll pane.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Luo
  • 445
  • 2
  • 10
  • 3
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Feb 18 '15 at 02:23
  • 2
    See [this answer](http://stackoverflow.com/a/7181197/418556) for two layouts that will center content without stretching it. – Andrew Thompson Feb 18 '15 at 02:25
  • The thing is that I allow JScrollPane to stretch with the parent. I just want to keep the size of JScrollPane content stable and be able to change it. – Luo Feb 18 '15 at 02:41
  • 1
    The thing is, without a [minimal runnable example program](http://stackoverflow.com/help/mcve), it's going to be awfully hard to help you. – Hovercraft Full Of Eels Feb 18 '15 at 02:43

1 Answers1

2

Did you follow Andrew's link about using layout managers to achieve your goal?

Here is another simple example:

//final JScrollPane scrollPane = new JScrollPane(innerPanel);
JPanel outer = new JPanel();
outer.add( innerPanel );
final JScrollPane scrollPane = new JScrollPane(outer);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Huge thanks! I tried a lot of examples with different layouts, but this concept is simple and working. – Luo Feb 18 '15 at 03:36