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

前端 未结 3 1184
清歌不尽
清歌不尽 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-21 00:46

    I found the pane listener helps but due to the proprietary nature of my component it still didn't resize. I was using fxml and attempting to place the SwingNode inside a Pane for layout purposes. I then I noticed a number of the examples used a StackPane rather than just a Pane and suddently have just made this change the code worked. This was inside a AnchorPane which also seemed to ensure the initial display of the component filled all the available space. In summary a StackPane within the AnchorPane with all the Anchors set to 0 ensured the controlled filled all the initial available space and then did all the resizing, when the manual resize listeners where added it all started working.

    pane.widthProperty().addListener((w,o,n)->c.resizeChart((int)n.intValue(), (int)pane.getHeight()));
    pane.heightProperty().addListener((w,o,n)->c.resizeChart((int)pane.getWidth(), (int)n.intValue()));
    
    0 讨论(0)
  • 2020-12-21 00:51

    I'm not sure if its exactly the same case, but seems related in the sense of getting swing components to size properly with the parent containers. In my case I had a SwingNode containing a JFreeChart (ChartPanel), which I simply couldn't get to resize properly when the parent frame (a border pane within a SplitPane) was itself resized. In the end i simply added a listener to the height/width properties:

    pane.widthProperty().addListener((w,o,n)->c.resizeChart((int)n.intValue(), (int)pane.getHeight()));
    pane.heightProperty().addListener((w,o,n)->c.resizeChart((int)pane.getWidth(), (int)n.intValue()));
    

    Nothing else I tried could emulate this. Thanks

    0 讨论(0)
提交回复
热议问题