Skip to content

Commit

Permalink
FileChooser: fixed layout of (optional) accessory component and fixed…
Browse files Browse the repository at this point in the history
… too large right margin (issue #604; regression since implementing PR #522 in FlatLaf 2.3)
  • Loading branch information
DevCharly committed Oct 21, 2022
1 parent f215356 commit b24e2db
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 35 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,15 @@
FlatLaf Change Log
==================

## 3.0-SNAPSHOT

#### Fixed bugs

- FileChooser: Fixed layout of (optional) accessory component and fixed too
large right margin. (issue #604; regression since implementing PR #522 in
FlatLaf 2.3)


## 2.6

#### New features and improvements
Expand Down
Expand Up @@ -243,11 +243,13 @@ private void patchUI( JFileChooser fc ) {
borderLayout.setHgap( 8 );

Component north = borderLayout.getLayoutComponent( BorderLayout.NORTH );
Component lineEnd = borderLayout.getLayoutComponent( BorderLayout.LINE_END );
Component center = borderLayout.getLayoutComponent( BorderLayout.CENTER );
Component south = borderLayout.getLayoutComponent( BorderLayout.SOUTH );
if( north != null && center != null && south != null ) {
if( north != null && lineEnd != null && center != null && south != null ) {
JPanel p = new JPanel( new BorderLayout( 0, 11 ) );
p.add( north, BorderLayout.NORTH );
p.add( lineEnd, BorderLayout.LINE_END );
p.add( center, BorderLayout.CENTER );
p.add( south, BorderLayout.SOUTH );
fc.add( p, BorderLayout.CENTER );
Expand Down
Expand Up @@ -16,11 +16,13 @@

package com.formdev.flatlaf.testing;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import javax.swing.*;
import javax.swing.border.*;
import com.formdev.flatlaf.icons.FlatFileChooserHomeFolderIcon;
import net.miginfocom.swing.*;

Expand Down Expand Up @@ -59,12 +61,34 @@ public static void main( String[] args ) {
initComponents();
}

private void showShortcuts() {
UIManager.put( "FileChooser.noPlacesBar", !showShortcutsCheckBox.isSelected() ? true : null );
fileChooser1.updateUI();
}

private void showAccessory() {
JPanel accessory = null;
if( showAccessoryCheckBox.isSelected() ) {
accessory = new JPanel( new BorderLayout() );
accessory.setBackground( Color.green );
accessory.add( new JLabel( " Accessory " ), BorderLayout.CENTER );
}
fileChooser1.setAccessory( accessory );
fileChooser1.revalidate();
fileChooser1.repaint();
}

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel colorChooserLabel = new JLabel();
JPanel panel2 = new JPanel();
JColorChooser colorChooser1 = new JColorChooser();
JLabel fileChooserLabel = new JLabel();
JFileChooser fileChooser1 = new JFileChooser();
JPanel panel1 = new JPanel();
fileChooser1 = new JFileChooser();
JPanel panel3 = new JPanel();
showShortcutsCheckBox = new JCheckBox();
showAccessoryCheckBox = new JCheckBox();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
Expand All @@ -86,64 +110,105 @@ private void initComponents() {
// rows
"[top]" +
"[grow,fill]" +
"[]" +
"[]"));

//---- colorChooserLabel ----
colorChooserLabel.setText("JColorChooser:");
add(colorChooserLabel, "cell 0 0");
add(colorChooser1, "cell 1 0");

//======== panel2 ========
{
panel2.setBorder(new MatteBorder(4, 4, 4, 4, Color.red));
panel2.setLayout(new BorderLayout());
panel2.add(colorChooser1, BorderLayout.CENTER);
}
add(panel2, "cell 1 0");

//---- fileChooserLabel ----
fileChooserLabel.setText("JFileChooser:");
add(fileChooserLabel, "cell 0 1");
add(fileChooser1, "cell 1 1,growx");
add(fileChooserLabel, "cell 0 1,aligny top,growy 0");

//======== panel1 ========
{
panel1.setBorder(new MatteBorder(4, 4, 4, 4, Color.red));
panel1.setLayout(new BorderLayout());
panel1.add(fileChooser1, BorderLayout.CENTER);
}
add(panel1, "cell 1 1,growx");

//======== panel3 ========
{
panel3.setLayout(new MigLayout(
"hidemode 3",
// columns
"[fill]" +
"[fill]",
// rows
"[]"));

//---- showShortcutsCheckBox ----
showShortcutsCheckBox.setText("Show Shortcuts");
showShortcutsCheckBox.setSelected(true);
showShortcutsCheckBox.addActionListener(e -> showShortcuts());
panel3.add(showShortcutsCheckBox, "cell 0 0");

//---- showAccessoryCheckBox ----
showAccessoryCheckBox.setText("Show Accessory");
showAccessoryCheckBox.addActionListener(e -> showAccessory());
panel3.add(showAccessoryCheckBox, "cell 1 0");
}
add(panel3, "cell 1 2");

//---- label1 ----
label1.setText("icons:");
add(label1, "cell 0 2");
add(label1, "cell 0 3");

//---- label2 ----
label2.setIcon(UIManager.getIcon("FileView.directoryIcon"));
add(label2, "cell 1 2");
add(label2, "cell 1 3");

//---- label3 ----
label3.setIcon(UIManager.getIcon("FileView.fileIcon"));
add(label3, "cell 1 2");
add(label3, "cell 1 3");

//---- label4 ----
label4.setIcon(UIManager.getIcon("FileView.computerIcon"));
add(label4, "cell 1 2");
add(label4, "cell 1 3");

//---- label5 ----
label5.setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
add(label5, "cell 1 2");
add(label5, "cell 1 3");

//---- label6 ----
label6.setIcon(UIManager.getIcon("FileView.floppyDriveIcon"));
add(label6, "cell 1 2");
add(label6, "cell 1 3");

//---- label7 ----
label7.setIcon(UIManager.getIcon("FileChooser.newFolderIcon"));
add(label7, "cell 1 2");
add(label7, "cell 1 3");

//---- label8 ----
label8.setIcon(UIManager.getIcon("FileChooser.upFolderIcon"));
add(label8, "cell 1 2");
add(label8, "cell 1 3");

//---- label9 ----
label9.setIcon(UIManager.getIcon("FileChooser.homeFolderIcon"));
add(label9, "cell 1 2");
add(label9, "cell 1 3");

//---- label10 ----
label10.setIcon(UIManager.getIcon("FileChooser.detailsViewIcon"));
add(label10, "cell 1 2");
add(label10, "cell 1 3");

//---- label11 ----
label11.setIcon(UIManager.getIcon("FileChooser.listViewIcon"));
add(label11, "cell 1 2");
add(label11, "cell 1 3");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JFileChooser fileChooser1;
private JCheckBox showShortcutsCheckBox;
private JCheckBox showAccessoryCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "15" encoding: "UTF-8"
JFDML JFormDesigner: "8.0.0.0.194" Java: "17.0.2" encoding: "UTF-8"

new FormModel {
contentType: "form/swing"
Expand All @@ -9,7 +9,7 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[][grow]"
"$rowConstraints": "[top][grow,fill][]"
"$rowConstraints": "[top][grow,fill][][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
Expand All @@ -18,91 +18,136 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JColorChooser" ) {
name: "colorChooser1"
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) {
name: "panel2"
"border": new javax.swing.border.MatteBorder( 4, 4, 4, 4, sfield java.awt.Color red )
add( new FormComponent( "javax.swing.JColorChooser" ) {
name: "colorChooser1"
}, new FormLayoutConstraints( class java.lang.String ) {
"value": "Center"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "fileChooserLabel"
"text": "JFileChooser:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
"value": "cell 0 1,aligny top,growy 0"
} )
add( new FormComponent( "javax.swing.JFileChooser" ) {
name: "fileChooser1"
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) {
name: "panel1"
"border": new javax.swing.border.MatteBorder( 4, 4, 4, 4, sfield java.awt.Color red )
add( new FormComponent( "javax.swing.JFileChooser" ) {
name: "fileChooser1"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
}, new FormLayoutConstraints( class java.lang.String ) {
"value": "Center"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1,growx"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "hidemode 3"
"$columnConstraints": "[fill][fill]"
"$rowConstraints": "[]"
} ) {
name: "panel3"
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "showShortcutsCheckBox"
"text": "Show Shortcuts"
"selected": true
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "showShortcuts", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "showAccessoryCheckBox"
"text": "Show Accessory"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "showAccessory", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label1"
"text": "icons:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
"value": "cell 0 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label2"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.directoryIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label3"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.fileIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label4"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.computerIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label5"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.hardDriveIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label6"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.floppyDriveIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label7"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileChooser.newFolderIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label8"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileChooser.upFolderIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label9"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileChooser.homeFolderIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label10"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileChooser.detailsViewIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label11"
"icon": new com.jformdesigner.model.SwingIcon( 2, "FileChooser.listViewIcon" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 790, 715 )
"size": new java.awt.Dimension( 790, 790 )
} )
}
}

0 comments on commit b24e2db

Please sign in to comment.