3

I have a JScrollPane that has a view component which uses SpringLayout.

final JPanel panel = new JPanel(new SpringLayout());
// add stuff to panel here
final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(scrollPane);

The JScrollPane doesn't seem to work, any help would be greatly appreciated!

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Ramus
  • 250
  • 1
  • 2
  • 10
  • 3
    This is a great question for an [sscce](http://sscce.org)! This would be a minimal compilable and runnable code example that demonstrates your problem.. This will allow us to run your code and modify it and perhaps even correct it. Please read the link before replying as it supplies many important details on the SSCCE requirements. – Hovercraft Full Of Eels Nov 24 '12 at 18:40
  • 1
    `The JScrollPane doesn't seem to work, any help would be greatly appreciated!` be sure that works but depends of, post an SSCCE demonstrated only JFrame, JScrollPane and JPanel(new SpringLayout()), hmmm empty JPanel never ever returns any of size, returns Dimension(0, 0), for all LayourManagers – mKorbel Nov 24 '12 at 18:45

1 Answers1

4

Quoting from How to Use Scroll Panes

Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.

  • so you would have to either call setPreferedSize(Dimension d) on JScrollPane instance

    final JPanel panel = new JPanel(new SpringLayout());
    // add stuff to panel here
    final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(300, 300));
    add(scrollPane);
    
  • or override getPreferredSize() of your JPanel/ component used as view port

    final JPanel panel = new JPanel(new SpringLayout()) {
    
        @Override
        public Dimension getPreferredSize() {
             return new Dimension(300, 300);
         }
     };
     // add stuff to panel here
     final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(scrollPane);
    

Other notes:

  • do not extend JFrame class unnecessarily.

  • simply call add(..) on JFrame instance as the call is forwarded to contentPane.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138