简体   繁体   中英

Force JScrollPane and JPanel to repaint

I have a JScrollPane that holds a JPanel. The layout on the JPanel is a GridBagLayout. On that JPanel, I add a number of custom components - each is a JPanel with 3 JLabels.

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. If I resize the JDialog in which this all sites, the JPanel will paint properly. It also works if I scroll the JScrollPane horizontally even a tiny bit.

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. I've run into this situation before and have never been able to fully solve it. Any suggestions?

Running under 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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