Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression (w/ fix): JFileChooser has irregular margins #604

Closed
LoadingByte opened this issue Oct 19, 2022 · 1 comment
Closed

Regression (w/ fix): JFileChooser has irregular margins #604

LoadingByte opened this issue Oct 19, 2022 · 1 comment
Milestone

Comments

@LoadingByte
Copy link

Consider the following minimal example, which draws a JFileChooser:

public class Main {
    public static void main(String[] args) {
        FlatLightLaf.setup();
        var frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new JFileChooser());
        frame.pack();
        frame.setVisible(true);
    }
}

In FlatLaf 2.2, this works as expected: the component's margins are precisely dictated by the new EmptyBorder(12, 12, 11, 11) set up by MetalFileChooserUI. You can measure pixels to convince yourself:

JFileChooser in FlatLaf 2.2

Starting with FlatLaf 2.3 however, there are 8 superfluous pixels of margin on the right:

JFileChooser in FlatLaf 2.3

I'm pretty sure that the culprit is this section of code, which repackages the topPanel, filePane, and bottomPanel in a wrapper JPanel, but forgets about the getAccessoryPanel() (AP). In addition, it increases the hgap of JFileChooser's BorderLayout from 0 to 8 in preparation for FlatLaf's shortcuts panel. Now, seeing that the empty AP is not repackaged but remains subject to JFileChooser's top-level BorderLayout with the constraint LINE_END, it becomes clear why there are exactly 8 additional pixels on the right.

I think the most straightforward solution is to simply not forget the AP, i.e.:

			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 );
			}

I've not tested this change myself, but I'm pretty certain it will resolve the issue.

Best,
Felix

DevCharly added a commit that referenced this issue Oct 21, 2022
… too large right margin (issue #604; regression since implementing PR #522 in FlatLaf 2.3)
@DevCharly
Copy link
Collaborator

Many thanks for the detailed report and the fix 👍

You're right, I forgot the (optional) accessory component.

This is the wrong layout of the accessory component in FlatLaf 2.3 - 2.5:

image

Fixed layout (same layout as in other Lafs and in FlatLaf 2.2 and older):

image

@DevCharly DevCharly added this to the 3.0 milestone Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants