Is it possible to embed an external program in JavaFX? - java

I'm currently developing a JavaFX desktop application that will take the place of an existing browser based application.
This new application have the option of launching external applications made in swing.
I'm now wondering if there's any chance of launching these external applications inside the existing JavaFX application?
In other words, is there a way to embed external applications in JavaFX?

Do you mean something like this?
JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button fxButton = new Button("javafx button");
fxButton.setOnAction(event -> new SwingTest());
Scene scene = new Scene(fxButton, 600, 400);
stage.setScene(scene);
stage.show();
}
}
Swing:
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Button;
public class SwingTest extends JFrame {
SwingTest() {
Button swingButton = new Button("swing button");
getContentPane().add(swingButton);
setSize(600, 600);
setVisible(true);
}
}

Related

Creating a pop up JavaFX WebView

I am having some difficulty understanding how to set up a pop up window that displays a web page in my Swing application. I have seen this code across multiple tutorials but what I am struggling with is how to include this in my own code.
Every tutorial uses the main method with this code, but I want to call this from one of my other classes and I am not sure how.
This is the code in question:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewMain extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("WebView test");
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
String url = "http://zoranpavlovic.blogspot.com/";
engine.load(url);
StackPane sp = new StackPane();
sp.getChildren().add(browser);
Scene root = new Scene(sp);
primaryStage.setScene(root);
primaryStage.show();
}
}
I want to be able to call the WebView from an ActionListener in one of my classes like this:
JButton mapExpandBtn = new JButton();
mapExpandBtn.setText("Expand");
mapExpandBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
new WebViewMain();
}
});
This doesn't work obviously. How can I adapt my own code or the WebViewMain code to get it to launch in my application?

Eclipse doesn't want to run simple JavaFX application

I have other classes named Test in other packages and one class with the same name in the default package.
When I click the Run button in Eclipse, instead of running this class, it runs another Test class from inside another package instead:
package jfx;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Test extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
}
How can I fix this?
Add a main method to allow Eclipse recognize the program as runnable application
public static void main(String[] args) {
Application.launch(args);
}

MiGLayout sizing not working properly in JavaFX

Here's my snippet:
package javafxdemo;
import org.tbee.javafx.scene.layout.MigPane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class FXDemo extends Application {
#Override
public void start (Stage stage) throws Exception {
MigPane root = new MigPane();
Scene scene = new Scene(root);
Button b = new Button("Hello");
root.getChildren().add(b);
stage.setScene(scene);
stage.setTitle("FX");
stage.show();
}
public static void main (String[] args) {
launch (args);
}
}
When running the gui doesn't show properly: the frame size is smaller than the button. Why does it happens? In HBox Layout when setting the scene it is automatically resized, so why with MiGLayout it doesn't work?
I'm using MigLayout 4.3
So, I filed an issue and later found out a workaround for this:
just add stage.sizeToScene() after stage.show().

JavaFX primaryStage remove windows borders? [duplicate]

This question already has an answer here:
JavaFX: Undecorated Window
(1 answer)
Closed 8 years ago.
I am making JavaFX destop application. I want to remove the default windows border and also I want to customize the 3 standard icons of minimize , maximize and close.
The original motivation of this kind of looks or customization is new Kaspersky 2012 User Interface.... I want to design something like that... :)
This example might be a good starting point. All window decoration is removed. A class extending HBox can be used to place custom buttons for standard window operations.
package javafxdemo;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class JavaDemo extends Application {
public static void main(String[] args) {
launch(args);
}
class WindowButtons extends HBox {
public WindowButtons() {
Button closeBtn = new Button("X");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
Platform.exit();
}
});
this.getChildren().add(closeBtn);
}
}
#Override
public void start(Stage primaryStage) {
//remove window decoration
primaryStage.initStyle(StageStyle.UNDECORATED);
BorderPane borderPane = new BorderPane();
borderPane.setStyle("-fx-background-color: green;");
ToolBar toolBar = new ToolBar();
int height = 25;
toolBar.setPrefHeight(height);
toolBar.setMinHeight(height);
toolBar.setMaxHeight(height);
toolBar.getItems().add(new WindowButtons());
borderPane.setTop(toolBar);
primaryStage.setScene(new Scene(borderPane, 300, 250));
primaryStage.show();
}
}
You can also download the JavaFX Samples where you can find many more useful examples.

java and javafx integrated programs

I have developed a desktop database application. But the interface is not rich. So I want integrate javafx interface in my code. But I am new to javafx, please give examples or tutorials for java and javafx integrated programs. I search in javafx site and some other books but I didn't get the java and javafx combined code.
Give me a code that replace Jbutton by javafx button.
import javax.swing.*;
class Javabutton extends JFrame
{
JButton b;
Container con;
Javabutton()
{
b=JButton("hello");
setLayout(new FlowLayout());
setSize(400,500);
con=getContentPane();
con.add(b);
setVisible(true);
}
public static void main(String args[])
{
new Javabutton();
}
}
`
You need to use JFXPanel, see http://docs.oracle.com/javafx/2.0/swing/jfxpub-swing.htm
It's better to write pure javaFX code other than using JFX with swing.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Hello extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Button");
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setLayoutX(100);
btn.setLayoutY(80);
btn.setText("OK");
btn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event)
{
System.out.println("Hello World");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Categories