JavaFX TextField EventHandler - java

Currently I have a TextField which is my address bar and a WebView which is my page. When I click ented the TextField doesn't seem to do anything. It's meant to run the loadPage method and set to page to load whatever the user entered into the address bar. Any help would be appreciated.
package javafx_webview;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
WebEngine myWebEngine;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Platinum v1");
TextField addressBar = new TextField();
addressBar.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
loadPage(event.toString());
}
});
WebView myBrowser = new WebView();
myWebEngine = myBrowser.getEngine();
StackPane root = new StackPane();
root.getChildren().add(myBrowser);
root.getChildren().add(addressBar);
primaryStage.setScene(new Scene(root, 640, 480));
primaryStage.show();
}
private void loadPage(String url) {
try {
myWebEngine.load(url);
} catch (Exception e) {
System.out.println("The URL you requested could not be found.");
}
}
}

Get the url to load from the text of the address bar, not the toString of the action event on the address bar.
final TextField addressBar = new TextField();
addressBar.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
myWebEngine.load(addressBar.getText());
}
});
Also the load is asynchronous, so your exception handler won't work. You need to monitor's the webengine loadworker's exception property to get exceptions from the engine. Also note that a url not found is not necessarily an exception which would be reported, instead a web server will usually return a page for a http 404 error.
Here is a working sample:
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class Main extends Application {
private WebEngine myWebEngine;
public void start(Stage stage) {
stage.setTitle("Platinum v1");
final TextField addressBar = new TextField();
addressBar.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
myWebEngine.load(addressBar.getText());
}
});
WebView myBrowser = new WebView();
myWebEngine = myBrowser.getEngine();
myWebEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
#Override public void changed(ObservableValue<? extends Throwable> observableValue, Throwable oldException, Throwable exception) {
System.out.println("WebView encountered an exception loading a page: " + exception);
}
});
VBox root = new VBox();
root.getChildren().setAll(
addressBar,
myBrowser
);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) { launch(args); }
}

Related

Restart Javafx Application from other class? Application launch must not be called on the JavaFX Application Thread

Main Class :
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.util.Objects;
public class Main extends Application {
private static Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("sample.fxml")));
primaryStage.setTitle("Chess");
primaryStage.getIcons().add(new Image("sample/pieces/chessIcon.jpg"));
primaryStage.setScene(new Scene(root, 700, 600));
primaryStage.setResizable(false);
this.primaryStage = primaryStage;
primaryStage.show();
}
public static void restart() {
Platform.runLater(() -> {
primaryStage.close();
try {
Application.launch(Main.class);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
The Button Class(CheckMatePopUp) from where the application will be relaunched
package sample;
public class CheckMatePopUp implements Initializable {
#FXML
private Button closeBtn;
#FXML
private Label player;
#FXML
private void closeBtn(ActionEvent event){
System.exit(0);
}
#FXML
private void RestartBtn(ActionEvent event){
// Stage stage = (Stage) closeBtn.getScene().getWindow();
// stage.close();
Main.restart();
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
Getting error : java.lang.IllegalStateException: Application launch must not be called on the JavaFX Application Thread
at javafx.graphics#19/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:173)
at javafx.graphics#19/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:154)
at javafx.graphics#19/javafx.application.Application.launch(Application.java:236)
at sample.Main.lambda$restart$0(Main.java:33)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)

LinkedList erased in Action Handelers methods in JavaFX

I am working on an App that simulates an online shop for my Data Structures course, I am currently using JavaFX in the Eclipse IDE and Scene Builder for the GUI design, however I am trying to use a Double LinkedList to store the Accounts in the Sign In UI window, but for some reason every time that I try to insert a new Account object (in the Action Event method) into my List it doesn't inserts it, at least if I try to print it´s toString gives me a Null pointer Exception, but if the insert and the println is in the start method works. Last year a did an MP3 player using JavaFX but in Netbeans and I didn't use Scene Builder and the List did work, some one has any idea why is this happening?
package com.javafx.quvbi.controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class interfazController extends Application {
#FXML
private Button pausa, CreateAccBttn, LoginBttn;
#FXML
private TextField NameTF, AccountTF, PasswordTF, CardTF;
//Data
ListaDobleEnlazada<Account> accList;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//NameTF = new TextField();
//AccountTF = new TextField();
//PasswordTF= new TextField();
//CardTF = new TextField();
accList = new ListaDobleEnlazada<Account>();
//accList.insertarInicio(new Account("Diego","Qubi","22f24rf2",123321));
Parent root = FXMLLoader.load(getClass().getResource("/view/OptionUI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(accList.toString());
}
public ListaDobleEnlazada<Account> getAccounts() {
return accList;
}
#FXML
public void controlMusica(ActionEvent event) {
System.out.println("musica");
System.out.println(getAccounts().getInicio().toString());
}
public void CreateAccClick(MouseEvent event) throws IOException {
if(!NameTF.getText().isEmpty() && !AccountTF.getText().isEmpty() && !PasswordTF.getText().isEmpty() && !CardTF.getText().isEmpty()) {
accList.insertarInicio(new Account(NameTF.getText(), AccountTF.getText(), PasswordTF.getText(), Integer.parseInt(CardTF.getText())));
}
}
#SuppressWarnings("restriction")
public void createAccount(ActionEvent event) throws IOException {
if(!NameTF.getText().isEmpty() && !AccountTF.getText().isEmpty() && !PasswordTF.getText().isEmpty() && !CardTF.getText().isEmpty()) {
Parent loginUI = FXMLLoader.load(getClass().getResource("/view/GUI2.fxml"));
Scene logInScene = new Scene(loginUI);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(logInScene);
window.show();
System.out.println(NameTF.getText());
System.out.println(AccountTF.getText());
System.out.println(PasswordTF.getText());
System.out.println(CardTF.getText());
//this.accList.insertarInicio(new Account(NameTF.getText(), AccountTF.getText(), PasswordTF.getText(), Integer.parseInt(CardTF.getText())));
}
}
public void loginAccount(ActionEvent event) throws IOException {
Parent loginUI = FXMLLoader.load(getClass().getResource("/view/GUI2.fxml"));
Scene logInScene = new Scene(loginUI);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(logInScene);
window.show();
}
}

JavaFX proper use of stages and stage controllers (bad start)

I have spent the last couple of days looking into this and trying my hand at some different solutions found but Im still having a hard time implementing them correctly.
I have a project that's bound to turn into something mid-sized so I want to make sure I am using MVC correctly from the start instead of just hacking it apart "making it work".
As of now the application will only open 3 scenes, 2 on start(no fxml needed) and another for settings(using a FXML). I do need to be careful of strong references to these as this application will be running on low resource devices.
Ultimately my questions are:
Am I using scenes and controllers correctly? and given the way I have initiated the settings scene Im not finding a way to edit it with a controller. What am I doing wrong?
Below is what I have so far and I must say I dont like the way I initialize the first 2 scenes from Main and of course trying to start the settings scene is rather haphazard. My hurdle now is trying to interact with the scenes through their controllers.
Main.java
package sample;
import javafx.application.Application;
import javafx.stage.Stage;
import sample.controllers.StageController;
<pre>
public class Main extends Application
{
#Override
public void start(Stage primaryStage) throws Exception
{
StageController newStage = new StageController();
newStage.start(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}
StageController.java
package sample.controllers;
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.util.List;
import static sample.controllers.HotkeyController.createHotkeys;
import static sample.controllers.WebViewController.createWebView;
/**
* Created by devin on 4/14/16.
*/
public class StageController extends Application
{
#Override
public void start(Stage primaryStage)throws Exception
{
Screen primaryScreen = Screen.getPrimary();
Screen secondaryScreen;
List<Screen> allScreens = Screen.getScreens();
if (allScreens.size() <= 1)
{
System.out.println("Only one screen");
secondaryScreen = primaryScreen;
}
else
{
if (allScreens.get(0).equals(primaryScreen))
{
secondaryScreen = allScreens.get(1);
}
else
{
secondaryScreen = allScreens.get(0);
}
}
configureStage("Primary", primaryStage, primaryScreen);
final Stage secondaryStage = new Stage();
configureStage("Secondary", secondaryStage, secondaryScreen);
}
private void configureStage(final String name, final Stage stage, final Screen screen)
{
Rectangle2D bounds = screen.getBounds();
System.out.println(name + ":" + bounds);
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
stage.initStyle(StageStyle.UNDECORATED);
showStage(name, stage, screen);
stage.show();
stage.setFullScreen(true);
}
private void showStage(final String name, final Stage stage, final Screen screen)
{
//Scene scene = new Scene(new Group());
StackPane root = new StackPane();
Scene scene = new Scene(root);
stage.setScene(scene);
createWebView(name, root);
scene.setRoot(root);
/*
* Use hotkeys to allow application actions
*/
createHotkeys(name, scene);
}
}
WebViewController
package sample.controllers;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.w3c.dom.Document;
/**
* Created by devin on 4/13/16.
*/
public class WebViewController
{
public static void createWebView(final String name, final StackPane root)
{
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
if (name == "Primary") {
webEngine.load("http://google.com");
}
else
{
webEngine.load("http://google.com");
}
webEngine.documentProperty().addListener(new ChangeListener<Document>()
{
#Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc)
{
String heightText = browser.getEngine().executeScript(
"window.getComputedStyle(document.body, null).getPropertyValue('height')"
).toString();
double height = Double.valueOf(heightText.replace("px", ""));
System.out.println("Height of browser on " + name + " " + height);
}
});
root.getChildren().addAll(browser);
/* This is a firebug call if we need to debug the webpage that is being loaded */
// webEngine.documentProperty().addListener(new ChangeListener<Document>() {
// #Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
// enableFirebug(webEngine);
// }
// });
}
private static void enableFirebug(final WebEngine engine) {
engine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
}
}
HotkeyController.java
package sample.controllers;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
/**
* Created by devin on 4/13/16.
*/
public class HotkeyController
{
public static void createHotkeys(final String name, final Scene scene)
{
final KeyCombination exitCMD = new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
{
#Override
public void handle(KeyEvent event)
{
if (exitCMD.match(event))
{
System.out.println("CTRL + E was pressed on " + name + " display\n" +
"Exiting the application");
Platform.exit();
}
}
});
final KeyCombination settingsCMD = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
{
#Override
public void handle(KeyEvent event)
{
if (settingsCMD.match(event))
{
System.out.println("CTRL + S was pressed on " + name + " display\n" +
"Opening Settings Scene");
/*
* This is where we need to launch a scene for settings
*/
try
{
Parent root = FXMLLoader.load(getClass().getResource("/sample/view/settingsscreen.fxml"));
Stage settingsStage = new Stage();
settingsStage.setTitle("FTX Signage Settings");
settingsStage.setScene(new Scene(root, 500 , 400));
settingsStage.show();
// SettingsController settingsController = new SettingsController();
// GettersSetters getSet = new GettersSetters();
// settingsController.getText();
// String hostname = getSet.getHostname();
// settingsController.setText(hostname);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
});
}
}

JavaFX changing Scenes on fullscreen mode

I have problem with JavaFX. I created two scenes and switch button.
When I click that button I'm changing scene. But earlier i set fullscreen on true and after I pressed the button, windows taskbar shows for a moment. Is there any way to change scenes without having this taskbar visible?
There is the code:
----Main class----
import java.io.IOException;
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
DesktopApplication.launch(DesktopApplication.class);
}
}
----DesktopApplication class----
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class DesktopApplication extends Application implements Runnable {
Scene firstScene;
Scene secondScene;
Scene scene;
public static Stage primaryStagePublic;
public DesktopApplication() {
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Title");
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.initStyle(StageStyle.UNDECORATED);
int width = (int) Screen.getPrimary().getBounds().getWidth();
int height = (int) Screen.getPrimary().getBounds().getHeight();
HBox mainLayout = new HBox();
mainLayout.getChildren().add(new Text("hello!"));
MyLayout myLayout = new MyLayout(this);
firstScene = new Scene(myLayout,width,height);
secondScene = new Scene(mainLayout, width, height);
scene = firstScene;
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
primaryStage.show();
primaryStagePublic = primaryStage;
}
#Override
public void run() {
Thread thread = new Thread() {
public void run() {
launch(DesktopApplication.class);
}
};
thread.start();
while (true) {
}
}
public void swapScenes(Stage primaryStage){
primaryStage.setScene(secondScene);
primaryStage.setFullScreen(true);
primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
}
}
----MyLayout class----
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
public class MyLayout extends HBox{
private DesktopApplication desktopApplication;
public MyLayout(DesktopApplication desktopApplication) {
this.desktopApplication = desktopApplication;
init();
}
private void init(){
this.setStyle("-fx-background-color: #f8ff7d;");
Label text = new Label("testing");
Button button = new Button("Button");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
desktopApplication.swapScenes(DesktopApplication.primaryStagePublic);
}
});
this.getChildren().addAll(text, button);
}
}
I had a similar issue and solved it like #James_D suggested: Do not replace the scene as a whole, but only the root element:
public void swapScenes(Parent newContent){
stage.getScene().setRoot(newContent);
}
This requires changing the rest of the initialisation code a bit:
public class DesktopApplication extends Application implements Runnable {
Parent myLayout;
Parent mainLayout;
Scene scene;
public static Stage stage; // if possible make this private and non static
public DesktopApplication() {
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Title");
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.initStyle(StageStyle.UNDECORATED);
int width = (int) Screen.getPrimary().getBounds().getWidth();
int height = (int) Screen.getPrimary().getBounds().getHeight();
mainLayout = new HBox();
mainLayout.getChildren().add(new Text("hello!"));
myLayout = new MyLayout(this);
scene = new Scene(myLayout,width,height);
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
primaryStage.show();
primaryStagePublic = primaryStage;
}
...
I personally solved it (After a few months of looking) by, instead of doing primaryStage.setFullScreen(true), which glitches or something, doing primaryStage.setMaximized(true) along with primaryStage.setWidth(var1) and primaryStage.setHeight(var2). My hypothesis on why setFullScreen deosn't work is a bug in full screen exclusive mode. Or, there just isn't enough permissions or something and it bugs out.

Access property outside class

please see this code
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
private String pageTitle;
public static void main(String args[]) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
this.setPageTitle("Hello World");
Button btn = ButtonBuilder.create()
.text("Test Button")
.onAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
// this handle should change the property if Test.pageTitle property
//this.setPageTitle("Change button");
System.out.println("Testing button action");
}
})
.build();
StackPane root = new StackPane();
root.getChildren().add(btn);
stage.setTitle(getPageTitle());
stage.setScene(new Scene(root, 400, 400));
stage.show();
}
public String getPageTitle() {
return pageTitle;
}
public void setPageTitle(String pageTitle) {
this.pageTitle = pageTitle;
}
}
I want every time is the button clicked, it will change the title of application.
How to make the event handler can access the property of Text class?
You should refer to current instance of Test class
Test.this.setPageTitle("Change button");

Categories