wildpeanut
wildpeanut

Reputation: 23

Nothing happens when setting the background of JScrollPane.getViewPort()

I have a JPanel with layout set to null and the background is white. Then I added that JPanel to JScrollPane.

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestJScollPane extends JPanel {

    private static final long serialVersionUID = 1L;

    public TestJScollPane() {
        initUI();
    }

    private void initUI()
    {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().setBackground(Color.GRAY);
        scrollPane.setBounds(1, 1, 200, 200);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.MAGENTA));
        setBackground(Color.WHITE);
        setLayout(null);

        JFrame frame = new JFrame();
        scrollPane.setViewportView(this);
        frame.add(scrollPane);

        frame.setLayout(null);
        frame.setVisible(true);
        frame.setSize(800, 500);
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(30,30);
    }

    public static void main(String[] args)
    {
        new TestJScollPane();
    }
}

My scenario is I have a zoom tool that if I zoom out the JPanel and all its shapes that were painted were scaled using AffineTransform. So I expect that if I zoom out, the background of JScrollPane was color gray but the actual was color white.

Apologies, I added a sample. Actually, this is not the actual code I created this so that I can provide a sample for you guys to help me.

I set the preferred size of JPanel to 30x30 so I expect that the background of JScrollPane will become visible but it was not.

Thanks in advance for any help.

Upvotes: 1

Views: 467

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Change:

scrollPane.setViewportView(this);

To something like:

JPanel centerPanel = new JPanel(new GridBagLayout());
centerPanel.add(this);
scrollPane.setViewportView(centerPanel);

A GridBagLayout (by default, unless configured otherwise) will respect the preferred size of the child components and won't stretch them to fill the 'cell'. A scroll pane on the other hand, will stretch the content to (at least) fill the visible area.

Result:

enter image description here

But seriously, drop the use of null layouts. If the effect cannot be achieved using an existing layout (inbuilt or 3rd party) or a combination of layouts, it must have such esoteric positioning constraints that it deserves a custom layout manager.

Upvotes: 1

camickr
camickr

Reputation: 324118

By default the panel is sized to fit the viewport so you will not see the background of the viewport.

You need to implement the Scrollable interface of your JPanel to tell the scroll pane you want the panel displayed at its preferred size.

Or, instead of implementing the Scrollable interface yourself you can use the Scrollable Panel which, by default, will display the panel at its preferred size.

Changes to your code would be:

ScrollablePanel panel = new ScrollablePanel();
panel.setPreferredSize( new Dimension(30, 30) );
panel.setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.MAGENTA));
panel.setBackground(Color.WHITE);
//panel.setLayout(null);
scrollPane.setViewportView(panel);
//scrollPane.setViewportView(this);

Upvotes: 1

Related Questions