2016-12-08 10 views
1

私は、スイングアプリケーションのサイズ変更可能なモーダルダイアログで表示したいシーンに、一連のJavaFXコンポーネントを作成しました。 JavaFXコンポーネントにはスキャンされたイメージのためにImageViewが含まれています。これはズームレベルに応じてかなり大きくなる可能性があります。私のオプションは、私の知る限りshowAndWaitとJavaFXのDialogを表示SwingアプリケーションのダイアログでJavaFXコンポーネントを提供する方法は?

  • Platform.runLaterであり、目に見えないJDialogでスイングEDTを停止します。それは明らかにデッドロックを引き起こし、かなり不愉快です。
  • JavaFXコンポーネントをJFXPanelに置き、JDialogに表示します。それはモダリティに関しては機能しますが、GroupLayoutではパネルが無限に成長するだけです(JavaFX ScrollPaneは効果がありません)ので、JFXPanelにコンポーネントをレイアウトする方法はわかりません。例えば

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.HeadlessException; 
import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javax.swing.GroupLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

public class NewMain extends JFrame { 
    private static final long serialVersionUID = 1L; 
    private final JFXPanel mainPanel = new JFXPanel(); 
    private final ImageView imageView; 
    private final JButton closeButton = new JButton("Close"); 

    public NewMain() throws HeadlessException { 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setBounds(0, 0, 800, 600); 
     setPreferredSize(new Dimension(800, 600)); 
     GroupLayout layout = new GroupLayout(this.getContentPane()); 
     this.getContentPane().setLayout(layout); 
     layout.setAutoCreateContainerGaps(true); 
     layout.setAutoCreateGaps(true); 
     this.imageView = new ImageView(NewMain.class.getResource("/File_CC-BY-SA_3_icon_88x31.png").toString()); 
     Platform.runLater(() -> { 
      BorderPane borderPane = new BorderPane(); 
      Button bottomButton = new Button("Some button"); 
      ScrollPane imageViewScrollPane = new ScrollPane(imageView); 
      borderPane.setCenter(imageViewScrollPane); 
      borderPane.setBottom(bottomButton); 
      imageView.setSmooth(true); 
      imageView.setFitHeight(400); 
      Group root = new Group(); 
      Scene scene = new Scene(root, Color.ALICEBLUE); 
      root.getChildren().add(borderPane); 
      mainPanel.setScene(scene); 
     }); 
     closeButton.addActionListener((event) -> { 
      setVisible(false); 
     }); 
     layout.setHorizontalGroup(layout.createParallelGroup() 
       .addComponent(mainPanel) 
       .addComponent(closeButton)); 
     layout.setVerticalGroup(layout.createSequentialGroup() 
       .addComponent(mainPanel) 
       .addComponent(closeButton)); 
     pack(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> { 
      new NewMain().setVisible(true); 
     }); 
    } 
} 

例である:スクロール/レイアウトが機能しないJDialogJFXPanelに一方

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class NewMain1 extends Application { 
    private final ImageView imageView; 

    public NewMain1() { 
     this.imageView = new ImageView(NewMain.class.getResource("/File_CC-BY-SA_3_icon_88x31.png").toString()); 
    } 

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

    @Override 
    public void start(Stage stage) throws Exception { 
     BorderPane borderPane = new BorderPane(); 
     Button bottomButton = new Button("Some button"); 
     ScrollPane imageViewScrollPane = new ScrollPane(imageView); 
     borderPane.setCenter(imageViewScrollPane); 
     borderPane.setBottom(bottomButton); 
     imageView.setSmooth(true); 
     imageView.setFitHeight(400); 
     StackPane root = new StackPane(); 
     root.getChildren().add(borderPane); 
     stage.setScene(new Scene(root, 800, 600)); 
     stage.show(); 
    } 
} 

ImageViewためうまく機能ScrollPaneを示しますhttps://github.com/krichter722/javafx-in-jdialog-in-swingから入手できます。

答えて

1

SceneのルートをGroupにしないでください。グループはサイズ変更できません。

シーンのルートにサイズ変更可能なレイアウトを使用するだけで(サンプルコードにサイズ変更可能なレイアウトがあります。これはBorderPaneなので、そのまま使用できます)。代わりの

Group root = new Group(); 
Scene scene = new Scene(root, Color.ALICEBLUE); 
root.getChildren().add(borderPane); 

書き込み:

Scene scene = new Scene(borderPane, Color.ALICEBLUE); 

のScrollPaneのSwingのJFrameの内部JFXPanel内部のJavaFXシーンの内部。

snapshot

それはあなたが純粋なJavaFXの間で観察された相違の理由ですので、あなたの純粋なJavaFXのNewMain1アプリケーションでご使用にはすでに、ルート(StackPane)として、サイズ変更可能なウィンドウを使用していることに注意してください、バージョンとSwing組み込みバージョンです。

関連する問題