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);
Related
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!
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!
public class Testing extends Application {
#Override
public void start(Stage stage)
{
Button button1 = new Button("First button");
Button button2 = new Button("Second button");
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2.setText("Working");
}
};
button1.addEventHandler(ActionEvent.ACTION, aHandler);
HBox hbox = new HBox(40,button1, button2);
Scene scene = new Scene(hbox, 840, 400);
stage.setScene(scene);
stage.setTitle("Testing");
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
You can see that this is a javafx Testing class where I am testing eventHandlers and it works fine but when I split the code and add it into on its own methods then the eventHandlers does not work like in the code below
public class Testing extends Application {
#Override
public void start(Stage stage)
{
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2().setText("Working");
}
};
button1().addEventHandler(ActionEvent.ACTION, aHandler);
stage.setScene(scene());
stage.setTitle("Testing");
stage.show();
}
public Button button1()
{
Button btn = new Button("First button");
return btn;
}
public Button button2()
{
Button btn = new Button("Second button");
return btn;
}
public HBox hbox()
{
HBox hbox = new HBox(40,button1(), button2());
return hbox;
}
public Scene scene()
{
Scene scene = new Scene(hbox(), 840, 400);
return scene;
}
public static void main(String[] args)
{
launch(args);
}
}
Now this code does not work. Please help.
Please note: If any one have another idea to encapsulate eventHandlers then please mention it if you can because my goal is to define eventHandlers in one class and registering it in another class.
Thank you.
Of course it's not working, you are creating an instance of Button every call to button1() and button2(). The instance of button1 and button2 in the HBox are different instances from the one you added the event handler.
I definitely recommend not splitting like what you are doing. This kind of splitting makes it hard to troubleshoot problems, and you are creating new instances whenever you call any of those methods. Stick to what you are doing originally.
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
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);
}
}