JavaFX UI issue(css and colors are not appearing) in windows 10 virtual box - css

Can any one help me on the below issue..
I am working on JavaFX and i have developed an standalone application using javafx. when i run the application in windows 10 it is working and the same is not working on windows 10 virtual box.
i have developed this application using JDK8u60 and created an executable jar file.when i run the jar file i am getting below issue.
Issue : colors of javafx ui is not appearing properly/UI shaded on virtual box. i have commented css and checked but still facing the same issue.
can any one please let me know a proper solution or root cause for this issue and let me know if any input required.
public class My_UI extends Application {
public static My_Controller controller;
private Stage stage;
private BorderPane root;
#Override
public void start(final Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(My_UI.class.getResource("My_GUI.fxml"));
root = (BorderPane) loader.load();
controller = (My_Controller)loader.getController();
Platform.setImplicitExit(false);
stage.setMaximized(true);
stage.setScene(new Scene(root));
stage.initStyle(StageStyle.UNIFIED);
stage.show();
}
}

Related

JAVAFX: Get User input using dialog before starting main application window

I have a client application, and I want to get the server address, port, and some other info from the user in order to initialize the controller of the main stage.
Currently my code look like this
public class MemoryGameClient extends Application {
#Override
public void start(Stage mainStage) throws Exception {
FXMLLoader fxml = new FXMLLoader(getClass().getResource("MemoryGameClient.fxml"));
MemoryGameClientController controller = fxml.getController()
Parent root = loader.load();
controller.connect(SERVER_ADDRESS, PORT, GAME_BOARD_SIZE);
Scene scene = new Scene(root);
mainStage.setScene(scene);
mainStage.show()
}
public static void main(String[] args) {
launch(args);
}
}
It works fine using the hardcoded values, but I want to be able to open a DialogPane or something like that to get those values from user before initializing the scene and running the main application logic.
Can I set an empty Scene that launch a dialog and after that quitting and starting the main stage? Can I do that from the controller before mainStage.show()?
(I need the user input not only for connecting the server but also to determine the size of the GridPane in root)

Is it possible to launch a JavaFX application through another JavaFX application?

Can I know why there is an error when I say.
Stage s = new Stage();
new CaeserCipherFX().start(s);
This is my code below. I need to launch another JavaFX Application from this one. Please help. Thank you.
public class Main extends Application
{
String args[];
#Override
public void start(Stage stage) throws Exception
{
// creating types of encryptions (Button)
Button caeserCipher = new Button("1. Caeser Cipher");
Button runningKeyCipher = new Button("2. Running Key Cipher");
Button trithemiusCipher = new Button("3. Trithemius Cipher");
Button vignereCipher = new Button("4. Vignere Cipher");
//setting styles
caeserCipher.setTextFill(Color.BLUE);
runningKeyCipher.setTextFill(Color.BLUE);
trithemiusCipher.setTextFill(Color.BLUE);
vignereCipher.setTextFill(Color.BLUE);
/*need to add more!*/
//setting action listeners
String arr [] = {"CaeserCipher","RunningKeyCipher","TrithemiusCipher","VignereCipher"};
caeserCipher.setOnAction((ActionEvent event)->{
//open caeser cipher
Stage s = new Stage();
new CaeserCipherFX().start(s);
});
runningKeyCipher.setOnAction((ActionEvent event)->{
//open running key cipher
stage.hide();
});
trithemiusCipher.setOnAction((ActionEvent event)->{
//open trithemius cipher
stage.hide();
});
vignereCipher.setOnAction((ActionEvent event)->{
//open vignere cipher
stage.hide();
});
// creating flowpane(FlowPane)
FlowPane menu = new FlowPane();
menu.setHgap(25);
menu.setVgap(25);
menu.setMargin(caeserCipher, new Insets(20, 0, 20, 20));
//list for Flowpane(ObservableList)
ObservableList list = menu.getChildren();
//adding list to flowpane
list.addAll(caeserCipher,runningKeyCipher,trithemiusCipher,vignereCipher);
//scene for stage
Scene scene = new Scene(menu);
stage.setTitle("Main Menu");
stage.setScene(scene);
// stage.initStyle(StageStyle.UTILITY);
stage.setHeight(100);
stage.setWidth(600);
stage.setResizable(false);
// Show the Stage (window)
stage.show();
}
}
And I want to launch the code below:
public class CaeserCipherFX extends Application
{
#Override
public void start(Stage stage) throws Exception
{//some other code
//some other code
}
}
There is a ubiquitous JavaFX main application thread which takes a while to get used to.
Think of it like the front-end thread. Theoretically, you should use that thread to handle UI updates and complex cpu tasks such as looking up something in a BD or figuring out the 100000th decimal of PI should be done in a background thread. If you don't do this, the UI will become unresponsive until the DB data is returned, or that decimal is found.
public class TestClass extends Application {
public static void main(String[] args) {
System.out.println("here");
Application.launch(TestClass.class, args);
System.out.println("this is called once application launch is terminated.");
}
#Override
public void init() throws Exception {
super.init(); //To change body of generated methods, choose Tools | Templates.
System.out.println("message from init");
}
#Override
public void start(Stage primaryStage) throws Exception { // this is abstract.
System.out.println("message from start");
Platform.exit(); // if you remove this line, the application won't exit.
}
}
Since JavaFX comes with some prerequisites, you need to start you rapplication using a front-end. You can work around this, but technically,
public void start(Stage primaryStage)
is what , for all intensive purposes, starts your program.
From here, you can use the primaryStage to control most of your application. It's a good idea to put a .onCloseRequest() on it in which you call Platform.exit();
If you want to have multiple windows in your application, you could use something like
public class TestClass extends Application {
public static void main(String[] args) {
System.out.println("here");
Application.launch(TestClass.class, args);
System.out.println("this is called once application launch is terminated.");
}
#Override
public void init() throws Exception {
super.init(); //To change body of generated methods, choose Tools | Templates.
System.out.println("message from init");
}
#Override
public void start(Stage primaryStage) throws Exception { // this is abstract.
primaryStage.setScene(new Scene(new TextArea("this is the first stage (window)")));
primaryStage.setTitle("stage 1");
primaryStage.show();
primaryStage.setOnCloseRequest((event) -> {
Platform.exit();
});
Stage secondaryStage = new Stage();
secondaryStage.setTitle("stage 2");
TextArea ta2 = new TextArea("this is a different stage.");
Scene scene = new Scene(ta2);
secondaryStage.setScene(scene);
secondaryStage.show();
primaryStage.setX(200);
secondaryStage.setX(200 + primaryStage.getWidth() + 50);
}
}
This is what I assume you want to do. Basically create a new window whenever you press a button. You can create stages like this.
The reason for which you can't do it your way is because you are attempting to start another javafx thread by invoking new CaeserCipherFX which is an application object, not a Stage.
new CaeserCipherFX().start(s); // this can only be called once.
IF you absolutely want to have 2 distinct applications (note: not application windows), then you need to have 2 distinct processes.
Lastly, the primaryStage parameter used in either examples is in the beginning basically a placeholder (as in it's constructed, but there's nothing really in it... like a new String()). You can use different stage objects as your "primary" UI.
Lastly, if depending on the stuff you want to decrypt, you may need to use background threads if you want to keep the UI responsiveness. For this you will need to check out the concurrency part of the javafx tutorial.
Is it possible to launch a JavaFX application through another JavaFX application? Not really.
Alternatively, you can use java.lang.ProcessBuilder
This class essentially sends command lines to your operating system shell.
You can use it to run something like "java -jar XXX\YYY\CaeserCipherFX.jar" whenever you click a button. (you'll have to build a CaeserCypherFX project into a jar file)
This will create a new JVM. This means no memory state sharing. You can handle this through IPC.

In JavaFX using JxBrowser invokeAndWaitFinishLoadingMainFrame() method crashes JVM

I'm experimenting with the JXBrowser Chromium browser engine in JavaFX on Mac OS Sierra. I would like to wait until the URL is fully loaded after I call browser.goBack() or browser.goForward() methods so I can check the Navigation History. The simple app below crashes the JVM but the same code works fine in Java (Swing). The same call in a Java swing app works without any issues. Does anyone have any idea why?
public class JavaFXSample extends Application {
#Override
public void init() throws Exception {
// On Mac OS X Chromium engine must be initialized in non-UI thread.
if (Environment.isMac()) {
BrowserCore.initialize();
}
}
#Override
public void start(final Stage primaryStage) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
Scene scene = new Scene(new BorderPane(view), 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>
() {
#Override
public void invoke(Browser browser) {
browser.loadURL("http://www.google.com");
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Looks like you've faced a deadlock because you create the Browser instance in heavyweight mode. You can try solving this issue by using the "jxbrowser.ipc.external=true" VM parameter that enables lightweight rendering mode and runs Chromium engine in separate native process to avoid deadlocks in UI thread.

Play Youtube Video in JavaFX

Is there is any way to play Youtube videos on JavaFX Application? I was trying this-
public class YoutubeVideoPlayer extends Application
{
#Override
public void start(Stage stage) throws Exception
{
WebView webview = new WebView();
webview.getEngine().load("http://www.youtube.com/embed/_3op5hukpIE?autoplay=1");
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
I don't know how, but once it worked fine. But every time it is showing me an error message:
An error occured. Please try again later.
Can anyone explain how did it work and how can I make it work again?
This code is not working when I was running this on IDE(Eclipse, NetBeans or IntelliJ). But when I am Exporting this from Eclipse IDE as "Runnable JAR file", and running the Jar file, it's working perfectly. It seems, it's just not running on IDE.

JavaFX Application Menu

So I've looked around a fair bit, but I've not been able to find any information on how to make an application menu in JavaFX.
I've seen a project 'Jayatana' which seems to allow applications to have proper application menus in Ubuntu using Intellij at least (as an example).
I've also seen a few suggestions that using something like the following will work for OS X users:-
final List<MenuBase> menus = new ArrayList<>();
menus.add(GlobalMenuAdapter.adapt(menu));
Toolkit.getToolkit().getSystemMenu().setMenus(menus);
And there is also the NSMenuFX project, again for OS X.
And I've also seen the java-gnome project which I think only works for Swing.
But what I'd really like is some way of making application menus, preferably in a non-OS specific manner.
I'm happy to use a third party jar or whatever which does the heavy lifting but really, does anything like this exist?
At this point would my best bet be using Swing to create the shell of the JavaFX application and use methods which will integrate application menus with Swing instead? If that's the case, is there something that can do this automatically from JavaFX and handle the switching of the differing implementations?
edit
In the end, I simply used a combination of both Swing and JavaFX. I put the JavaFX app inside which allowed me to use the application menus which already work in Swing.
Not ideal, but it did work.
I think you are just looking for MenuBar.useSystemMenuBarProperty(). If you call this method on a menu bar, then if the platform supports system menus (e.g. OS X) the menu will not appear in the scene graph but will be used as the system menu.
SSCCE:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SystemMenuExample extends Application {
#Override
public void start(Stage primaryStage) {
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
MenuItem quit = new MenuItem("Quit");
quit.setOnAction(e -> Platform.exit());
menu.getItems().add(quit);
menuBar.getMenus().add(menu);
menuBar.setUseSystemMenuBar(true);
BorderPane root = new BorderPane();
root.setTop(menuBar);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The solution provided by James_D is the standard way of dealing with this in JavaFX but this solution does not work on the Mac, e.g., if you plan to internationalize your application. The Mac introduces some default menu items which you cannot properly deal with that way. That's where the NSMenuFX project comes into play.
NSMenuFX helps you to
Customize the auto-generated application menu of your JavaFX app
Automatically use the same menu bar for all stages
Create common OS X menus like the Window menu
If you can live with the deficiencies of the JavaFX solution use it, if not have a look at NSMenuFX or any of the other projects mentioned for Linux.
This is my code. Howe can i add this to my below code. After choosing start, then i want to run the game
private static final double SCREENWIDTH = 615.0;
private static final double SCREENHEIGHT = 635.0;enter code here
private GridPane root = new GridPane();
private Scene scene = new Scene(root);
private Stage primaryStage;
private GameController controller;
private boolean gewonnen;
private boolean pauze;
private int opstartSeconden;
private int aantalLevels;
private List<Level> levels;
public static void main(String args[]) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws NoPathPossibleException, StartAndEndAreThereSameException {
controller = new GameController(root, scene);// game controler wordt gemaakt
new Thread(this).start(); // we starten het spel in een nieuwe thread. we
this.primaryStage = primaryStage;
primaryStage.setTitle("HapMan9000");
primaryStage.setWidth(SCREENWIDTH);
primaryStage.setHeight(SCREENHEIGHT);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> closeProgram());
scene.setCursor(Cursor.NONE);
primaryStage.show();
levels = new ArrayList<>();
}
/**
* Zorgt ervoor dat het spel in zijn geheel wordt afgesloten.
*/
private void closeProgram() {
Platform.exit();
System.exit(0);
}
public void spelPauzeren() {
pauze = true;
}

Resources