Creating a pop up JavaFX WebView - java

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?

Related

Is it possible to embed an external program in JavaFX?

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);
}
}

Image won't show up in JavaFX [duplicate]

This question already has an answer here:
How to load images from URL in JavaFx if recieving HTTP Error 403 [duplicate]
(1 answer)
Closed 5 years ago.
I've been trying for a while now, following various documentations but I just cannot get any images to show up on JavaFX.
Here is my code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
//stage and stuff
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//images
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class KingsGame extends Application {
public static ImageView iv = new ImageView();
Button btn = new Button();
public static Image test = new Image("http://puu.sh/vOk8i/754c8fee68.png");
#Override
public void start(Stage primaryStage) {
//stackpane
StackPane root = new StackPane();
root.getChildren().add(iv);
//scene
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("test program lol");
primaryStage.setScene(scene);
primaryStage.show();
//---actual game---
drawMainMenu();
}
public void helloTest() {
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
}
public static void drawMainMenu() {
iv.setImage(test);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Whenever I run the program, all I get is this: http://puu.sh/vOko6/b669cfc20b.png
The strange thing is, when I initially tested this, I used this (https://osu.ppy.sh/ss/8062698) image link as my test image and it somehow worked even though there wasn't even a .jpg or .png extension. It was a game screenshot and I simply just used the link the game gave me to test. When I switched it to another test link, it just broke.
How can I get ALL image links to work?
It looks like an access problem.
If you print the exception returned with this instruction :
System.err.println(test.getException());
you get this :
java.io.IOException: Server returned HTTP response code: 403 for URL: http://puu.sh/vOk8i/754c8fee68.png
The site probably authorizes only the browser clients

JavaFX/Swing Load WebPage Issue

I am creating an application, In the application I have three tabs, one which is a 'News' tab, which I simply want to load a webpage.
My code so far:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.scene.layout.BorderPane;
public class BrowserPane extends JFXPanel{
public BrowserPane() {
final BrowserPane _this = this;
Platform.runLater(new Runnable() {
#Override
public void run() {
BorderPane borderPane = new BorderPane();
WebView webComponent = new WebView();
System.out.println("Loading URL!");
webComponent.getEngine().load("http://www.google.com.au/");
borderPane.setCenter(webComponent);
Scene scene = new Scene(borderPane,450,450);
_this.setScene(scene);
}
});
}
}
Invoked like so:
contentArea.removeAll();
contentArea.revalidate();
contentArea.repaint();
newsPane = new BrowserPane();
newsPane.setBounds(0, 0, 1194, 593);
contentArea.add(newsPane);
contentArea.revalidate();
contentArea.repaint();
Now this works fine...the first time you click on the tab. But if you want to return to the tab later. It does not seem to render the webpage. I think this may be the way i am repainting the contentArea each time a tab is clicked.
NOTE: I am note using JTabPanel the tabs are simply JButtons that are styled as I like which invoke the various functions.

JavaFx 8 TextField in Popup won't focus

I'm making an applet using JavaFX 8 (via a JFXPanel) and was planning on using a Popup to display an area with a TextField in it. This works as expected, until the application loses focus. After that, I'm unable to make the TextField regain focus so you can't type in it any more. I've tried calling requestFocus() on the TextField but that didn't seem to do anything. The problem can be seen in the simple example below:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Popup;
import javax.swing.*;
public class FXApplet extends JApplet {
protected Scene scene;
protected Group root;
#Override
public final void init() {
SwingUtilities.invokeLater(() -> initSwing());
}
private void initSwing() {
JFXPanel fxPanel = new JFXPanel();
add(fxPanel);
Platform.runLater(() -> {
initFX(fxPanel);
initApplet();
});
}
private void initFX(JFXPanel fxPanel) {
root = new Group();
scene = new Scene(root);
fxPanel.setScene(scene);
}
public void initApplet() {
Popup popup = new Popup();
popup.setAutoHide(false);
popup.getContent().add(new TextField());
popup.show(scene.getWindow());
}
}

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