JFXPanel drag and drop seems to have broken somewhere between 2.2.3 and 8.0.0. The included code demonstrates the issue. When dragging file(s) onto the Scene, it works as expected in a normal fx Scene, but no longer works when that Scene is inside a JFXPanel. The issue isn't present if you build with Java 7.
The code opens two windows, a JFrame with a JFXPanel, and a JavaFx Stage. Both are given identical Scenes.
Try dragging a file into each Scene. In Java 7 (2.2.3) both respond as expected by telling you what file you are dragging over them. In Java 8 the Dragboard doesn't get populated properly when dragged over the scene inside the JFXPanel. I haven't checked everything in the Dragboard, but I know that getFiles() and getContentTypes() are both empty. Let me know if you have any questions.
Thanks!!
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.DragEvent;
import javafx.stage.Stage;
import javax.swing.JFrame;
public class JFXPanelTestFileDrag extends Application
{
/**
* Creates a simple Scene with some dragOver handling
* @return
*/
private static Scene createScene()
{
final Label label = new Label("Drag a file here...");
Scene scene = new Scene(label);
scene.setOnDragOver(new EventHandler<DragEvent>()
{
@Override
public void handle(DragEvent event)
{
if (event.getDragboard().getFiles().isEmpty()) //JFXPanel isn't properly translating the Files
label.setText("NO FILES!!!");
else
label.setText(event.getDragboard().getFiles() + "");
}
});
return scene;
}
public static void main(String[] args)
{
launchSwing();
launch();
}
/**
* Creates a simple JFrame with a JFXPanel containing the scene
*/
private static void launchSwing()
{
JFrame frame = new JFrame("Swing Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable()
{
@Override
public void run()
{
fxPanel.setScene(createScene());
}
});
frame.setContentPane(fxPanel);
frame.setSize(200, 200);
frame.setVisible(true);
}
/**
* starts the fx application with a scene identical to that created by launchSwing
*/
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("FX Frame");
primaryStage.setScene(createScene());
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
}
The code opens two windows, a JFrame with a JFXPanel, and a JavaFx Stage. Both are given identical Scenes.
Try dragging a file into each Scene. In Java 7 (2.2.3) both respond as expected by telling you what file you are dragging over them. In Java 8 the Dragboard doesn't get populated properly when dragged over the scene inside the JFXPanel. I haven't checked everything in the Dragboard, but I know that getFiles() and getContentTypes() are both empty. Let me know if you have any questions.
Thanks!!
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.DragEvent;
import javafx.stage.Stage;
import javax.swing.JFrame;
public class JFXPanelTestFileDrag extends Application
{
/**
* Creates a simple Scene with some dragOver handling
* @return
*/
private static Scene createScene()
{
final Label label = new Label("Drag a file here...");
Scene scene = new Scene(label);
scene.setOnDragOver(new EventHandler<DragEvent>()
{
@Override
public void handle(DragEvent event)
{
if (event.getDragboard().getFiles().isEmpty()) //JFXPanel isn't properly translating the Files
label.setText("NO FILES!!!");
else
label.setText(event.getDragboard().getFiles() + "");
}
});
return scene;
}
public static void main(String[] args)
{
launchSwing();
launch();
}
/**
* Creates a simple JFrame with a JFXPanel containing the scene
*/
private static void launchSwing()
{
JFrame frame = new JFrame("Swing Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable()
{
@Override
public void run()
{
fxPanel.setScene(createScene());
}
});
frame.setContentPane(fxPanel);
frame.setSize(200, 200);
frame.setVisible(true);
}
/**
* starts the fx application with a scene identical to that created by launchSwing
*/
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("FX Frame");
primaryStage.setScene(createScene());
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
}
- duplicates
-
JDK-8124500 Dragging from Swing to JavaFX (JFXPanel) that was working in JavaFX 2.2 fails in JavaFX 8.0
- Resolved