From b1d24680b210b23627127e26c3edd2a518952e5b Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 1 Aug 2020 22:53:09 +0200 Subject: [PATCH] ToolTip: fixed truncated text in HTML formatted tooltip on HiDPI displays (issue #142) --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/ui/FlatPopupFactory.java | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73acdaeb..329f7685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ FlatLaf Change Log close buttons are hidden. (issue #132) - ToolTip: Do not show empty tooltip component if tooltip text is an empty string. (issue #134) +- ToolTip: Fixed truncated text in HTML formatted tooltip on HiDPI displays. + (issue #142) - ComboBox: Fixed width of popup, which was too small if popup is wider than combo box and vertical scroll bar is visible. (issue #137) - MenuItem on macOS: Removed plus characters from accelerator text and made diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java index a38bc588..f4f2f50e 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java @@ -32,6 +32,7 @@ import java.lang.reflect.Method; import javax.swing.JComponent; import javax.swing.JLayeredPane; import javax.swing.JPanel; +import javax.swing.JToolTip; import javax.swing.Popup; import javax.swing.PopupFactory; import javax.swing.RootPaneContainer; @@ -132,6 +133,7 @@ public class FlatPopupFactory extends Popup { private Popup delegate; + private Component contents; // heavy weight protected Window popupWindow; @@ -139,6 +141,7 @@ public class FlatPopupFactory NonFlashingPopup( Popup delegate, Component contents ) { this.delegate = delegate; + this.contents = contents; popupWindow = SwingUtilities.windowForComponent( contents ); if( popupWindow != null ) { @@ -153,8 +156,25 @@ public class FlatPopupFactory @Override public void show() { - if( delegate != null ) + if( delegate != null ) { delegate.show(); + + // increase tooltip size if necessary because it may be too small on HiDPI screens + // https://bugs.openjdk.java.net/browse/JDK-8213535 + if( contents instanceof JToolTip ) { + Container parent = contents.getParent(); + if( parent instanceof JPanel ) { + Dimension prefSize = parent.getPreferredSize(); + if( !prefSize.equals( parent.getSize() ) ) { + Container panel = SwingUtilities.getAncestorOfClass( Panel.class, parent ); + if( panel != null ) + panel.setSize( prefSize ); // for medium weight popup + else + parent.setSize( prefSize ); // for light weight popup + } + } + } + } } @Override @@ -162,6 +182,7 @@ public class FlatPopupFactory if( delegate != null ) { delegate.hide(); delegate = null; + contents = null; } if( popupWindow != null ) {