Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show ScrollBars in FlowLayout only when necessary

I will rephrase my question:**

How to prevent Java ScrollBar from being enabled in FlowLayout when there is enough space to show all items by warping them.

example**

Here a screenshot of what I am trying to achieve:

Note that scrollbar is disabled when it is not necessary. enter image description here

And when you resize the window scrollbar should appear if some items are out the viewplane enter image description here

P.S. I am aware of things called documentation and web.

like image 741
Vitalij Avatar asked Nov 15 '11 18:11

Vitalij


2 Answers

Your updated screen shots suggest that you may be looking for Wrap Layout, which "extends the FlowLayout" in a way that "will result in synchronizing the preferred size of the container with the layout of the container." See also Creating a Custom Layout Manager.

like image 191
trashgod Avatar answered Sep 22 '22 03:09

trashgod


The default scroll bar policy is as needed, but I remember having to account for the FlowLayout gaps to get an even number initially. If you stretch this example out, you'll see the horizontal scroll bar disappear.

Update: It doesn't really fix your problem, but it shows how I implemented Scrollable. I wanted to get rid of setPreferredSize(), even tho' the image is just a placeholder. The Wrap Layout article talks about why FlowLayout works the way it does.

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

public class ImageScrollTest extends JPanel implements Scrollable {

    private static final int N = 8;
    private static final int W = 120;
    private static final int H = 100;
    private final FlowLayout layout = new FlowLayout();
    private final int hGap = layout.getHgap();
    private final int vGap = layout.getVgap();
    private final Dimension size;

    // Show n of N images in a Scrollable FlowLayout
    public ImageScrollTest(int n) {
        setLayout(layout);
        for (int i = 0; i < N; i++) {
            this.add(new ImagePanel());
        }
        size = new Dimension(n * W + (n + 1) * hGap, H + 2 * vGap);
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return size;
    }

    @Override
    public int getScrollableUnitIncrement(
        Rectangle visibleRect, int orientation, int direction) {
        return getIncrement(orientation);
    }

    @Override
    public int getScrollableBlockIncrement(
        Rectangle visibleRect, int orientation, int direction) {
        return getIncrement(orientation);
    }

    private int getIncrement(int orientation) {
        if (orientation == JScrollBar.HORIZONTAL) {
            return W + hGap;
        } else {
            return H + vGap;
        }
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    private static class ImagePanel extends JPanel {

        private static final Random rnd = new Random();
        private Color color = new Color(rnd.nextInt());

        public ImagePanel() {
            this.setBackground(color);
            this.setBorder(BorderFactory.createLineBorder(Color.blue));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(W, H);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageScrollTest ist = new ImageScrollTest(N / 2);
        JScrollPane sp = new JScrollPane(ist);
        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        f.add(sp);
        f.pack();
        f.setVisible(true);
    }
}
like image 27
Catalina Island Avatar answered Sep 22 '22 03:09

Catalina Island