1
votes

I have this piece of code that is basically a JFrame that contains a JSplitPane which contains on the left side a JScrollPane which contains a JPanel. I expected to see the scroll bars since the JPanel inside the JScrollPane is larger that the JScrollPane itself. Why are the scroll bars not displayed? If I replace setSize() with setPreferredSize() then it works, but I want to use setSize(). Is there any way I can use setSize() and have the scroll bars showing?

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

public class Test {

    public static void main( String[] args ) {
        JFrame frame = new JFrame();
        frame.setLayout( new BorderLayout() );
        JSplitPane splitPane = new JSplitPane();
        frame.add( splitPane, BorderLayout.CENTER );
        JPanel panel = new JPanel();
        panel.setBackground( Color.red );
        panel.setSize( 1920, 1200 );
        //panel.setPreferredSize( new Dimension( 1920, 1200 ) );
        JScrollPane scrollPane = new JScrollPane( panel );
        splitPane.setLeftComponent( scrollPane );
        splitPane.setRightComponent( new JPanel() );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
        frame.setSize( 960, 600 );
    }
}

Edit: I've added a modified version where I use setPreferredSize(). Is there a better solution for dynamically changing the size?

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

public class Test {

    public static void main( String[] args ) {
        JFrame frame = new JFrame();
        frame.setLayout( new BorderLayout() );
        JSplitPane splitPane = new JSplitPane();
        frame.add( splitPane, BorderLayout.CENTER );
        final JPanel panel = new JPanel();
        panel.setBackground( Color.red );
        panel.setPreferredSize( new Dimension( 1920, 1200 ) );
        JScrollPane scrollPane = new JScrollPane( panel );
        splitPane.setLeftComponent( scrollPane );
        JButton button = new JButton();
        button.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed( ActionEvent e ) {
                panel.setPreferredSize( new Dimension( 3840, 2400 ) );
                panel.revalidate();
            }
        });
        splitPane.setRightComponent( button );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
        frame.setSize( 960, 600 );
    }
}
1
Add something to the panel which causes the size of panel to exceeded the viewable area of the scroll pane; override the panel's getPreferredSize method instead of using setSizeMadProgrammer
with setPreferredSize() then it works, but I want to use setSize() Why do you want to use something that doesn't work?FredK
If I replace setSize() with setPreferredSize() then it works, but I want to use setSize(). - but that is NOT the way Swing works or was designed to work. So you can't use setSize() because that means nothing to the viewport of the scrollpane.camickr
@FredK I have an image (1920x1200) that i need to display at various zooms. At zoom 8x for example the image is too big for the memory so I have to calculate the offset of the section that is visible and it is easier to calculate that offset when the JLabel inside that JPanel is the same size as the image. I can use setSize() to dynamically change the size of the JPanel but I think setPreferredSize() won't dynamically change the size. Am I wrong?corneliu

1 Answers

1
votes

Your setSize will be ignored by the JSplitPane because the layout of the left/right components is not null, and it tries to fit the internal components in the available space.

The layout manager for the JSplitPane's left/right components honors the preferredSize property(and not the size property) and if it hasn't been set, it just tries to fit the internal component inside the available space of left/right area in JSplitPane.

Use setPreferredSize instead or override the getPreferredSize method for your panel as camickr described in the comment.