Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

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

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

Sometimes, I has controls resized inside SwingNode. But SwingNode seems to resist this.

It is said in resize() apidoc, that

Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout.

But apparently it does not work.

Example code is below.

The question is: how to allow control to turn bigger?

public class Try_Sizes_01 extends Application {

    private static final Logger log = LoggerFactory.getLogger(Try_Sizes_01.class);

    private static final String text = "Very Long Text For Appear On Button ";
    private static int position = 7;

    //private JButton button = new JButton("short");
    private JButton button = new JButton(text.substring(0, position));

    private SwingNode swingNode = new SwingNode();
    {
        swingNode.setContent(button);
    }

    @Override
    public void start(Stage stage) throws Exception {


        Group group = new Group();
        group.getChildren().add(swingNode);

        Scene scene = new Scene(group);

        stage.setTitle("Try_Sizes_01");
        stage.setScene(scene);
        stage.show();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                new Timer(1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        /*
                        button.setText(button.getText() + text.charAt(position));

                        position++;
                        if( position >= text.length() ) {
                            position=0;
                        }
                        */

                        button.setPreferredSize(new Dimension((int)button.getPreferredSize().getWidth()+10, (int)button.getPreferredSize().getHeight()));
                        button.setMinimumSize(new Dimension((int)button.getPreferredSize().getWidth(), (int)button.getPreferredSize().getHeight()));
                        //button.revalidate();

                        //button.setBounds(0, 0, (int)button.getBounds().getWidth()+10, (int)button.getBounds().getHeight());

                        Platform.runLater(new Runnable() {

                            @Override
                            public void run() {

                                //swingNode.autosize(); // does not work
                                //swingNode.resize(button.getBounds().getWidth(), button.getBounds().getHeight()); // does not work and cancels button resizing
                                //swingNode.setContent(button); // works sometimes but imperfect


                            }
                        });

                        log.info("Swing thread");
                        log.info("Preferred width is now = {}", button.getPreferredSize().getWidth());
                        log.info("Bounds width is now = {}", button.getBounds().getWidth());

                    }
                }).start();

            }
        });

    }

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...