简体   繁体   English

强制JScrollPane和JPanel重绘

[英]Force JScrollPane and JPanel to repaint

I have a JScrollPane that holds a JPanel. 我有一个包含JPanel的JScrollPane。 The layout on the JPanel is a GridBagLayout. JPanel上的布局是GridBagLayout。 On that JPanel, I add a number of custom components - each is a JPanel with 3 JLabels. 在那个JPanel上,我添加了许多自定义组件 - 每个组件都是一个带有3个JLabel的JPanel。

The first time in the program I lay all of this out, it works fine. 在程序中我第一次将所有这些都放在一边,它运行正常。 When I invoke the code to add another custom component to the JPanel, the panel appears empty, but I can determine by examining the contents of the JPanel that my components are actually there. 当我调用代码将另一个自定义组件添加到JPanel时,面板显示为空,但我可以通过检查JPanel的内容来确定我的组件实际存在。 If I resize the JDialog in which this all sites, the JPanel will paint properly. 如果我调整所有网站的JDialog大小,JPanel将正确绘制。 It also works if I scroll the JScrollPane horizontally even a tiny bit. 如果我水平滚动JScrollPane甚至一点点也可以。

I use the same method for the initial layout as I do when adding an item. 我在添加项目时使用与初始布局相同的方法。

I've tried various combinations of repaint(), invalidate() and doLayout() but nothing seems to work all the time. 我已经尝试了repaint(),invalidate()和doLayout()的各种组合,但似乎没有任何东西一直在工作。 I've run into this situation before and have never been able to fully solve it. 我以前遇到过这种情况,从来没有能够完全解决它。 Any suggestions? 有什么建议么?

Running under OpenJDK 7u25. 在OpenJDK 7u25下运行。 Below is the code that lays out the scroll pane and panel. 下面是列出滚动窗格和面板的代码。

    private void displayRelatedBug(ArrayList<Bug> a_bugs) {
      // sort the bugs by ID
      ArrayList<Bug> l_sorted = new ArrayList<>(a_bugs);
      Collections.sort(l_sorted);

      pnlRelatedBugs.removeAll();
      pnlRelatedBugs.setLayout(new GridBagLayout());
      GridBagConstraints l_gbc = new GridBagConstraints();
      l_gbc.gridx = 0;
      l_gbc.gridy = 0;
      l_gbc.gridwidth = 1;
      l_gbc.gridheight = 1;
      l_gbc.anchor = GridBagConstraints.NORTHWEST;
      l_gbc.fill = GridBagConstraints.NONE;
      l_gbc.insets = new Insets(3, 4, 0, 0);
      for (Bug r : l_sorted) {
        pnlRelatedBugs.add(new RelatedBugDisplay(r, this), l_gbc);
        l_gbc.gridy++;
      }
      // add a filler at the bottom to push it up
      l_gbc.weighty = 1.0;
      pnlRelatedBugs.add(new MMPanel(), l_gbc);
      // add a filler on the right to push them left
      l_gbc.weighty = 0.0;
      l_gbc.weightx = 1.0;
      l_gbc.gridx++;
      pnlRelatedBugs.add(new MMPanel(), l_gbc);

      // try in vain to make it show up!!!
      pnlRelatedBugs.invalidate();
      pnlRelatedBugs.doLayout();
      pnlRelatedBugs.repaint();
      scrollerRelatedBugs.doLayout();

      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          pnlRelatedBugs.repaint();
          scrollerRelatedBugs.repaint();
          // this seems to help if the scroll bar is showing
          scrollerRelatedBugs.getHorizontalScrollBar().setValue(1);
          scrollerRelatedBugs.getHorizontalScrollBar().setValue(0);
        }
      });

    }

Whenever you add/remove components from a visible panel, the basic code is: 每当您从可见面板添加/删除组件时,基本代码为:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

Without a proper SSCCE we can't really tell what your code is doing. 如果没有合适的SSCCE我们无法确切知道您的代码在做什么。

如果您在显示容器上添加/删除/替换/其他操作与组件,则必须revalidaterepaint容器,并向其添加组件以进行正确显示。

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

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