Popup & PopupFactory

The bugtraq report that corresponds to this change is: 4303635.

Certain Components, such as JPopupMenu and JToolTip, present themselves above all other Components in a particular containment hierarchy. Rather than each of these Components containing the same code to obtain the same behavior, they delegate to a Popup which can be obtained from a PopupFactory. A Popup is able to display a Component at a particular location on the screen. Based on the size and location of the requested Component, PopupFactory returns an appropriate Popup.

Previously, Popup and PopupFactory were package private. For JDK 1.4, we have exposed these classes to enable custom look and feel implementations to create their own Popups. This allows other look and feels to position menus in an appropriate manner for their look and feel. To this end, we have made these classes public:

public class PopupFactory {
    public static void setSharedInstance(PopupFactory factory);
    public static PopupFactory getSharedInstance();
    public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException;
public class Popup {
    protected Popup(Component owner, Component contents, int x, int y);
    protected Popup();
    public void show();
    public void hide();

To enable the Popup used by JPopupMenu to be replaced by the UI, we added the following to PopupMenuUI:

    public Popup getPopup(JPopupMenu popup, int x, int y);

PopupMenuUI.getPopup's implementation obtains the Popup from the shared PopupFactory, but custom look and feel implementations may override this and return whatever Popup they desire.

Semantic Changes

JPopupMenu previously defined the method setLocation. Prior to this release, this method would invoke setLocation on the Popup, but as we have removed this method from Popup, JPopupMenu now recreates the Popup.

JPopupMenu also defined the method setPopupSize, which invoked setSize on the Popup if it was visible. Internally, we changed this method to set the preferred size of the JPopupMenu, which gives the same results. If the JPopupMenu is visible when this is invoked, the Popup will be recreated. The JPopupMenu javadoc changed to reflect the new behavior:

    public void setPopupSize(Dimension d);

    public void setPopupSize(int width, int height);

Copyright © 1993, 2021, Oracle and/or its affiliates. All rights reserved.