title: JPopupMenu change size dynamically if there is a JPanel inside
body: <p>I have a <code>JPopupMenu</code> and I want to change it's inner size dynamically depending on it's <code>inner components' size</code>. In my SSCCE I described the problem.</p>
  
  <p><strong>SSCCE:</strong></p>
  
  <pre><code>public class PopupTest2 {
      public static void main(String[] a) {
          final JFrame frame = new JFrame();
          frame.setSize(500, 500);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          final JPanel panel = new JPanel(new BorderLayout());
          panel.setBorder(BorderFactory.createLineBorder(Color.RED));
  
          final JPopupMenu menu = new JPopupMenu();
  
          // critical step
          JPanel itemPanel = new JPanel();
          itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.Y_AXIS));
  
          final JMenuItem[] items = new JMenuItem[10];
          for (int i = 0; i &lt; 10; i++) {
              JMenuItem item = new JMenuItem("Item #"+String.valueOf(i));
              itemPanel.add(item);
              items[i] = item;
          }
  
          menu.add(itemPanel);
  
          JToggleButton button = new JToggleButton("Press me");
          button.addActionListener(new ActionListener() {
              boolean pressed = false;
              @Override
              public void actionPerformed(ActionEvent e) {
                  pressed = !pressed;
                  if (pressed) {
                      for (JMenuItem item : items) {
                          item.setText(item.getText()+" changed");
                      }
                  } else {
                      for (JMenuItem item : items) {
                          item.setText(item.getText().substring(0, item.getText().length() - 8));
                      }
                  }
              }
          });
  
          panel.add(button, BorderLayout.NORTH);
  
          panel.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseClicked(MouseEvent e) {
                  if (e.getButton() == MouseEvent.BUTTON3) {
                      menu.show(panel, (int) (e.getX() - menu.getPreferredSize().getWidth()), e.getY());
                  }
              }
          });
          frame.setContentPane(panel);
          frame.setUndecorated(true);
          frame.setBackground(new Color(50, 50, 50, 200));
  
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  frame.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Steps 2 reproduce:</strong></p>
  
  <ol>
  <li>Right-click to show popup menu.</li>
  <li>Click the <code>Press me</code> button (on the top of window).</li>
  <li>Right-click to show popup menu again (bug #1 - popup is still small-size)</li>
  <li>Right-click to show popup menu again (popup menu size is OK)</li>
  <li>Click the <code>Press me</code> button again.</li>
  <li>Right-click to show popup menu. (bug #2 - popup is still large-size)</li>
  </ol>
  
  <p>How can I force <code>JPopupMenu</code> to change its size before showing? And why does it work if I add items directly to <code>popupMenu</code>?</p>