mikaelgrev / miglayout

Official MiG Layout for Swing, SWT and JavaFX
miglayout.com
423 stars 76 forks source link

Does sizegroup work in javaFX? #106

Closed MrKuip closed 5 months ago

MrKuip commented 6 months ago

The following code doesn't show the buttons with an equal size. Does sizegroup work in javaFX?

package org.kku.jdiskusage.main;

import org.tbee.javafx.scene.layout.MigPane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Test
  extends Application
{
  @Override
  public void start(Stage stage)
  {
    MigPane pane;
    Scene scene;

    pane = new MigPane();
    pane.add(new Button("hello"), "sizegroup test");
    pane.add(new Button("Long text"), "sizegroup test");

    scene = new Scene(pane);
    stage.setScene(scene);

    stage.show();
  }

  public static void main(String[] args)
  {
    launch();
  }
}

This is the result: image

MrKuip commented 6 months ago

The same layout works perfect when used with swing


package org.kku.jdiskusage.main;

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class Test
{
  public Test()
  {
    start();
  }

  public void start()
  {
    JPanel pane;
    MigLayout layout;
    JFrame frame;

    layout = new MigLayout();
    pane = new JPanel();
    pane.setLayout(layout);
    pane.add("sizegroup test", new JButton("Text"));
    pane.add("sizegroup test", new JButton("Long text"));

    frame = new JFrame("Test");
    frame.setContentPane(pane);
    frame.setSize(new Dimension(600, 400));
    frame.show();
  }

  public static void main(String[] args)
  {
    new Test();
  }
}

image

tbee commented 6 months ago

Apparently not would be the answer. Which is weird, because both layouts share the same 'engine'. I am curious what the calculated sized is in each scenario.

MrKuip commented 5 months ago

Do you want me to debug and report the calculated sizes?

tbee commented 5 months ago

No. I need some time to look into it, but busy schedule.

tbee commented 5 months ago

Sigh, ever since the project was modularized, I can't get it to run in IntelliJ. Working on it.

tbee commented 5 months ago

Update. So, have it setup like a kludge, but I was able to figure out what goes wrong, but I'm not sure what the fix would be.

What happens is that during initial setup the max value in the sizegroup is correctly determined, and the component wrappers get their sizes set accordingly (grid.setForcedSizes). Then the container gets its size set. In MigPane (so JavaFX) this calls invalidateContainerSize, which causes the sizes of the components to be redetermined, undoing the sizegroup.

The question now is: why is the invalidateContainerSize necessary? Or (since MigLayout's algorithm is Mikael's, not mine): why does the invalidate also not apply the sizegroup logic again?

I suspect the invalidateContainerSize has to do with resizing the scene/stage (it has been too long), so maybe that is the wrong place, or there is no alternative place to react to that event.

MrKuip commented 5 months ago

As far as I can see the only module that calls invalidateContainerSize is the javafx module. So I removed the calls to invalidateContainerSize in javafx MigPane and I get the correct layout. Was invalidateContainerSize specifically added to Grid.java for JavaFX?

tbee commented 5 months ago

I don't know; that code was added by Mikael in 2016. He will have had a reason for it. I'll try to run the tests with the change in and see how they fare.

tbee commented 5 months ago

What also works is replacing invalidateContainerSize with invalidateGrid. That seems to make more sense; in 2018 I've added some caching logic where _grid is created less often. That is probably the cause, so basically that should be reverted. Or improved to include the sizegroup bounds, so also run the grid constructor code.

After a few tries I think I'm going for the invalidateGrid change.

MrKuip commented 5 months ago

I think you are right. invalidateContainerSize was invented to solve a bug. Just removing is not helpful

Did you also notice that lots of javafx test fail? The tests expect a layout that is pixel perfect. But if for instance the font is changed in the jdk then the tests will fail. Maybe that is the cause?

tbee commented 5 months ago

MigLayout uses different values on different OSes, so the test were written on/for Windows.

tbee commented 5 months ago

I've released 11.4 with the invalidateGrid change