How to resize Swing control which is inside SwingNode in JavaFX8?

前端 未结 3 1191
清歌不尽
清歌不尽 2020-12-21 00:16

How to resize Swing control which is inside SwingNode in JavaFX8?

Sometimes, I has controls resized inside SwingNode. But

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 00:27

    After fighting for hours with basically the same issue, I finally figured out what was going on.

    Basically, the problem is that the parent of the SwingNode is trying to set its size when layout occurs, based on the size of the parent. So when you resize your button, and then trigger a layout, the parent of the SwingNode sets it back to its default size. This is occurring because SwingNode overrides the isResizable() method to return true, giving permission to its parent objects to resize it.

    In order to avoid this, you can:

    • Create a custom subclass of SwingNode which overrides isResizable() to false,

    or:

    • Call setAutosizeChildren(false) on the Group which contains the SwingNode.

    The latter technique will probably need to be used if you are defining your classes in FXML.

    Note, by the way, that you can still call resize(width,height) on a SwingNode even if it overrides isResizable() to false.

提交回复
热议问题