From a54aeb38382a567fccf3c15800ba36b1252d1e4e Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 29 May 2024 11:14:52 +0200 Subject: [PATCH] FlatTestFrame: automatically add scroll pane if content is very large (or screen is small) --- .../flatlaf/testing/FlatTestFrame.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java index 50676208..e516f6d0 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java @@ -340,7 +340,8 @@ public class FlatTestFrame if( menuBarFactory != null ) setJMenuBar( menuBarFactory.apply( content ) ); - contentPanel.add( content ); + addContentToContentPanel(); + pack(); setLocationRelativeTo( null ); setVisible( true ); @@ -350,6 +351,27 @@ public class FlatTestFrame } ); } + private void addContentToContentPanel() { + if( content instanceof JScrollPane ) { + contentPanel.add( content ); + return; + } + + Dimension contentSize = content.getPreferredSize(); + int buttonBarHeight = buttonBar.getPreferredSize().height; + Rectangle screenBounds = getGraphicsConfiguration().getBounds(); + + // add scroll pane if content is larger than screen + if( contentSize.width > screenBounds.width || + contentSize.height + buttonBarHeight > screenBounds.height ) + { + JScrollPane scrollPane = new JScrollPane( content ); + scrollPane.setBorder( BorderFactory.createEmptyBorder() ); + contentPanel.add( scrollPane ); + } else + contentPanel.add( content ); + } + private void selectLookAndFeel( String lafClassName ) { lookAndFeelComboBox.setSelectedLookAndFeel( lafClassName ); } @@ -697,9 +719,9 @@ public class FlatTestFrame } private void recreateContent() { - contentPanel.remove( content ); + contentPanel.removeAll(); content = contentFactory.get(); - contentPanel.add( content ); + addContentToContentPanel(); if( rightToLeftCheckBox.isSelected() ) rightToLeftChanged();