1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package helloswingnode; 33 34 import java.net.URL; 35 import java.util.ResourceBundle; 36 import javafx.embed.swing.SwingNode; 37 import javafx.event.ActionEvent; 38 import javafx.fxml.FXML; 39 import javafx.scene.control.Button; 40 import javax.swing.SwingUtilities; 41 42 /** 43 * The controller for our 'HelloSwingNode' application, see 44 * 'HelloSwingNode.fxml'. This class has all the logic to instantiate the 2 45 * buttons (FX button and Swing button) as well as the logic to handle the click 46 * action on the FX button. 47 */ 48 public class HelloSwingNodeController { 49 50 @FXML // ResourceBundle that was given to the FXMLLoader 51 private ResourceBundle resources; 52 53 @FXML // URL location of the FXML file that was given to the FXMLLoader 54 private URL location; 55 56 @FXML // fx:id="fxButton" 57 private Button fxButton; // Value injected by FXMLLoader 58 59 @FXML // fx:id="swingNode" 60 private SwingNode swingNode; // Value injected by FXMLLoader 61 62 private SwingButton swingButton; 63 private boolean enabled; 64 65 /** 66 * Called by the FXMLLoader when initialization is complete 67 * @param url 68 * @param rb 69 */ 70 @FXML // This method is called by the FXMLLoader when initialization is complete 71 void initialize() { 72 assert swingNode != null : "fx:id=\"swingNode\" was not injected: check your FXML file 'HelloSwingNode.fxml'."; 73 assert fxButton != null : "fx:id=\"fxButton\" was not injected: check your FXML file 'HelloSwingNode.fxml'."; 74 75 createSwingContent(swingNode); 76 enabled = true; 77 } 78 79 /** 80 * Called when the FX Button is fired. The click action alternatively 81 * disables/enables the Swing Button 82 * 83 * @param event the action event 84 */ 85 @FXML 86 void handleButtonAction(ActionEvent event) { 87 if (enabled) { 88 // if Swing Button is currently enabled, then disable it, 89 // and update text and tooltip of FX Button so it can be used now 90 // to re-enable the Swing button 91 SwingUtilities.invokeLater(() -> { 92 swingButton.setEnabled(false); 93 }); 94 enabled = false; 95 fxButton.setText("Enable Swing Button"); 96 fxButton.getTooltip().setText("Click this button to enable the Swing button"); 97 } else { 98 // if Swing Button is currently disabled, then enable it, 99 // and update text and tooltip of FX Button so it can be used now 100 // to disable the Swing button 101 SwingUtilities.invokeLater(() -> { 102 swingButton.setEnabled(true); 103 }); 104 enabled = true; 105 fxButton.setText("Disable Swing Button"); 106 fxButton.getTooltip().setText("Click this button to disable the Swing button"); 107 } 108 } 109 110 // Instantiates our SwingButton and sets it as the content of the SwingNode. 111 // The SwingButton will in turn be able to disable/enable the FX button (passed as an arg). 112 // All this has to be done on the Swing event thread. 113 private void createSwingContent(final SwingNode swingNode) { 114 SwingUtilities.invokeLater(() -> { 115 swingButton = new SwingButton(fxButton); 116 swingNode.setContent(swingButton); 117 }); 118 } 119 }