Swing GUI Design with JScrollPane and JLayeredPane

I would like to have a GUI setup as shown below.

JLayeredPane must always be the same size, but JPanel and JScrollPane can vary in size. I need a JScrollPane to display the JLayedPane by clicking the arrows, and what not if the JPanel and JScrollPane 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 if the JScrollPane smaller than the JLayeredPane , it does not provide scrolling ability.

Any ideas on what's going on? Is there a simpler code for 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) ); 

+2
source share
2 answers

Yes, the trick is to add a layered panel to a panel that uses a layout manager that will keep the layered panel centered. Sort of:

 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(); } }); } } 
+1
source

This is the intended behavior of JScrollPane .. it provides a JScrollPane for the base component. This means that if the component is a JLayeredPane , then the entire panel is treated as an attached scroll view, and there are no invisible inserts that center the layered panel the way you want, keeping the scroll larger.

To solve your problem, you can add a JLayeredPane to a JPanel and then add that JPanel as a JScrollPane by setting the correct insertion of the layered panel so that it remains in the middle as you want.

+1
source

All Articles