GUI not appearing with eclipse JavaFX - java

I'm currently stuck with JavaFX on eclipse.
My program runs, compiles, and the java application will open as if the program is being executed correctly, however the actual GUI and app won't appear.
The main difference between my problem and the problem of many other people is:
I have the stage.show(); method being called and
the program works as intended on textEdit (Implying that this problem is due to eclipse)
In case you need it, here is the program
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloFX extends Application
{
#Override
public void start(Stage stage)
{
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
Scene scene = new Scene(new StackPane(l), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch();
}
}
Any ideas? I am thoroughly lost.
P.S. I am extremely new to programming - Java is my only language as of right now and this is my first time on stack overflow, so expect a dumb mistake to have been made

Related

Show a variable's value in JavaFX

I'm currently working on a capstone project for a Java class and a problem I'm coming across frequently is displaying a variable's value in a JavaFX scene. I need a kickstart to get me moving, my google searches aren't bearing any fruit.
Thanks all :)
You can use a Label. Attach it to your scene and call Label.setText(String text) with the string representation of your variable value. Here's a complete example, using a Label:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class main3 extends Application {
static Integer variable = 250; // The value will be displayed in the window
#Override public void start(Stage primaryStage) {
Label variableLabel = new Label();
variableLabel.setFont(new Font(30));
variableLabel.setText("" + variable);
variableLabel.setLayoutX(175);
variableLabel.setLayoutY(125);
Group group = new Group(variableLabel);
Scene scene = new Scene(group, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
Result

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 app icon fails to show 9/10 times

I have an application which opens absolutely fine, but am having trouble setting an icon for it. The icon I give the path to is there, and changing to another imagine in that directory shows the icon 9/10 times, but this image never shows. There is always a question mark in it's place. So even on another file, which I know will work (ie. isn't corrupted), how come it only shows so rarely?
Below is the code of MyApplication.java
package MyApp;
import MyApp.Variables.Constants;
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;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/img/appicon.png")));
primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")");
primaryStage.setScene(new Scene(root, 1000, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Below is the project directory structure relating /img/ to Main.java:
I have tried all the solutions here but nothing fixed my issue.
Running on Ubuntu 16.04, intelliJ IDEA for the IDE, though the problem persists with an exported JAR file.
Loading Data from your disk is time consuming, so you be able to start loading the icon while the object is constructed. Place it in a constructor and save it in a instance member. Normally you need to add more than one icon, because each platform needs it own sizes (for links and so on).
package MyApp;
import MyApp.Variables.Constants;
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;
public class Main extends Application {
private Image icon;
public Main() {
icon = new Image(Main.class.getResource("/img/appicon.png").toExternalForm());
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));
primaryStage.getIcons().add(icon);
primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")");
primaryStage.setScene(new Scene(root, 1000, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My icon was this:
and the app structure in Netbeans looks like that:
and the running app look like that:

JavaFX ScrollBar setOnMousePressed not working

really liking JavaFX but have come across this problem and wondered if it was a bug.
The ScrollBar.setOnMousePressed() doesn't seem to fire when it has been initialised with a handler. The code below demonstrates the problem:-
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Play extends Application {
public static void main(String[] args) {
launch(args);
}
private static int cnt;
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Bug?");
Button btn = new Button("This text will get replaced by the event handlers");
ScrollBar scrollBar = new ScrollBar();
// When pressing and releasing the ScrollBar thumb, we only get decrements
// If you replace the ScrollBar with say a Button, then the code below works as you might expect.
scrollBar.setOnMousePressed( event -> btn.setText("X" + cnt++));
scrollBar.setOnMouseReleased( event -> btn.setText("X" + cnt--));
VBox root = new VBox();
root.getChildren().add(btn);
root.getChildren().add(scrollBar);
primaryStage.setScene(new Scene(root, 350, 250));
primaryStage.show();
}
}
Note, Im running on JDK 1.8.0_66 64 Bit on Microsoft Windows 10.
A simple workaround, as suggested by James_D, is to use EventFilters instead of setOnMousePressed(), as follows:-
So,
scrollBar.addEventFilter(MouseEvent.MOUSE_PRESSED,
event -> btn.setText("X" + cnt++));
instead of
scrollBar.setOnMousePressed( event -> btn.setText("X" + cnt++));
I believe .setOnMousePressed() should work, but doesn't because of a bug in the library. I've raised with oracle and will update the answer once oracle clarifies.

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

Categories