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

Limit size of child to bounds of parent when applying visualPadding #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

weisJ
Copy link

@weisJ weisJ commented May 4, 2023

If child components have e.g. the fillx constraint and also visualPaddings specified the child may be layed out outside the parents bounds. This results in e.g. focus rings not be painted on those side.

Here is a small example that demonstrated the problem:

SwingUtilities.invokeLater(() -> {
    JFrame frame = new JFrame("Test");

    JPanel titlePanel = new JPanel(new MigLayout("fillx, insets 0", "[fill]"));

    JPanel overflowing = new JPanel();
    overflowing.setMinimumSize(new Dimension(50, 50));
    int thickness = 5;
    overflowing.setBorder(new LineBorder(Color.MAGENTA, thickness));
    overflowing.putClientProperty("visualPadding", new Insets(thickness, thickness, thickness, thickness));

    titlePanel.add(overflowing);

    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(new CompoundBorder(
            new EmptyBorder(30, 30, 30, 30),
            new LineBorder(Color.BLACK)
    ));
    panel.add(titlePanel, BorderLayout.NORTH);
    panel.add(new JPanel(), BorderLayout.CENTER);

    frame.setContentPane(panel);

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
});

Without the patch the magenta border will be invisible.

I am not fully sure it is the correct approach. Maybe there is some scenario where one would actually want a child to extend outside the parent. But I wasn't able to track down the code where fill constraints are evaluated to patch it at that point already.

Any feedback is highly appreciated.

@DevCharly
Copy link
Contributor

I'm also interested in a solution for this problem. It occurs often when nesting panels.

Here is a screenshot that shows the problem (cut-off focus border if nested panel uses insets 0 in layout constraints):

image

This PR works, it simply reduces the size of the components if they are outside of the container.
But there are some problems with this approach.

The components no longer have their preferred sizes.

Here is an example that shows that button height gets too small
(left is without this PR, right is with this PR):

image

Another problem is that if components are "intentionally" placed partly (or fully) outside of the container, then this PR "forces" them into the container bounds.

A "manual" fix for the issue would be to increase the layout insets for the nested panels where necessary (e.g. change layout constraints from insets 0 to insets 3 0 3 3). But this is error-prone and it does not work if the application also supports L&Fs that do not provide visualPaddings or where visualPadding is always 0,0,0,0.

It would be great to find a solution that automatically increases the container insets, but only on those sides where it is necessary.
This should also increase the preferred size of the container.

But have no idea where and how to implement this. I think it has to be done very late in the layout calculation because the bounds of the container and of all children is needed to find out whether a component is located at one of the four sides.

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

Successfully merging this pull request may close these issues.

None yet

2 participants