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.
**
Here a screenshot of what I am trying to achieve:
Note that scrollbar is disabled when it is not necessary.
And when you resize the window scrollbar should appear if some items are out the viewplane
P.S. I am aware of things called documentation and web.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With