[英]Interoperability between Swing and Javafx : Closing parent Javafx stage from embedded Swing application
我使用 SwingNode 在 JavaFx 應用程序中嵌入了 swing 應用程序,如下所示:
final SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent(new ButtonHtml());
}
});
ButtonHtml 是一個 swing 應用程序,如下所示:
public class ButtonHtml extends JPanel
implements ActionListener {
protected JButton b1, b3;
public ButtonHtml() {
ImageIcon buttonIcon = createImageIcon("images/down.gif");
b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
+ "<font color=#ffffdd>FX button</font>",
buttonIcon);
Font font = b1.getFont().deriveFont(Font.PLAIN);
b1.setFont(font);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
+ "<font color=#ffffdd>FX button</font>",
buttonIcon);
b3.setFont(font);
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);
b1.setToolTipText("Click this button to disable the FX button.");
b3.setToolTipText("Click this button to enable the FX button.");
//Add Components to this container, using the default FlowLayout.
add(b1);
add(b3);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
Platform.runLater(new Runnable() {
@Override
public void run() {
//close the parent javafx window
}
});
b1.setEnabled(false);
b3.setEnabled(true);
} else {
Platform.runLater(new Runnable() {
@Override
public void run() {
//do something
}
});
b1.setEnabled(true);
b3.setEnabled(false);
}
}
/** Returns an ImageIcon, or null if the path was invalid.
* @param path
* @return */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonHtml.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
現在在我的 javafx 應用程序中,我將 SwingNode 嵌入到窗格中並創建了一個場景並執行 stage.show 以打開 javafx window:
BorderPane pane = new BorderPane();
Image fxButtonIcon = new Image(getClass().getResourceAsStream("images/middle.gif"));
fxbutton = new Button("FX button", new ImageView(fxButtonIcon));
fxbutton.setTooltip(new Tooltip("This middle button does nothing when you click it."));
fxbutton.setStyle("-fx-font: 22 arial; -fx-base: #cce6ff;");
pane.setTop(swingNode);
pane.setCenter(fxbutton);
Scene scene = new Scene(pane, 300, 100);
stage.setScene(scene);
stage.setTitle("Enable FX Button");
stage.show();
我需要知道,我們如何從嵌入式 Swing 應用程序觸發父 Javafx window 的隱藏/處置/關閉。 基本上我想點擊一個按鈕(在上面的例子中說按鈕 b1),然后關閉 javafx window。 請注意,swing 應用程序和 javafx 應用程序是獨立的。
一個相當干凈的方法是在ButtonHtml
class 中定義一個字段,表示按下b1
時要做什么。
結合這一點,並對動作偵聽器實現進行現代化改造,您可以執行以下操作:
public class ButtonHtml extends JPanel {
protected JButton b1, b3;
private Runnable onCloseRequested = () -> {} ;
public void setOnCloseRequested(Runnable onCloseRequested) {
this.onCloseRequested = onCloseRequested ;
}
public Runnable getOnCloseRequested() {
return onCloseRequested ;
}
public ButtonHtml() {
ImageIcon buttonIcon = createImageIcon("images/down.gif");
b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
+ "<font color=#ffffdd>FX button</font>",
buttonIcon);
Font font = b1.getFont().deriveFont(Font.PLAIN);
b1.setFont(font);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
+ "<font color=#ffffdd>FX button</font>",
buttonIcon);
b3.setFont(font);
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(e -> {
Platform.runLater(onCloseRequested);
b1.setEnabled(false);
b3.setEnabled(true);
});
b3.addActionListener(e -> { /* ... */ });
b1.setToolTipText("Click this button to disable the FX button.");
b3.setToolTipText("Click this button to enable the FX button.");
//Add Components to this container, using the default FlowLayout.
add(b1);
add(b3);
}
/** Returns an ImageIcon, or null if the path was invalid.
* @param path
* @return */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonHtml.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
然后你可以做
final SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
ButtonHtml buttonHtml = new ButtonHtml();
buttonHtml.setOnCloseRequested(() -> swingNode.getScene().getWindow().hide());
swingNode.setContent(buttonHtml);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.