Re: Flowlayout, JPanel and JScrollPane: Scrolling vertically impossible?

From: loquak (liam_parc_at_hotmail.com)
Date: 03/27/04

  • Next message: loquak: "Invisible but fully operational panel"
    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);
    > }
    > }
    > }


  • Next message: loquak: "Invisible but fully operational panel"

    Relevant Pages

    • Re: Flowlayout, JPanel and JScrollPane: Scrolling vertically impossible?
      ... > Horizontal scrolling works just fine with a JPanel embedded in a ... > enable wrapping, and when components inside the panel go out of view, ... FlowLayout is too dumb a LayoutManager for use in containers that need ... Setting the preferredSize value to a hard coded value number is no ...
      (comp.lang.java.gui)
    • Re: Dialog Management Scrolling WAS Left & Right Scrolling in ISPF
      ... Making the panel width larger than 80 characters will cause an error if someone attempts to display the panel on a regular 80 width screen size. ... Dialog Management Scrolling WAS Left & Right Scrolling in ISPF ... My thought was that I would have to build a Panel table larger than the traditional 80 Character width. ... For IBM-MAIN subscribe / signoff / archive access instructions, ...
      (bit.listserv.ibm-main)
    • Re: I have an issue with an image getting corrupted upon scroll in a panel.
      ... The GDI+ FAQ shows you how to save without compression. ... corrupted and disappears upon scrolling within a panel. ...
      (microsoft.public.dotnet.framework.drawing)
    • Re: Distorted image when scrolling in DoubleBufferPanel
      ... i have a UserControl called DoubleBufferPanel which is a typical Panel ... By the way, I could see inherited Panel, but I don't see the point in inheriting UserControl, nor is it clear from your question whether you are actually inheriting Panel or UserControl. ... used this code to set the AutoScroll and draw the image: ... But, I suspect based on the code and picture that you've shown that you aren't limiting scrolling to having the bottom edge of the bitmap appear no higher than the bottom edge of the control, and thus when you redraw, nothing that would erase or otherwise redraw the area not part of the bitmap happens, so you just get the left-over bits from the stuff that was drawn before. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: I have an issue with an image getting corrupted upon scroll in a panel.
      ... the code project to come up with an overlayed map image, ... corrupted and disappears upon scrolling within a panel. ... If there is no way around this picturebox in a panel redraw issue; ...
      (microsoft.public.dotnet.framework.drawing)