2

I would like to have a GUI setup like the diagram below.

The JLayeredPane should always be the same size, but the JPanel and the JScrollPane can change in size. I need the JScrollPane to be able to display the JLayedPane by clicking the arrows and what not if the JPanel and JScrollPane are not large enough to show the entire JLayeredPane.

The problem is that with the code below, the JLayeredPane always expands to fit the size of the JScrollPane and in the event that the JScrollPane is smaller than the JLayeredPane, it does not provide the scrolling ability.

Any Ideas on what is going on? Is there simpler code to achieve this?

Thanks


contentPanePanel = new javax.swing.JPanel();
contentPaneScollPane = new javax.swing.JScrollPane();
contentPane = new javax.swing.JLayeredPane();

contentPanePanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

contentPane.setRequestFocusEnabled(false);
contentPane.setVerifyInputWhenFocusTarget(false);

contentPaneScollPane.setViewportView(contentPane);

javax.swing.GroupLayout contentPanePanelLayout = new javax.swing.GroupLayout(contentPanePanel);

contentPanePanel.setLayout(contentPanePanelLayout);

contentPanePanelLayout.setHorizontalGroup(
    contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 968, Short.MAX_VALUE)
);
contentPanePanelLayout.setVerticalGroup(
    contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
);

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
user489041
  • 27,916
  • 55
  • 135
  • 204

2 Answers2

1

This is the intended behavior of a JScrollPane.. it provides a viewport to the underlying component. This means that, if the component is a JLayeredPane, then the whole pane is considered as the attached view of the scrollpane and there are no invisible insets that place the layered pane centered as you want while keeping the scrollpane bigger.

To solve your problem you could add the JLayeredPaneto a JPaneland then add this JPanelas the view of the JScrollPane, after having set the correct insets of the layered pane so that it stays in middle as you want.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

Yes, the trick is to add the layered pane to a panel that uses a layout manager which will keep the layered pane centered. Something like:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class ScrollPaneSSCCE
{
    private static void createAndShowUI()
    {
        JPanel center = new JPanel();
        center.setPreferredSize( new Dimension(300, 300) );
        center.setBackground( Color.RED );

        JPanel main = new JPanel( new GridBagLayout() );
        main.add(center, new GridBagConstraints());

        JFrame frame = new JFrame("Basic ScrollPaneSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane( main ) );
        frame.setSize(400, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288