RFR: 8154846: SwingNode does not resize when content size constraints are changed [v2]

Andy Goryachev angorya at openjdk.org
Thu Oct 5 19:12:03 UTC 2023


On Wed, 4 Oct 2023 05:48:59 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>> SwingNode does not update its internal cache of Swing pref/max/min height and widths when its JComponent content's corresponding size constraints are updated. As such, it isn't resized to honor those size constraints. 
>> 
>> JLightweightFrame does install a PropertyChangeListener for "preferredSize", "maximumSize", and "minimumSize" properties, but this only happens via a ContainerListener which is not added until after the content has already been added to the content pane, and since the application cannot call this methods directly as per the documentation for the SwingNode.resize() method: `Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout.`
>> 
>> so the fix is to add the listener as soon as the component is added to the JLightweightFrame's content.
>
> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Register listener before component add

While running the SCCE in the ticket with the jdk compiled with the fix from pull/15960 on macOS 13.5.2 M1, noticed two things:

1. the very first time it works as expected.  There is a bit of flicker when the button gets replaced, perhaps it's due to runLater and invokeLater and debug output to stdout.
2. subsequent runs exhibit a strange behavior where the button is not positioned at the center.  Sometimes it's at the corner, and one time it was nowhere to be seen (the whole window was red).  Example:

<img width="612" alt="Screenshot 2023-10-05 at 11 59 14" src="https://github.com/openjdk/jdk/assets/107069028/9965858c-d0b6-4bb3-8bb9-e0b9c1465476">

3. when clicking the button to make the size exceed the window size, I've got OOME.  Perhaps it's expected:


Button size: java.awt.Dimension[width=3296,height=928]
SwingNode size: 3296.0 928.0
Button size: java.awt.Dimension[width=6592,height=1856]
SwingNode size: 6592.0 1856.0
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:76)
	at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:538)
	at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.desktop/java.awt.image.BufferedImage.<init>(BufferedImage.java:351)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.createImage(AquaPainter.java:193)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.lambda$paintFromSingleCachedImage$0(AquaPainter.java:178)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter$$Lambda/0x00000007702d7020.apply(Unknown Source)
	at java.desktop/sun.awt.image.MultiResolutionCachedImage.getResolutionVariant(MultiResolutionCachedImage.java:84)
	at java.desktop/sun.java2d.SunGraphics2D.getResolutionVariant(SunGraphics2D.java:3305)
	at java.desktop/sun.java2d.SunGraphics2D.drawHiDPIImage(SunGraphics2D.java:3133)
	at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3371)
	at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3321)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.paintFromSingleCachedImage(AquaPainter.java:186)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.paint(AquaPainter.java:141)
	at java.desktop/com.apple.laf.AquaPainter.paint(AquaPainter.java:90)
	at java.desktop/com.apple.laf.AquaButtonBorder.doButtonPaint(AquaButtonBorder.java:104)
	at java.desktop/com.apple.laf.AquaButtonBorder$Dynamic.doButtonPaint(AquaButtonBorder.java:241)
	at java.desktop/com.apple.laf.AquaButtonBorder.paintButton(AquaButtonBorder.java:100)
	at java.desktop/com.apple.laf.AquaButtonUI.paint(AquaButtonUI.java:316)
	at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
	at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:852)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1128)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:961)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1137)
	at java.desktop/sun.swing.JLightweightFrame$3.paint(JLightweightFrame.java:343)
	at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5318)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1656)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1631)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1569)
	at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1336)
	at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5266)
	at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5076)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:76)
	at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:538)
	at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.desktop/java.awt.image.BufferedImage.<init>(BufferedImage.java:351)
	at java.desktop/sun.swing.JLightweightFrame.resizeBuffer(JLightweightFrame.java:458)
	at java.desktop/sun.swing.JLightweightFrame.reshape(JLightweightFrame.java:443)
	at java.desktop/java.awt.Component.setBounds(Component.java:2339)
	at java.desktop/java.awt.Window.setBounds(Window.java:3560)
	at jdk.unsupported.desktop/jdk.swing.interop.LightweightFrameWrapper.setBounds(LightweightFrameWrapper.java:110)
	at javafx.swing/com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.setBounds(SwingNodeInteropN.java:160)
	at javafx.swing/javafx.embed.swing.SwingNode.lambda$20(SwingNode.java:707)
	at javafx.swing/javafx.embed.swing.SwingNode$$Lambda/0x0000000770234888.run(Unknown Source)
	at javafx.swing/com.sun.javafx.embed.swing.SwingNodeHelper.runOnEDT(SwingNodeHelper.java:170)
	at javafx.swing/javafx.embed.swing.SwingNode.locateLwFrame(SwingNode.java:704)
	at javafx.swing/javafx.embed.swing.SwingNode.lambda$17(SwingNode.java:486)
	at javafx.swing/javafx.embed.swing.SwingNode$$Lambda/0x0000000770234ee8.run(Unknown Source)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
	at java.base/java.security.AccessController.executePrivileged(AccessController.java:778)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:76)
	at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:538)
	at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.desktop/java.awt.image.BufferedImage.<init>(BufferedImage.java:351)
	at java.desktop/sun.swing.JLightweightFrame.resizeBuffer(JLightweightFrame.java:458)
	at java.desktop/sun.swing.JLightweightFrame.reshape(JLightweightFrame.java:443)
	at java.desktop/java.awt.Component.setBounds(Component.java:2339)
	at java.desktop/java.awt.Window.setBounds(Window.java:3560)
	at jdk.unsupported.desktop/jdk.swing.interop.LightweightFrameWrapper.setBounds(LightweightFrameWrapper.java:110)
	at javafx.swing/com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN.setBounds(SwingNodeInteropN.java:160)
	at javafx.swing/javafx.embed.swing.SwingNode.lambda$20(SwingNode.java:707)
	at javafx.swing/javafx.embed.swing.SwingNode$$Lambda/0x0000000770234888.run(Unknown Source)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
	at java.base/java.security.AccessController.executePrivileged(AccessController.java:778)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:76)
	at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:538)
	at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.desktop/java.awt.image.BufferedImage.<init>(BufferedImage.java:351)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.createImage(AquaPainter.java:193)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.lambda$paintFromSingleCachedImage$0(AquaPainter.java:178)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter$$Lambda/0x00000007702d7020.apply(Unknown Source)
	at java.desktop/sun.awt.image.MultiResolutionCachedImage.getResolutionVariant(MultiResolutionCachedImage.java:84)
	at java.desktop/sun.java2d.SunGraphics2D.getResolutionVariant(SunGraphics2D.java:3305)
	at java.desktop/sun.java2d.SunGraphics2D.drawHiDPIImage(SunGraphics2D.java:3133)
	at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3371)
	at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3321)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.paintFromSingleCachedImage(AquaPainter.java:186)
	at java.desktop/com.apple.laf.AquaPainter$AquaSingleImagePainter.paint(AquaPainter.java:141)
	at java.desktop/com.apple.laf.AquaPainter.paint(AquaPainter.java:90)
	at java.desktop/com.apple.laf.AquaButtonBorder.doButtonPaint(AquaButtonBorder.java:104)
	at java.desktop/com.apple.laf.AquaButtonBorder$Dynamic.doButtonPaint(AquaButtonBorder.java:241)
	at java.desktop/com.apple.laf.AquaButtonBorder.paintButton(AquaButtonBorder.java:100)
	at java.desktop/com.apple.laf.AquaButtonUI.paint(AquaButtonUI.java:316)
	at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
	at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:852)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1128)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:961)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1137)
	at java.desktop/sun.swing.JLightweightFrame$3.paint(JLightweightFrame.java:343)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:961)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1137)
	at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:961)
	at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5325)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1656)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1631)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15960#issuecomment-1749491912


More information about the client-libs-dev mailing list