Re: Flowlayout, JPanel and JScrollPane: Scrolling vertically impossible?
From: loquak (liam_parc_at_hotmail.com)
Date: 03/27/04
- Previous message: John Smith: "SWT database GUI plug in for Eclipse"
- In reply to: Babu Kalakrishnan: "Re: Flowlayout, JPanel and JScrollPane: Scrolling vertically impossible?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Mar 2004 20:57:12 +0200
Thanks a lot! I found a route around the problem by setting setPreferredSize
every time the applet size changes (which works okay), but it's a hack and
I'm sure your solution is more sophisticated.
"Babu Kalakrishnan" <k.a.l.a@sankya.com> wrote in message
news:c43ovb$2db505$1@ID-76750.news.uni-berlin.de...
> loquak wrote:
> >
> > Horizontal scrolling works just fine with a JPanel embedded in a
> > JScrollPane using FlowLayout. When I change setPreferredSize to
> > enable wrapping, and when components inside the panel go out of view,
> > the vertical scrollbar is never invoked!! So, what are the proper
> > components to get a panel with consecutive items vertically
> > scrollable?
> >
>
> FlowLayout is too dumb a LayoutManager for use in containers that need
> to be placed in a JScrollPane (unless you want only horizontal
> scrolling). This is because the preferredLayoutSize value returned by it
> is always the same - irrespective of the actual width of the container.
> (And therefore the preferredSize value returned by the panel would
> always be what is required to place all components in a single row).
>
> Setting the preferredSize value to a hard coded value number is no
> solution either, because the panel will now always return the set
> value irrespective of the components it contains.
>
> Attached below is a modified FlowLayout class that will return a
> variable preferredSize depending on the current width of the container.
> and hence allow you to enable vertical scrolling.
>
> Also, in order to limit your panel width and enable wrapping, it is
> better to make your panel implement the Scrollable interface. Return
> true for the getScrollableTracksViewportWidth() method so that
> horizontal scrolling is disabled. Also return your preferred viewport
> dimension from the getPreferredScrollableViewportSize() method.
>
> BK
>
> /*
> */
> import java.awt.*;
>
> /**
> * A modified version of FlowLayout that allows containers using this
> * Layout to behave in a reasonable manner when placed inside a
> * JScrollPane
>
> * @author Babu Kalakrishnan
> */
> public class ModifiedFlowLayout extends FlowLayout
> {
> public ModifiedFlowLayout()
> {
> super();
> }
>
> public ModifiedFlowLayout(int align)
> {
> super(align);
> }
>
> public ModifiedFlowLayout(int align, int hgap, int vgap)
> {
> super(align, hgap, vgap);
> }
>
> public Dimension minimumLayoutSize(Container target)
> {
> return computeSize(target, false);
> }
>
> public Dimension preferredLayoutSize(Container target)
> {
> return computeSize(target, true);
> }
>
> private Dimension computeSize(Container target, boolean minimum)
> {
> synchronized (target.getTreeLock())
> {
> int hgap = getHgap();
> int vgap = getVgap();
> int w = target.getWidth();
>
> // Let this behave like a regular FlowLayout (single row)
> // if the container hasn't been assigned any size yet
> if (w == 0)
> w = Integer.MAX_VALUE;
>
> Insets insets = target.getInsets();
> if (insets == null)
> insets = new Insets(0, 0, 0, 0);
> int reqdWidth = 0;
>
> int maxwidth = w - (insets.left + insets.right + hgap * 2);
> int n = target.getComponentCount();
> int x = 0;
> int y = insets.top;
> int rowHeight = 0;
>
> for (int i = 0; i < n; i++)
> {
> Component c = target.getComponent(i);
> if (c.isVisible())
> {
> Dimension d =
> minimum ? c.getMinimumSize() :
> c.getPreferredSize();
> if ((x == 0) || ((x + d.width) <= maxwidth))
> {
> if (x > 0)
> {
> x += hgap;
> }
> x += d.width;
> rowHeight = Math.max(rowHeight, d.height);
> } else
> {
> x = d.width;
> y += vgap + rowHeight;
> rowHeight = d.height;
> }
> reqdWidth = Math.max(reqdWidth, x);
> }
> }
> y += rowHeight;
> return new Dimension(reqdWidth+insets.left+insets.right, y);
> }
> }
> }
- Previous message: John Smith: "SWT database GUI plug in for Eclipse"
- In reply to: Babu Kalakrishnan: "Re: Flowlayout, JPanel and JScrollPane: Scrolling vertically impossible?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|