2017-01-13 8 views
0

私はSwingで書かれたコンポーネント(JPanel)をJavaFXアプリケーションに入れようとしています。 click hereFXFファイルとシーンビルダを持つJavaFXアプリケーションにSwingコンテンツを埋め込む

私はFXMLファイルとシーンビルダを使用してアプリケーションを構築しています。私は(それが下の画像の上にあったているように)SwingNodeとペインコンテナに私のSwingコンポーネントを入れしようとしていた。

scene builder

しかし残念ながら、私はそれを行うために管理していませんでした。私はどこにでもいて、私の場合は何も役に立たなかった。

私はJavaFXにはかなり新しく、シーンビルダを使用することは本当に重要です。 それは私が可能にしようとしていることですか?

私の試練:

Main.JAVA

package application; 


import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Letter Recognition with Neural Networks"); 
     primaryStage.setScene(new Scene(root, 1150, 650)); 
     primaryStage.setResizable(false); 
     primaryStage.show(); 
    } 

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

} 

Controller.JAVA

package application; 

import application.gui.DrawingPanel; 
import javafx.embed.swing.SwingNode; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.StackPane; 

import javax.swing.*; 
import java.net.URL; 
import java.util.ResourceBundle; 

public class Controller implements Initializable { 

    @FXML 
    private Pane pane; 

    @FXML 
    private SwingNode swingNode; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     swingNode = new SwingNode(); 
     pane = new Pane(); 

     createAndSetSwingDrawingPanel(swingNode); 
     pane.getChildren().add(swingNode); 
    } 

    public void createAndSetSwingDrawingPanel(final SwingNode swingNode) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       swingNode.setContent(new DrawingPanel(400, 400 , 20)); 
      } 
     }); 
    } 
} 

FXMLファイル:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.embed.swing.SwingNode?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.text.Font?> 

<GridPane gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60"> 
    <columnConstraints> 
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
    </columnConstraints> 
    <rowConstraints> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    </rowConstraints> 
    <children> 
     <Pane fx:id="pane" prefHeight="200.0" prefWidth="200.0"> 
     <children> 
      <SwingNode fx:id="swingNode" /> 
     </children> 
     </Pane> 
     <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="80.0" text="Click" GridPane.rowIndex="2"> 
     <font> 
      <Font size="18.0" /> 
     </font> 
     </Button> 
    </children> 
</GridPane> 
+0

FXMLファイルを投稿できますか? '@ FXML'で注釈を付けたフィールドを初期化するのは常に*間違いです。 –

+0

OK、投稿しました。多分、私はJavaFXの初心者です。時には私が何をしているのか分かりません:) –

答えて

1

スイングノードは既にFXMLでは<Pane>要素内に<children>要素内に<SwingNode>要素があるため、この枠の子です。したがって、コントローラーで2回目にペインの子ノードに追加しないでください。

さらに、FXMLLoader(すなわちそれら@FXMLに注釈を付けるの全体のポイントである)FXMLファイルに対応する作成された値にコントローラにpaneswingNodeフィールドを初期化しようとしています。 @FXMLという名前のフィールドを初期化するのは間違いです。

ですから、initialize方法で必要なのは

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    createAndSetSwingDrawingPanel(swingNode); 
} 

最後に、あなたはあなたのFXMLファイルから<fx:controller>属性が欠落しているように思われます。シーンビルダで、左下の「コントローラ」パネルを展開し、「コントローラクラス」フィールドにapplication.Controllerと入力します。

+0

Yeees :)今はうまくいきます。実際には、属性が設定されていました。なぜそれが消えたのか分かりません。どうもありがとうございます :) –

関連する問題