下面列出了javax.swing.PopupFactory#getPopup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showPopup(Painter p, Rectangle rect) {
mouse.deinstall();
Point l = table.getLocationOnScreen();
rect.translate(l.x, l.y);
popupRect = rect;
popupLocation = new Point(l.x + p.getX(), l.y + p.getY());
PopupFactory popupFactory = PopupFactory.getSharedInstance();
popup = popupFactory.getPopup(table, p, popupLocation.x, popupLocation.y);
popup.show();
paranoid = new Paranoid(p);
paranoid.install();
awt = new AWT();
awt.install();
}
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();
}
@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);
}
/**
* Create and display the popup at the given bounds.
*
* @param popupBounds location and size of the popup.
* @param displayAboveCaret whether the popup is displayed above the anchor
* bounds or below them (it does not be right above them).
*/
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
// Hide the original popup if exists
if (popup != null) {
popup.hide();
popup = null;
}
// Explicitly set the preferred size
Dimension origPrefSize = getPreferredSize();
Dimension newPrefSize = popupBounds.getSize();
JComponent contComp = getContentComponent();
if (contComp == null){
return;
}
contComp.setPreferredSize(newPrefSize);
showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
PopupFactory factory = PopupFactory.getSharedInstance();
// Lightweight completion popups don't work well on the Mac - trying
// to click on its scrollbars etc. will cause the window to be hidden,
// so force a heavyweight parent by passing in owner==null. (#96717)
JTextComponent owner = layout.getEditorComponent();
if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) {
owner = null;
}
// #76648: Autocomplete box is too close to text
if(displayAboveCaret && Utilities.isMac()) {
popupBounds.y -= 10;
}
popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
popup.show();
this.popupBounds = popupBounds;
this.displayAboveCaret = displayAboveCaret;
}
/**
* Create and display the popup at the given bounds.
*
* @param popupBounds location and size of the popup.
* @param displayAboveCaret whether the popup is displayed above the anchor
* bounds or below them (it does not be right above them).
*/
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
// Hide the original popup if exists
if (popup != null) {
popup.hide();
popup = null;
}
// Explicitly set the preferred size
Dimension origPrefSize = getPreferredSize();
Dimension newPrefSize = popupBounds.getSize();
JComponent contComp = getContentComponent();
if (contComp == null){
return;
}
contComp.setPreferredSize(newPrefSize);
showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
PopupFactory factory = PopupFactory.getSharedInstance();
// Lightweight completion popups don't work well on the Mac - trying
// to click on its scrollbars etc. will cause the window to be hidden,
// so force a heavyweight parent by passing in owner==null. (#96717)
JTextComponent owner = getEditorComponent();
if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) { //NOI18N
owner = null;
}
// #76648: Autocomplete box is too close to text
if(displayAboveCaret && Utilities.isMac()) {
popupBounds.y -= 10;
}
popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
popup.show();
this.popupBounds = popupBounds;
this.displayAboveCaret = displayAboveCaret;
}
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);
}
}
private void run() {
JPanel panel = new JPanel();
int count = 0;
long diffTime, initialDiffTime = 0;
while (count < ITERATION_NUMBER) {
robot.delay(ROBOT_DELAY);
PopupFactory factory = PopupFactory.getSharedInstance();
Popup popup = factory.getPopup(panel, textArea, editorPane.getLocation().x + 20,
editorPane.getLocation().y + 20);
long startTime = System.currentTimeMillis();
popup.show();
long endTime = System.currentTimeMillis();
diffTime = endTime - startTime;
if (count > 1) {
if (diffTime * HANG_TIME_FACTOR < (endTime - startTime)) {
throw new RuntimeException("The test is near to be hang: iteration count = " + count
+ " initial time = " + initialDiffTime
+ " current time = " + diffTime);
}
} else {
initialDiffTime = diffTime;
}
count++;
robot.delay(ROBOT_DELAY);
popup.hide();
}
}
protected void showTipWindow() {
if(insideComponent == null || !insideComponent.isShowing())
return;
cancelled = false;
LOG.fine("cancelled=false"); //NOI18N
for (Container p = insideComponent.getParent(); p != null; p = p.getParent()) {
if (p instanceof JPopupMenu) break;
if (p instanceof Window) {
if (!((Window)p).isFocused()) {
return;
}
break;
}
}
if (enabled) {
Dimension size;
// Just to be paranoid
hideTipWindow();
tip = createToolTip();
tip.setTipText(toolTipText);
size = tip.getPreferredSize();
Point location = provider.getToolTipLocation( mouseEvent.getPoint(), size );
// we do not adjust x/y when using awt.Window tips
if (popupRect == null){
popupRect = new Rectangle();
}
popupRect.setBounds( location.x, location.y, size.width, size.height );
PopupFactory popupFactory = PopupFactory.getSharedInstance();
tipWindow = popupFactory.getPopup(insideComponent, tip,
location.x,
location.y);
tipWindow.show();
Window componentWindow = SwingUtilities.windowForComponent(
insideComponent);
window = SwingUtilities.windowForComponent(tip);
if (window != null && window != componentWindow) {
window.addMouseListener(this);
}
else {
window = null;
}
Toolkit.getDefaultToolkit().addAWTEventListener( getAWTListener(), AWTEvent.KEY_EVENT_MASK );
tipShowing = true;
}
}
private void showPopupAtMousePosition() {
// StaticDebug.debug("DND POPUP: Show popup at Mouse position");
// get mouse location
Point screenLocation = MouseInfo.getPointerInfo().getLocation();
screenLocation.x += 26;
screenLocation.y += 10;
// if tooltip is shown
if (tipWindow != null) {
// StaticDebug.debug("DND POPUP: Popup is already shown");
// check if mouse has moved
if (mouseX != screenLocation.x || mouseY != screenLocation.y) {
// StaticDebug.debug("DND POPUP: old position x = "+mouseX);
// StaticDebug.debug("DND POPUP: old position y = "+mouseY);
// StaticDebug.debug("DND POPUP: new position x = "+screenLocation.x);
// StaticDebug.debug("DND POPUP: new position y = "+screenLocation.y);
// StaticDebug.debug("DND POPUP: Mouse position has changed.. hide popup first");
// hide tooltip
hideDropDeniedTooltip();
} else {
// StaticDebug.debug("DND POPUP: Restart hide timer to prevent popup from being hidden.");
// otherwise restart hide timer
hideTimer.restart();
return;
}
}
Point componentLocation = (Point) screenLocation.clone();
SwingUtilities.convertPointFromScreen(componentLocation, popupSource);
if (tipWindow == null && popupSource.contains(componentLocation)) {
// StaticDebug.debug("DND POPUP: Mouse is inside popupSource and popup is not shown");
JToolTip tip = popupSource.createToolTip();
tip.setTipText(reason);
PopupFactory popupFactory = PopupFactory.getSharedInstance();
mouseX = screenLocation.x;
mouseY = screenLocation.y;
// StaticDebug.debug("DND POPUP: show popup at "+mouseX+","+mouseY+" and start hide timer");
tipWindow = popupFactory.getPopup(popupSource, tip, mouseX, mouseY);
tipWindow.show();
hideTimer.restart();
}
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}