下面列出了javax.swing.PopupFactory#setSharedInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
});
}
@Override
public void uninitialize() {
// remove desktop property listener
if( desktopPropertyListener != null ) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.removePropertyChangeListener( desktopPropertyName, desktopPropertyListener );
if( desktopPropertyName2 != null )
toolkit.removePropertyChangeListener( desktopPropertyName2, desktopPropertyListener );
toolkit.removePropertyChangeListener( DESKTOPFONTHINTS, desktopPropertyListener );
desktopPropertyName = null;
desktopPropertyName2 = null;
desktopPropertyListener = null;
}
// uninstall popup factory
if( oldPopupFactory != null ) {
PopupFactory.setSharedInstance( oldPopupFactory );
oldPopupFactory = null;
}
// uninstall mnemonic handler
if( mnemonicHandler != null ) {
mnemonicHandler.uninstall();
mnemonicHandler = null;
}
// restore default link color
new HTMLEditorKit().getStyleSheet().addRule( "a { color: blue; }" );
postInitialization = null;
// restore enable/disable window decorations
if( oldFrameWindowDecorated != null ) {
JFrame.setDefaultLookAndFeelDecorated( oldFrameWindowDecorated );
JDialog.setDefaultLookAndFeelDecorated( oldDialogWindowDecorated );
oldFrameWindowDecorated = null;
oldDialogWindowDecorated = null;
}
super.uninitialize();
}
@Override
protected void installOther(UIDefaults table)
{
// 使用自定义工厂, 设置Popup为透明, 否则无法使用阴影边框
// Use a custom factory, set the Popup to be transparent.
// otherwise you can not use the shadow border.
PopupFactory.setSharedInstance(new LuckPopupFactory());
}
public static void install() {
PopupFactory factory = PopupFactory.getSharedInstance();
if (factory instanceof RoundedPopupFactory) {
return;
}
PopupFactory.setSharedInstance(new RoundedPopupFactory(factory));
}
public static void uninstall() {
PopupFactory factory = PopupFactory.getSharedInstance();
if (!(factory instanceof RoundedPopupFactory)) {
return;
}
PopupFactory stored = ((RoundedPopupFactory) factory).storedFactory;
PopupFactory.setSharedInstance(stored);
}
/**
* This default implementation only setup the tool tip durations. Subclasses might override this
* method.
*/
protected void setupGUI() throws Exception {
System.setProperty(BookmarkIO.PROPERTY_BOOKMARKS_DIR, FileSystemService.getUserRapidMinerDir().getAbsolutePath());
System.setProperty(BookmarkIO.PROPERTY_BOOKMARKS_FILE, ".bookmarks");
try {
if (SystemInfoUtilities.getOperatingSystem() == OperatingSystem.OSX) {
// to support OS Xs menu bar shown in the OS X menu bar,
// we have to load the default system look and feel
// to exchange the MenuBarUI from RapidLookAndFeel with the
// default OS X look and feel UI class.
// See here for more information:
// http://www.pushing-pixels.org/2008/07/13/swing-applications-and-mac-os-x-menu-bar.html
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Map<String, Object> macUIDefaults = new HashMap<>();
macUIDefaults.put("MenuBarUI", UIManager.get("MenuBarUI"));
UIManager.setLookAndFeel(new RapidLookAndFeel(macUIDefaults));
// tooltips are painted behind heavyweight windows (e.g. the native Chromium browser window) on OS X
// despite the call above of ToolTipManager#setLightWeightPopupEnabled(false);
// so we force a heavyweight popup factory for OS X
PopupFactory.setSharedInstance(new HeavyweightOSXPopupFactory());
} else {
UIManager.setLookAndFeel(new RapidLookAndFeel());
}
} catch (Exception e) {
LogService.getRoot().log(Level.WARNING, I18N.getMessage(LogService.getRoot().getResourceBundle(),
"com.rapidminer.gui.RapidMinerGUI.setting_up_modern_look_and_feel_error"), e);
}
// needed because of native browser window which otherwise renders above all popup menus
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
}
/**
* Installs the ShadowPopupFactory as the shared popup factory
* on non-Mac platforms. Also stores the previously set factory,
* so that it can be restored in <code>#uninstall</code>.<p>
*
* In some Mac Java environments the popup factory throws
* a NullPointerException when we call <code>#getPopup</code>.<p>
*
* The Mac case shows that we may have problems replacing
* non PopupFactory instances. Therefore we should consider
* replacing only instances of PopupFactory.
*
* @see #uninstall()
*/
public static void install() {
final String os = System.getProperty("os.name");
final boolean macintosh = os != null && os.indexOf("Mac") != -1;
if (macintosh) {
return;
}
final PopupFactory factory = PopupFactory.getSharedInstance();
if (factory instanceof ShadowPopupFactory) {
return;
}
PopupFactory.setSharedInstance(new ShadowPopupFactory(factory));
}
/**
* Uninstalls the ShadowPopupFactory and restores the original
* popup factory as the new shared popup factory.
*
* @see #install()
*/
public static void uninstall() {
final PopupFactory factory = PopupFactory.getSharedInstance();
if (!(factory instanceof ShadowPopupFactory)) {
return;
}
final PopupFactory stored = ((ShadowPopupFactory) factory).storedFactory;
PopupFactory.setSharedInstance(stored);
}
/**
* Setup applications Look and Feel.
*/
private void setupLookAndFeel(ApplicationArguments args)
{
/*
* Don't prevent the user from overriding the laf is they choose to use
* Swing's default laf prop
*/
String userSpecifiedOverride = System.getProperty("swing.defaultlaf");
if (userSpecifiedOverride != null && !"".equals(userSpecifiedOverride)) { return; }
String lafClassName =
args.useNativeLAF() ? UIManager.getSystemLookAndFeelClassName() : MetalLookAndFeel.class.getName();
if (!args.useDefaultMetalTheme())
{
MetalLookAndFeel.setCurrentTheme(new AllBluesBoldMetalTheme());
}
try
{
// The following is a work-around for the problem on Mac OS X where
// the Apple LAF delegates to the Swing Popup factory but then
// tries to set a 90% alpha on the underlying Cocoa window, which
// will always be null if you're using JGoodies L&F
// see http://www.caimito.net/pebble/2005/07/26/1122392314480.html#comment1127522262179
// This has no effect on Linux/Windows
PopupFactory.setSharedInstance(new PopupFactory());
UIManager.setLookAndFeel(lafClassName);
}
catch (Exception ex)
{
// i18n[Application.error.setlaf=Error setting LAF]
s_log.error(s_stringMgr.getString("Application.error.setlaf"), ex);
}
}
@Override
public void initialize() {
if( SystemInfo.IS_MAC )
initializeAqua();
super.initialize();
// install popup factory
oldPopupFactory = PopupFactory.getSharedInstance();
PopupFactory.setSharedInstance( new FlatPopupFactory() );
// install mnemonic handler
mnemonicHandler = new MnemonicHandler();
mnemonicHandler.install();
// listen to desktop property changes to update UI if system font or scaling changes
if( SystemInfo.IS_WINDOWS ) {
// Windows 10 allows increasing font size independent of scaling:
// Settings > Ease of Access > Display > Make text bigger (100% - 225%)
desktopPropertyName = "win.messagebox.font";
} else if( SystemInfo.IS_LINUX ) {
// Linux/Gnome allows changing font in "Tweaks" app
desktopPropertyName = "gnome.Gtk/FontName";
// Linux/Gnome allows extra scaling and larger text:
// Settings > Devices > Displays > Scale (100% or 200%)
// Settings > Universal access > Large Text (off or on, 125%)
// "Tweaks" app > Fonts > Scaling Factor (0,5 - 3)
desktopPropertyName2 = "gnome.Xft/DPI";
}
if( desktopPropertyName != null ) {
desktopPropertyListener = e -> {
String propertyName = e.getPropertyName();
if( desktopPropertyName.equals( propertyName ) || propertyName.equals( desktopPropertyName2 ) )
reSetLookAndFeel();
else if( DESKTOPFONTHINTS.equals( propertyName ) ) {
if( UIManager.getLookAndFeel() instanceof FlatLaf ) {
putAATextInfo( UIManager.getLookAndFeelDefaults() );
updateUILater();
}
}
};
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addPropertyChangeListener( desktopPropertyName, desktopPropertyListener );
if( desktopPropertyName2 != null )
toolkit.addPropertyChangeListener( desktopPropertyName2, desktopPropertyListener );
toolkit.addPropertyChangeListener( DESKTOPFONTHINTS, desktopPropertyListener );
}
// Following code should be ideally in initialize(), but needs color from UI defaults.
// Do not move this code to getDefaults() to avoid side effects in the case that
// getDefaults() is directly invoked from 3rd party code. E.g. `new FlatLightLaf().getDefaults()`.
postInitialization = defaults -> {
// update link color in HTML text
Color linkColor = defaults.getColor( "Component.linkColor" );
if( linkColor != null ) {
new HTMLEditorKit().getStyleSheet().addRule(
String.format( "a { color: #%06x; }", linkColor.getRGB() & 0xffffff ) );
}
};
// enable/disable window decorations, but only if system property is either
// "true" or "false"; in other cases it is not changed
Boolean useWindowDecorations = FlatSystemProperties.getBooleanStrict( FlatSystemProperties.USE_WINDOW_DECORATIONS, null );
if( useWindowDecorations != null ) {
oldFrameWindowDecorated = JFrame.isDefaultLookAndFeelDecorated();
oldDialogWindowDecorated = JDialog.isDefaultLookAndFeelDecorated();
JFrame.setDefaultLookAndFeelDecorated( useWindowDecorations );
JDialog.setDefaultLookAndFeelDecorated( useWindowDecorations );
}
}
private static void tweakNimbusUI() {
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusOrange", defaults.get("nimbusBase"));
defaults.put("Table.gridColor", new Color(214, 217, 223));
defaults.put("Table.disabled", false);
defaults.put("Table.showGrid", true);
defaults.put("Table.intercellSpacing", new Dimension(1, 1));
defaults.put("CheckBoxMenuItem.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("RadioButtonMenuItem.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("MenuItem.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("Menu.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("Table.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("Label.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("TextField.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("TextArea.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("CheckBox.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("ComboBox.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("ToolTip.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("Button.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("TableHeader.font", new java.awt.Font("sansserif", 0, 11));
defaults.put("FileChooser.font", new java.awt.Font("sansserif", 0, 11));
/**
* custom tab-area border painter
*/
Painter tabborder = (Painter) (Graphics2D g, Object object, int width, int height) -> {
//add code to customize
};
defaults.put("TabbedPane:TabbedPaneTabArea[Disabled].backgroundPainter", tabborder);
defaults.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter", tabborder);
defaults.put("TabbedPane:TabbedPaneTabArea[Enabled+Pressed].backgroundPainter", tabborder);
defaults.put("TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter", tabborder);
PopupFactory.setSharedInstance(new PopupFactory() {
@Override
public Popup getPopup(Component owner, final Component contents, int x, int y) throws IllegalArgumentException {
Popup popup = super.getPopup(owner, contents, x, y);
SwingUtilities.invokeLater(() -> {
contents.repaint();
});
return popup;
}
});
}
protected void refreshUI() {
if (qPopupFactory == null) {
qPopupFactory = new QPopupFactory(PopupFactory.getSharedInstance());
}
if (toolTipTypeComboBox.getSelectedIndex() == 1) {
PopupFactory.setSharedInstance(qPopupFactory.getParentDelegate());
} else {
qPopupFactory.setToolTipCallout(!NONE.equals(calloutTypeComboBox
.getSelectedItem()));
PopupFactory.setSharedInstance(qPopupFactory);
}
boolean tooltipsActive = toolTipTypeComboBox.getSelectedIndex() != 2;
if (tooltipsActive) {
Font font = fontComboBox.getSelectedFont();
float size = fontSizeSlider.getValue();
font = font.deriveFont(size);
UIManager.getDefaults().put("ToolTip.font", font);
Color background = color.getColorSelectionModel()
.getSelectedColor();
Color foreground;
float[] hsl = HSLColor.fromRGB(background, null);
if (hsl[2] < .5) {
foreground = Color.white;
} else {
foreground = Color.black;
}
UIManager.getDefaults().put("ToolTip.background", background);
UIManager.getDefaults().put("ToolTip.foreground", foreground);
}
ToolTipManager.sharedInstance().setEnabled(
toolTipTypeComboBox.getSelectedIndex() != 2);
colorLabel.setVisible(tooltipsActive);
color.setVisible(tooltipsActive);
fontSizeLabel.setVisible(tooltipsActive);
fontSizeSlider.setVisible(tooltipsActive);
fontLabel.setVisible(tooltipsActive);
fontComboBox.setVisible(tooltipsActive);
calloutTypeLabel
.setVisible(toolTipTypeComboBox.getSelectedIndex() == 0);
calloutTypeComboBox
.setVisible(toolTipTypeComboBox.getSelectedIndex() == 0);
CalloutType type = null;
try {
type = CalloutType.valueOf((String) calloutTypeComboBox
.getSelectedItem());
} catch (Exception e) {
}
sampleButton.putClientProperty(QPopup.PROPERTY_CALLOUT_TYPE, type);
}
public void uninitialize()
{
PopupFactory.setSharedInstance(new PopupFactory());
}
/**
* Ui impl.
*/
public static void uiImpl()
{
PopupFactory.setSharedInstance(popupFactoryDIY);
}