How to embed JPanel into JavaFX pane? - java

How can I add a swingNode to a specific pane?
I'm actually trying to add a JPanel that loads an applet to the transparent area of the following and I'm not sure how to do it.

SwingNode is a javafx scene node and can be added to any javafx scene layouts.
To add a JPanel to a Pane and display it on JavaFX stage:
Add JPanel to a SwingNode
Assign the swingnode as a child to any of the layouts (which includes Pane).
Set the layout as the root of the scene
Set the scene to the stage and display it
A very simple code sample to show how you can add it to a Pane is (from SwingNode Javadoc):
public class SwingNodeExample extends Application {
#Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
Pane pane = new Pane();
pane.getChildren().add(swingNode); // Adding swing node
stage.setScene(new Scene(pane, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanel panel = new JPanel();
panel.add(new JButton("Click me!"));
swingNode.setContent(panel);
}
});
}
public static void main(String[] args) {
launch(args);
}
}

Related

How to invoke HSQLDB DatabaseManager Swing app in JavaFX Swingnode?

I am trying to embed the HSQLDatabaseManager Swing App in my own JavaFX project.
public class SQLTool extends Application {
#Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 800, 600));
stage.show();
stage.centerOnScreen();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanel panel = new JPanel();
DatabaseManagerSwing dbms = new DatabaseManagerSwing();
panel.add(dbms);
swingNode.setContent(panel);
}
});
}
Getting a Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: java/sql/SQLException
Looks like the jar file couldn't be found. It's placed under resources/lib/sqltool. I am using latest JDK14.
Thanks for the help!

JavaFX: java.lang.module.ResolutionException: Modules sqltool and hsqldb export package org.hsqldb.lib to module javafx.graphicsEmpty

I am learning JavaFX and I m trying to use external libraries (HSQLDB and the corresponding Datamanager GUI Tool) which I imported with Maven. My goal is to open an instance of the Datamanager GUI Tool which is a Swing application.
public class SQLTool extends Application {
#Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 640, 480));
stage.show();
stage.centerOnScreen();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//swingNode.setContent(new JButton("Click me!"));
**swingNode.setContent(new SwingForm());**
}
});
}}
class SwingForm extends JPanel {
public SwingForm(){
JPanel innerPanel = new JPanel();
**DatabaseManagerSwing dbms = new DatabaseManagerSwing();**
innerPanel.add(dbms);
add(innerPanel);
}}
Getting
java.lang.NoClassDefFoundError: java/sql/SQLException
at com...SQLTool$SwingForm.<init>(SQLTool.java:45)
at com...SQLTool$1.run(SQLTool.java:34)
Thanks for the help!

How to make the Button appear on the right side of a TitledPane JavaFX

I have managed to place a Button inside a TitledPane using setGraphic but I want it to be located on the right side; here is the code:
#Override
public void start(Stage primaryStage) {
Accordion acordion = new Accordion();
TitledPane tp1 = new TitledPane();
Button b1 = new Button();
b1.setText("X");
tp1.setGraphic(b1);
acordion.getPanes().addAll(tp1);
Scene scene = new Scene(acordion, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
The Button should be located on the right but I do not know how to do it
Set the alignment property of TitledPane to CENTER_RIGHT:
tp1.setAlignment(Pos.CENTER_RIGHT);

How to embed JPanel into JavaFX pane for graph visualization

How can I add a swingNode to a specific pane using my controller class?
I'm trying to add a JPanel that loads a network graph created using the JUNG software library I'm not sure how to do it.
You can achieve what you want to do using SwingNode class. Here is an example code where a JButton (swing component) is added to a StackPane (JavaFX component):
public class SwingFx extends Application {
#Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setScene(new Scene(pane, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
swingNode.setContent(new JButton("Click me!"));
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Source: http://download.java.net/jdk8/jfxdocs/javafx/embed/swing/SwingNode.html

Java applet using JavaFx

I'm developing an applet. I want to use JavaFx to create the controls. Currently, I'm using An JFXPanel. Here is the code:
private JFXPanel jfxPanel;
private Canvas canvas;
private Scene scene;
private BorderPane borderPane;
public void init() {
jfxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
jfxPanel.setScene(createScene());
add(jfxPanel);
}
});
}
public Scene createScene() {
borderPane = new BorderPane();
scene = new Scene(borderPane, 400, 800);
canvas = new Canvas();
canvas.getGraphicsContext2D().setFill(Color.RED);
borderPane.setCenter(canvas);
return scene;
}
public void paint(Graphics g) {
}
The problem is that it doesn't work. I can't see anything. Do you have any idea?
Maybe it's too late to answer, but...
Try to add this line below to your init()
getContentPane().add(jfxPanel);

Categories