Cannot load an image from internet with JavaFX - java

I have a little problem, actually I'm trying to display this url
https://s.ankama.com/www/static.ankama.com/wakfu/portal/game/item/115/13620723.png
in an ImageView but it doesn't work and I don't understand why.
I tried with this url https://gluonhq.com/wp-content/uploads/2015/11/phones-v3-small#2x.png
and it works perfectly fine.
Here's the code :
FXML part :
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<ImageView fitWidth="50" fitHeight="50">
<Image url="https://s.ankama.com/www/static.ankama.com/wakfu/portal/game/item/115/13620723.png"/>
</ImageView>
</GridPane>
Java part :
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Note : I use JavaFX with JDK 8

Related

For loop in FXML file || How to send data from controller to fxml file?

So I am working on a javaFX application and I want to create multiple <ImageView> using for loop!
Is that possible to make for/foreach loop in a .fxml file ?
If yes , then how ?
Also i have another question! how to send data from the controller to sample.fxml file ?
for exemple i want to send a table form controller.java to sample.fxml file and use that table+for loop to make <ImageView> in the fxml file!
Note: I am using the fxml to display the image because I am using those images as buttons.
Here is the code of sample.fxml :
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<ImageView fitHeight="120" fitWidth="120" fx:id="panda" GridPane.columnIndex="0" GridPane.rowIndex="0" onMousePressed="#mousePressed">
<image>
<Image url="#pics/panda.png">
</Image>
</image>
</ImageView>
</GridPane>
Here is the code of Controller.java :
package sample;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.AudioClip;
public class Controller {
public void play_audio()
{
AudioClip sound = new AudioClip(this.getClass().getResource("voices/panda.mp3").toString());
sound.play();
}
public void mousePressed() {
play_audio();
}
}
Code of Main.java :
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Animal Sound");
Scene scene = new Scene(root, 790, 675);
scene.getStylesheets().add("sample/styles.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I don't think flow control is manageable in an fxml file. Also you don't really pass arguments to an fxml file.
I think you've separated this well, and a little more paring down should get it to work. I would recommend to specify the sounds and the images from the controller. So I would make the controller for each button.
package sample;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
public class AudioButtonController {
String sound;
#FXML
ImageView image;
public void setAudioLocation(String resourcePath){
sound = resourcePath;
}
public void setImageLocation(String img){
image.setImage( new Image( img ) );
}
public void mousePressed() {
System.out.println(sound);
}
}
Now your fxml for each button can be.
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<HBox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.AudioButtonController">
<ImageView
fitHeight="120" fitWidth="120" fx:id="image" onMousePressed="#mousePressed">
</ImageView>
</HBox>
Here is an example main class that starts up and loads 2 audio buttons, but it could be used to load N buttons.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application{
#Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane(5, 5);
primaryStage.setTitle("Animal Sound");
String[] imgs = { "https://conserveblog.files.wordpress.com/2016/05/flagship-panda-thumbnail.jpeg?w=188", "http://news.bbc.co.uk/media/images/38625000/jpg/_38625095_021223panda150.jpg" };
for(String img: imgs){
FXMLLoader loader = new FXMLLoader( getClass().getResource("audio_button.fxml") );
Parent audioButton = loader.load();
AudioButtonController abc = loader.getController();
abc.setAudioLocation("not supported");
abc.setImageLocation(img);
root.getChildren().add(audioButton);
}
Scene scene = new Scene(root, 790, 675);
primaryStage.setScene(scene);
primaryStage.show();
}
}
This is a bit cumbersome for this example. If your button had more layout controls to it, and your outer layout was more complicated, then this could save some time.

Why can't I see the changes in my project but I see them in the fxml file?

I have a problem with my project in JavaFX. I use SceneBuilder which saves all changes to fxml file, but when i run project I see only empty background, even the size of the working area does not change. I try to clean project and workspace but problem is the same. I have refresh project and all files and nothing, I choose "Refresh using native hooks or pooling" but i have all time this same problem. Does anyone have any other idea? I use java 13 and JavaFX 11
mainWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com /fxml/1" fx:controller="application.MainWindowController">
<children>
<Label fx:id="labelText" layoutX="286.0" layoutY="42.0" prefHeight="17.0" prefWidth="0.0" text="" />
<Button fx:id="button1" layoutX="270.0" layoutY="82.0" mnemonicParsing="false" text="OK" />
</children>
</Pane>
main.class
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass()
.getResource("mainWindow.fxml").toExternalForm());
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainWindowController class
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MainWindowController implements Initializable {
#FXML
Label labelText;
#FXML
Button button1;
#Override
public void initialize(URL location, ResourceBundle resources) {
labelText.setText("Start");
}
}
i think the problem is in your start method in main. Try this implementation instead.
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
primaryStage.setTitle("Test");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
This is what you expected?

Unable to add text to InlineCssTextArea in RichTextFX

I am new to use the RichTextFX for my JAVAFX project. But I find that I cannot add the text to the InlineCssTextArea. In the project I used the MVC design and generate the UI code from the Scene Builder. The InlineCssTextArea is succesfully created but I cannot figure out why I cannot add text content.The program successfully compiled without error.But the InlineCssTextArea is empty as default.
I have read the official demos of the RichTextFX on Github, but they build up the UI purely from code but not from Scene Builder.
The release "fat" version of the library required :https://github.com/FXMisc/RichTextFX
Thank you
Tree structure
> ├───library
> │ richtextfx-fat-0.10.2.jar
> │
> │
> └───src
> Main.fxml
> Main.java
> specialController.java
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.InlineCssTextArea?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="specialController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<InlineCssTextArea fx:id="specialArea" layoutX="14.0" layoutY="14.0" prefHeight="346.0" prefWidth="611.0" />
</children>
</AnchorPane>
</children>
</VBox>
Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
private static final String indexFXMLFileName="Main.fxml";
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent index= FXMLLoader.load(getClass().getResource(indexFXMLFileName));
primaryStage.setTitle("Writing Assitant Index");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(index));
primaryStage.show();
}
}
specialController.java
import javafx.fxml.FXML;
import org.fxmisc.richtext.InlineCssTextArea;
public class specialController{
#FXML
public InlineCssTextArea specialArea;
public specialController() {
String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
specialArea = new InlineCssTextArea(alphabet);
}
}
I finally found the solution. Change the specialController.java to this. Thanks to #kleopatra. Except the problem for re-instantiation mentioned by kleopatra, the major problem was that I didn't realize the difference between initialize and constructor, and therefore I tried to manipulate before the initialization of the controls, which caused the nullPointerException. There are already some answers related the difference between these two on StackOverflow.
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import org.fxmisc.richtext.InlineCssTextArea;
import java.net.URL;
import java.util.ResourceBundle;
public class specialController implements Initializable {
public InlineCssTextArea specialArea;
public specialController() {
}
#Override
public void initialize(URL location, ResourceBundle resources) {
String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
specialArea.deleteText(0,specialArea.getLength());
specialArea.appendText(alphabet);
specialArea.setPrefWidth(400);
specialArea.setPrefHeight(600);
}
}

Connect SceneBuilder to fxmlloader

i made this short drawing app in IntelliJ, and i've tried SceneBuilder for the first time. The "sample.fxml" i made in scene builder won't load into my Main-class, so i made the Canvas etc. myself directly in the Main-class itself.
What am i doing wrong with the FXML loader/file?
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
try {
// FXMLLoader load = FXMLLoader.load(getClass().getResource("sample.fxml"));
// load.load();
Group root = new Group();
Controller controller = new Controller();
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root,800,500));
primaryStage.show();
root.getChildren().add(controller.canvas);
/*METODER I PROGRAMMET */
controller.drawCanvas();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
Canvas canvas = new Canvas(800,500);
#FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Not much documentation exists on scenebuilder.
Thanks!
I took your fxml and controller and added them to a project I created in Netbeans and it worked.
This is your problem:
Change:
Canvas canvas = new Canvas(800,500);
to:
#FXML Canvas canvas;
in your Controller
Sample.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Sample extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));//You may need so make the s lowercase.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
#FXML Canvas canvas;
#FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Not really sure why, but i tried to change it a few times to this, and it actually worked: Thanks #Sedrick Jefferson
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
try {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}

How to change fxml lable text by id

I have Label on my fxml file:
<children>
<Label fx:id="lblTest" text="Label" />
</children>
How can i change the text from "Label" to "Hello" from the main/controller java file?
I just started to learn the basics of JavaFX and i am not sure if it possible
Problem
You want to set the text on a label that is part of FXML/Controller/MainApp
Solution
Normally you have three files:
FXML-Document
FXML-Controller
Class that extends Application and overrides the start method
A little Example:
LabelText.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LabelText extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="labeltext.FXMLDocumentController">
<children>
<Label fx:id="lblTest" layoutX="126.0" layoutY="92.0" minHeight="16" minWidth="69" />
</children>
</AnchorPane>
FXMLDocumentController.java
package labeltext;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class FXMLDocumentController {
#FXML
private Label lblTest;
#FXML
private void initialize() {
lblTest.setText("I'm a Label.");
}
}
And that's it.
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Label lblData = (Label) root.lookup("#lblTest");
if (lblData!=null) lblData.setText("bye");

Categories

Resources