Skip to content

Commit

Permalink
OptionPane: fixed styling custom panel background in JOptionPane (i…
Browse files Browse the repository at this point in the history
…ssue #761)
  • Loading branch information
DevCharly committed Nov 14, 2023
1 parent 61ba011 commit 7d0bdf3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ FlatLaf Change Log
foreground color. (issue 756)
- Table: Switching theme looses table grid and intercell spacing. (issues #733
and #750)
- OptionPane: Fixed styling custom panel background in `JOptionPane`. (issue
#761)


## 3.2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicOptionPaneUI;
import com.formdev.flatlaf.FlatClientProperties;
Expand Down Expand Up @@ -115,13 +113,6 @@ protected void installDefaults() {
sameSizeButtons = FlatUIUtils.getUIBoolean( "OptionPane.sameSizeButtons", true );
}

@Override
protected void installComponents() {
super.installComponents();

updateChildPanels( optionPane );
}

@Override
protected PropertyChangeListener createPropertyChangeListener() {
PropertyChangeListener superListener = super.createPropertyChangeListener();
Expand Down Expand Up @@ -155,6 +146,13 @@ protected int getMaxCharactersPerLineCount() {
protected Container createMessageArea() {
Container messageArea = super.createMessageArea();

// use non-UIResource OptionPane.messageAreaBorder to avoid that it is replaced when switching LaF
// and make panel non-opaque for OptionPane.background
updateAreaPanel( messageArea );

// make known sub-panels non-opaque for OptionPane.background
updateKnownChildPanels( messageArea );

// set icon-message gap
if( iconMessageGap > 0 ) {
Component iconMessageSeparator = SwingUtils.getComponentByName( messageArea, "OptionPane.separator" );
Expand All @@ -169,6 +167,10 @@ protected Container createMessageArea() {
protected Container createButtonArea() {
Container buttonArea = super.createButtonArea();

// use non-UIResource OptionPane.buttonAreaBorder to avoid that it is replaced when switching LaF
// and make panel non-opaque for OptionPane.background
updateAreaPanel( buttonArea );

// scale button padding and subtract focusWidth
if( buttonArea.getLayout() instanceof ButtonAreaLayout ) {
ButtonAreaLayout layout = (ButtonAreaLayout) buttonArea.getLayout();
Expand Down Expand Up @@ -218,22 +220,33 @@ protected void addMessageComponents( Container container, GridBagConstraints con
super.addMessageComponents( container, cons, msg, maxll, internallyCreated );
}

private void updateChildPanels( Container c ) {
for( Component child : c.getComponents() ) {
if( child.getClass() == JPanel.class ) {
JPanel panel = (JPanel)child;
private void updateAreaPanel( Container area ) {
if( !(area instanceof JPanel) )
return;

// make sub-panel non-opaque for OptionPane.background
panel.setOpaque( false );
// use non-UIResource border to avoid that it is replaced when switching LaF
// and make panel non-opaque for OptionPane.background
JPanel panel = (JPanel) area;
panel.setBorder( FlatUIUtils.nonUIResource( panel.getBorder() ) );
panel.setOpaque( false );
}

// use non-UIResource borders to avoid that they are replaced when switching LaF
Border border = panel.getBorder();
if( border instanceof UIResource )
panel.setBorder( FlatUIUtils.nonUIResource( border ) );
private void updateKnownChildPanels( Container c ) {
for( Component child : c.getComponents() ) {
if( child instanceof JPanel && child.getName() != null ) {
switch( child.getName() ) {
case "OptionPane.realBody":
case "OptionPane.body":
case "OptionPane.separator":
case "OptionPane.break":
// make known sub-panels non-opaque for OptionPane.background
((JPanel)child).setOpaque( false );
break;
}
}

if( child instanceof Container )
updateChildPanels( (Container) child );
updateKnownChildPanels( (Container) child );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ public static void main( String[] args ) {
FlatOptionPaneTest() {
initComponents();

JPanel panel = new JPanel();
panel.setBackground( Color.green );
panel.add( new JLabel( "label in green panel" ) );
customOptionPane.setMessage( new Object[] {
"string",
"multi-\nline string",
new JCheckBox( "check box" ),
new JTextField( "text field" ),
panel,
"more text",
} );
customOptionPane.setOptions( new Object[] {
Expand Down

0 comments on commit 7d0bdf3

Please sign in to comment.