简体   繁体   English

JScrollPane中的JPanel

[英]JPanel inside a JScrollPane

I have a JPanel and I create, dynamically, JCheckBoxes inside. 我有一个JPanel,并在其中动态创建JCheckBoxes。 These have to be added JCheckBoxes always a side by side. 这些必须始终并排添加JCheckBoxes。 In case there is more space to be inserted in the side, a new line of JCheckBoxes is created, as in a simple text editor. 如果要在侧面插入更多空间,则像在简单的文本编辑器中一样,将创建新的JCheckBoxes行。

This is happening perfectly. 这是完美的发生。 But ... 但是...

I set the layout on this JPanel to FlowLayout, exactly what I want. 我将这个JPanel上的布局设置为FlowLayout,正是我想要的。

The obvious problem is that a window has limited space. 明显的问题是窗口的空间有限。 So a good solution to this is: Insertion of this JPanel in a JScrollPane,l and making that happen only in the vertical scrolling. 因此,一个好的解决方案是:在JScrollPane,l中插入此JPanel,并使之仅在垂直滚动中发生。 But I have problems. 但是我有问题。 Although you can make only a vertical scroll bar to appear, the items are always added "forever" side by side. 尽管您只能使垂直滚动条出现,但始终始终将项“永久”添加。 And the vertical scroll simply does not work, only horizontally. 垂直滚动根本不起作用,只能水平滚动。

I've tried many ways to make the scroll only vertically, but nothing worked (if it had worked I would not be here:]). 我已经尝试了多种方法来仅垂直滚动,但没有任何效果(如果成功,我不会在这里:])。

So, has anyone had any similar problem, and can help me? 那么,有人遇到过类似的问题,可以为我提供帮助吗?

I shall be very grateful to those who help me. 我将非常感谢那些帮助我的人。

No more. 不再。

I dealt with the same issue with ScrollPanes and FlowLayouts. 我用ScrollPanes和FlowLayouts处理了相同的问题。 I found the best solution is to use a modified version of FlowLayout that takes into account vertical changes. 我发现最好的解决方案是使用考虑到垂直变化的FlowLayout的修改版本。 Here is the code for such a layout. 这是这种布局的代码。 You can include it in your project and call it just like a FlowLayout, however it will actually work nice with a scrollpane. 您可以将其包含在您的项目中,并像FlowLayout一样调用它,但是实际上它可以在滚动窗格中很好地工作。

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
  * Modifications by greearb and jzd
  */

 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) {
          // Size of largest component, so we can resize it in
          // either direction with something like a split-pane.
          return computeMinSize(target);
       }

       public Dimension preferredLayoutSize(Container target) {
          return computeSize(target);
       }

       private Dimension computeSize(Container target) {
          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 + vgap; // FlowLayout starts by adding vgap, so do that here too.
             int rowHeight = 0;

             for (int i = 0; i < n; i++) {
                Component c = target.getComponent(i);
                if (c.isVisible()) {
                   Dimension d = c.getPreferredSize();
                   if ((x == 0) || ((x + d.width) <= maxwidth)) {
                      // fits in current row.
                      if (x > 0) {
                         x += hgap;
                      }
                      x += d.width;
                      rowHeight = Math.max(rowHeight, d.height);
                   }
                   else {
                      // Start of new row
                      x = d.width;
                      y += vgap + rowHeight;
                      rowHeight = d.height;
                   }
                   reqdWidth = Math.max(reqdWidth, x);
                }
             }
             y += rowHeight;
             y += insets.bottom;
             return new Dimension(reqdWidth+insets.left+insets.right, y);
          }
       }

       private Dimension computeMinSize(Container target) {
          synchronized (target.getTreeLock()) {
             int minx = Integer.MAX_VALUE;
             int miny = Integer.MIN_VALUE;
             boolean found_one = false;
             int n = target.getComponentCount();

             for (int i = 0; i < n; i++) {
                Component c = target.getComponent(i);
                if (c.isVisible()) {
                   found_one = true;
                   Dimension d = c.getPreferredSize();
                   minx = Math.min(minx, d.width);
                   miny = Math.min(miny, d.height);
                }
             }
             if (found_one) {
                return new Dimension(minx, miny);
             }
             return new Dimension(0, 0);
          }
       }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM