下面列出了怎么用javax.swing.Popup的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Popup getPopup( Component owner, Component contents, int x, int y )
throws IllegalArgumentException
{
if( !isDropShadowPainted( owner, contents ) )
return new NonFlashingPopup( super.getPopup( owner, contents, x, y ), contents );
// macOS and Linux adds drop shadow to heavy weight popups
if( SystemInfo.IS_MAC || SystemInfo.IS_LINUX ) {
Popup popup = getHeavyWeightPopup( owner, contents, x, y );
if( popup == null )
popup = super.getPopup( owner, contents, x, y );
return new NonFlashingPopup( popup, contents );
}
// create drop shadow popup
return new DropShadowPopup( super.getPopup( owner, contents, x, y ), owner, contents );
}
/**
* There is no API in Java 8 to force creation of heavy weight popups,
* but it is possible with reflection. Java 9 provides a new method.
*
* When changing FlatLaf system requirements to Java 9+,
* then this method can be replaced with:
* return getPopup( owner, contents, x, y, true );
*/
private Popup getHeavyWeightPopup( Component owner, Component contents, int x, int y )
throws IllegalArgumentException
{
try {
if( SystemInfo.IS_JAVA_9_OR_LATER ) {
if( java9getPopupMethod == null ) {
java9getPopupMethod = PopupFactory.class.getDeclaredMethod(
"getPopup", Component.class, Component.class, int.class, int.class, boolean.class );
}
return (Popup) java9getPopupMethod.invoke( this, owner, contents, x, y, true );
} else {
// Java 8
if( java8getPopupMethod == null ) {
java8getPopupMethod = PopupFactory.class.getDeclaredMethod(
"getPopup", Component.class, Component.class, int.class, int.class, int.class );
java8getPopupMethod.setAccessible( true );
}
return (Popup) java8getPopupMethod.invoke( this, owner, contents, x, y, /*HEAVY_WEIGHT_POPUP*/ 2 );
}
} catch( NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException ex ) {
// ignore
return null;
}
}
@Override
public Popup getPopup(Component owner, Component contents,
int x, int y) throws IllegalArgumentException {
assert owner instanceof JComponent;
Dimension d = contents.getPreferredSize();
Container c = ((JComponent) owner).getTopLevelAncestor();
if (c == null) {
throw new IllegalArgumentException ("Not onscreen: " + owner);
}
Point p = new Point (x, y);
SwingUtilities.convertPointFromScreen(p, c);
Rectangle r = new Rectangle (p.x, p.y, d.width, d.height);
if (c.getBounds().contains(r)) {
//XXX need API to determine if editor area comp is heavyweight,
//and if so, return a "medium weight" popup of a java.awt.Component
//that embeds the passed contents component
return new LWPopup (owner, contents, x, y);
} else {
return new HWPopup (owner, contents, x, y);
}
}
private void showPopup(Set<AppearancePort> portObjects) {
dragStart = null;
CircuitState circuitState = canvas.getCircuitState();
if (circuitState == null)
return;
ArrayList<Instance> ports = new ArrayList<Instance>(portObjects.size());
for (AppearancePort portObject : portObjects) {
ports.add(portObject.getPin());
}
hideCurrentPopup();
LayoutThumbnail layout = new LayoutThumbnail();
layout.setCircuit(circuitState, ports);
JViewport owner = canvasPane.getViewport();
Point ownerLoc = owner.getLocationOnScreen();
Dimension ownerDim = owner.getSize();
Dimension layoutDim = layout.getPreferredSize();
int x = ownerLoc.x + Math.max(0, ownerDim.width - layoutDim.width - 5);
int y = ownerLoc.y + Math.max(0, ownerDim.height - layoutDim.height - 5);
PopupFactory factory = PopupFactory.getSharedInstance();
Popup popup = factory.getPopup(canvasPane.getViewport(), layout, x, y);
popup.show();
curPopup = popup;
curPopupTime = System.currentTimeMillis();
}
private void initPopup(Component owner, Component contents, int x, int y, Popup popup) {
this.owner = owner;
this.contents = contents;
this.popup = popup;
this.x = x;
this.y = y;
boolean mac = false;
try {
mac = System.getProperty("os.name").toLowerCase().startsWith("mac");
} catch (SecurityException e) {
// do nothing
}
if (mac) {
((JComponent) contents).setBorder(Borders.getPopupMenuBorder());
} else if (((JComponent) contents).getBorder() instanceof DummyBorder) {
if ((owner != null) //
&& (((owner instanceof JMenu) && ((JMenu) owner).isTopLevelMenu()) //
|| ((owner.getParent() != null) && (owner.getParent() instanceof javax.swing.JToolBar)) //
|| (owner instanceof javax.swing.JComboBox))) {
((JComponent) contents).setBorder(Borders.getPopupBorder());
} else {
((JComponent) contents).setBorder(Borders.getShadowedPopupMenuBorder());
}
}
}
/**
* Checks if the focus is still on this component or its child components.
*/
private boolean isFocusInside(Object newFocusedComp) {
if (newFocusedComp instanceof Popup) {
return true;
}
if (newFocusedComp instanceof Component && !SwingUtilities.isDescendingFrom((Component) newFocusedComp, this)) {
// Check if focus is on other window
if (containingWindow == null) {
return false;
}
Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
// if focus is on other window in owner chain, return false
Window parent = containingWindow;
do {
if (parent == focusedWindow) {
return false;
} else {
parent = parent.getOwner();
}
} while (parent != null);
}
return true;
}
public static void setPopupFactory() {
PopupFactory.setSharedInstance(new PopupFactory() {
@Override
public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
if (contents instanceof JToolTip) {
JToolTip toolTip = (JToolTip)contents;
int width = (int) toolTip.getPreferredSize().getWidth();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
// if there is enough room, move tooltip to the right to have enough room
// for large tooltips.
// this way they don't hinder mouse movement and make it possible to easily
// view multiple tooltips of items.
if (x + width + TOOLTIP_X_OFFSET < screenWidth) {
x += TOOLTIP_X_OFFSET;
}
}
return super.getPopup(owner, contents, x, y);
}
});
}
static Popup getInstance(Component owner, Component contents, int x, int y,
Popup delegate) {
final ShadowPopup result;
synchronized (ShadowPopup.class) {
if (cache == null) {
cache = new ArrayList<>(MAX_CACHE_SIZE);
}
if (!cache.isEmpty()) {
result = cache.remove(0);
} else {
result = new ShadowPopup();
}
}
result.reset(owner, contents, x, y, delegate);
return result;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
NonFlashingPopup( Popup delegate, Component contents ) {
this.delegate = delegate;
popupWindow = SwingUtilities.windowForComponent( contents );
if( popupWindow != null ) {
// heavy weight popup
// fix background flashing which may occur on some platforms
// (e.g. macOS and Linux) when using dark theme
oldPopupWindowBackground = popupWindow.getBackground();
popupWindow.setBackground( contents.getBackground() );
}
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
public Toast(Component comp, Point toastLocation, String msg, long forDuration) {
this.component = comp;
this.location = toastLocation;
this.message = msg;
this.duration = forDuration;
if(this.component != null)
{
if(this.location == null)
{
this.location = component.getLocationOnScreen();
}
new Thread(new Runnable()
{
@Override
public void run()
{
Popup view = null;
try
{
Label tip = new Label(message);
tip.setForeground(Color.black);
tip.setBackground(Color.white);
view = PopupFactory.getSharedInstance().getPopup(component, tip , location.x + 30, location.y + component.getHeight() + 5);
view.show();
Thread.sleep(duration);
} catch (InterruptedException ex)
{
Logger.getLogger(Toast.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
view.hide();
}
}
}).start();
}
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
@Override
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = layer.hackedPopupFactory;
if(popupFactory == null) {
return super.getPopup(popup, x, y);
}
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
@Override
public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
final JMenu menu = (JMenu) owner;
JPanel cont = containerMap.get(menu);
if (cont == null) {
cont = new VisualDesignerJPanelContainer(menu,this);
cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
RADVisualContainer menuRAD = (RADVisualContainer) canvas.formDesigner.getMetaComponent(menu);
for(RADComponent c : menuRAD.getSubBeans()) {
JComponent comp = (JComponent) canvas.formDesigner.getComponent(c);
cont.add(comp);
}
cont.setBorder(BorderFactory.createLineBorder(Color.BLACK));
containerMap.put(menu,cont);
canvas.layers.add(cont, JLayeredPane.DEFAULT_LAYER);
}
cont.setSize(cont.getLayout().preferredLayoutSize(cont));
canvas.validate();
canvas.setVisible(true);
final JPanel fcont = cont;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setLocationFromMenu(menu, fcont);
}
});
canvas.validate();
canvas.repaint();
VisualDesignerJPanelPopup popup = new VisualDesignerJPanelPopup(cont, menu, this);
popupMap.put(menu,popup);
return popup;
}
@Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
int col = listClasses.columnAtPoint(p);
int row = listClasses.rowAtPoint(p);
if (col < 0 || row < 0) {
hidePopup();
return;
}
if (col == currentCol && row == currentRow) {
// the tooltip is (probably) shown, do not create again
return;
}
Rectangle cellRect = listClasses.getCellRect(row, col, false);
Point pt = cellRect.getLocation();
SwingUtilities.convertPointToScreen(pt, listClasses);
RenderedImage ri = new RenderedImage();
if (!updateTooltipImage(ri, row, col)) {
return;
}
ri.addMouseListener(this);
Popup popup = PopupFactory.getSharedInstance().getPopup(listClasses, ri, pt.x, pt.y);
popupContents = ri;
currentPopup = popup;
currentCol = col;
currentRow = row;
popup.show();
System.err.println("Hello");
}
private void addStatistics(String kind, int count, int numRow,
final MouseEvent e) {
JLabel label = (JLabel)e.getComponent();
if(!label.getToolTipText().contains(kind)) {
// add the statistics to the tooltip
String toolTip = label.getToolTipText();
toolTip = toolTip.replaceAll("</?html>", "");
toolTip = kind + " = " + count + "<br>" + toolTip;
toolTip = "<html>" + toolTip + "</html>";
label.setToolTipText(toolTip);
}
if(bottomSplitPane.getDividerLocation()
/ bottomSplitPane.getSize().getWidth() < 0.90) {
// select the row in the statistics table
statisticsTabbedPane.setSelectedIndex(1);
oneRowStatisticsTable.setRowSelectionInterval(numRow, numRow);
oneRowStatisticsTable.scrollRectToVisible(oneRowStatisticsTable
.getCellRect(numRow, 0, true));
} else {
// display a tooltip
JToolTip tip = label.createToolTip();
tip.setTipText(kind + " = " + count);
PopupFactory popupFactory = PopupFactory.getSharedInstance();
final Popup tipWindow =
popupFactory.getPopup(label, tip, e.getX()
+ e.getComponent().getLocationOnScreen().x, e.getY()
+ e.getComponent().getLocationOnScreen().y);
tipWindow.show();
Date timeToRun = new Date(System.currentTimeMillis() + 2000);
Timer timer = new Timer("Annic statistics hide tooltip timer", true);
timer.schedule(new TimerTask() {
@Override
public void run() {
// hide the tooltip after 2 seconds
tipWindow.hide();
}
}, timeToRun);
}
}
private void addStatistics(String kind, int count, int numRow,
final MouseEvent e) {
JLabel label = (JLabel)e.getComponent();
if(!label.getToolTipText().contains(kind)) {
// add the statistics to the tooltip
String toolTip = label.getToolTipText();
toolTip = toolTip.replaceAll("</?html>", "");
toolTip = kind + " = " + count + "<br>" + toolTip;
toolTip = "<html>" + toolTip + "</html>";
label.setToolTipText(toolTip);
}
if(bottomSplitPane.getDividerLocation()
/ bottomSplitPane.getSize().getWidth() < 0.90) {
// select the row in the statistics table
statisticsTabbedPane.setSelectedIndex(1);
oneRowStatisticsTable.setRowSelectionInterval(numRow, numRow);
oneRowStatisticsTable.scrollRectToVisible(oneRowStatisticsTable
.getCellRect(numRow, 0, true));
} else {
// display a tooltip
JToolTip tip = label.createToolTip();
tip.setTipText(kind + " = " + count);
PopupFactory popupFactory = PopupFactory.getSharedInstance();
final Popup tipWindow =
popupFactory.getPopup(label, tip, e.getX()
+ e.getComponent().getLocationOnScreen().x, e.getY()
+ e.getComponent().getLocationOnScreen().y);
tipWindow.show();
Date timeToRun = new Date(System.currentTimeMillis() + 2000);
Timer timer = new Timer("Annic statistics hide tooltip timer", true);
timer.schedule(new TimerTask() {
@Override
public void run() {
// hide the tooltip after 2 seconds
tipWindow.hide();
}
}, timeToRun);
}
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
private void dispose() throws Exception {
SwingUtilities.invokeAndWait(() -> {
Popup popup = PopMenuUIExt.getPopup();
if (popup != null) {
popup.hide();
}
frame.dispose();
});
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
Popup popup = ((PopMenuUIExt) jpopup.getUI()).getPopup();
if (popup != null) {
isLightWeight = !popup.getClass().toString().
contains("HeavyWeightPopup");
}
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
public void hideCurrentPopup() {
Popup cur = curPopup;
if (cur != null) {
curPopup = null;
dragStart = null;
cur.hide();
}
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getPopup</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public Popup getPopup(JPopupMenu a, int b, int c) {
Popup returnValue =
((PopupMenuUI) (uis.elementAt(0))).getPopup(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((PopupMenuUI) (uis.elementAt(i))).getPopup(a,b,c);
}
return returnValue;
}