Building Jar For Custom UI Element JavaFX - java

I followed the steps of the following blog but cannot create a jar using netbeans whenever I am going to build a dialog box is coming like this
The blog which I have followed:
Adding a Custom JavaFX Component to Scene Builder 2.0
The project contains following three files
i) FXML file
ii) Controller Class
iii) Style Sheet
Here is the tree view of the project:

I seems that you miss a main class where you load your fxml and set and show the scene.
In NetBeans use Run/Build Project (F11) to compile and create a jar file.
Use Run/Run Project (F6) to compile and run your project. For this you need a main class.
The tutorial in the link works for me. I did the following:
create project 'Stackoverflow' (the library)
create project 'Stackoverflow2' (the main application using the library). 'Stackoverflow2' ist using library 'Stackoverflow' as you can see below the 'Libraries' node.
Content of file StackOverflow2.java:
package stackoverflow2;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StackOverflow2 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("DemoScreen.fxml"));
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("stackoverflow");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Content of file DemoScreenController:
package stackoverflow2;
import javafx.fxml.FXML;
import stackoverflow.CommodityImageLabel;
public class DemoScreenController {
#FXML
protected CommodityImageLabel commodityLabel1;
}
Content of file DemoScreen.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import stackoverflow.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="321.0" prefWidth="543.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow2.DemoScreenController">
<children>
<Label layoutX="72.0" layoutY="34.0" text="Main App" />
<Separator layoutX="24.0" layoutY="78.0" prefHeight="1.0" prefWidth="499.0" />
<CommodityImageLabel layoutX="43.0" layoutY="98.0" />
</children>
</AnchorPane>
Content of StackOverflow.java:
package stackoverflow;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StackOverflow extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I didn't take care of the functionality - so the code is very basic.

Related

How can I alter GUI elements of a stages owner?

I'm currently trying to figure out how to implement a feature in which the user clicks a button and a popup appears, and then the user enters data into that popup, clicks a confirm button, and the data becomes visible in the popups owner. I was able to make data from the owner stage carry over to the popup stage after follow Bro Code's tutorial on making controllers communicate, however getting it to work the other way around is proving troublesome. Below is a project in which I've isolated the issue to try and figure it out.
App.java
package org.example;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Objects;
public class App extends Application {
public static Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("primary.fxml")));
Scene scene = new Scene(root);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
PrimaryController.java
package org.example;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class PrimaryController {
#FXML
Label label;
#SuppressWarnings("unused")
public void login(ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("secondary.fxml"));
Parent root = loader.load();
SecondaryController secondaryController = loader.getController();
secondaryController.stage = new Stage();
secondaryController.stage.initModality(Modality.APPLICATION_MODAL);
secondaryController.stage.initOwner(App.stage);
Scene scene = new Scene(root);
secondaryController.stage.setScene(scene);
secondaryController.stage.show();
}
public void displayMessage(String message){
label.setText(message);
}
}
SecondaryController.java
package org.example;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class SecondaryController {
#FXML
TextField textField;
public Stage stage;
#SuppressWarnings("unused")
public void writeToOwner(ActionEvent event) throws IOException {
String message = textField.getText();
FXMLLoader loader = new FXMLLoader(getClass().getResource("primary.fxml"));
Parent root = loader.load();
PrimaryController primaryController = loader.getController();
primaryController.displayMessage(message);
stage.close();
}
}
primary.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.PrimaryController">
<children>
<Button layoutX="270.0" layoutY="230.0" mnemonicParsing="false" onAction="#login" text="Login" />
<Label fx:id="label" layoutX="280.0" layoutY="191.0" />
</children>
</AnchorPane>
secondary.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.SecondaryController">
<children>
<TextField fx:id="textField" layoutX="219.0" layoutY="187.0" onAction="#writeToOwner" />
</children>
</AnchorPane>
The current behavior that I get from this code is almost what I want, except when the user would submit the text, it closes the popup but doesn't change the popups owner.
In your SecondaryController, it looks like you're creating a second instance of PrimaryController, leaving the first unchanged when invoking writeToOwner().
One approach is to arrange for the controllers to see a common model, much as they share a common stage in your example. The simplest such model is a single ObservableValue, illustrated here. To see the effect,
Add a StringProperty named text to your SecondaryController and make it accessible. In writeToOwner(), simply update the text and the bound label will follow.
public class SecondaryController {
#FXML
TextField textField;
public Stage stage;
private final StringProperty text = new SimpleStringProperty();
public StringProperty textProperty() {
return text;
}
#SuppressWarnings("unused")
public void writeToOwner(ActionEvent event) throws IOException {
text.set(textField.getText());
stage.close();
}
}
In your PrimaryController, bind the label's textProperty() to the textProperty() of the SecondaryController.
public class PrimaryController {
#FXML
Label label;
public StringProperty text = new SimpleStringProperty();
#SuppressWarnings("unused")
public void login(ActionEvent event) throws IOException {
…
secondaryController.stage.initOwner(App.stage);
label.textProperty().bind(secondaryController.textProperty());
…
}
}
In practice, you'll want to avoid public access to class members.

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.

FileChooser in Controller class - SceneBuilder JavaFX

I'm trying to code the functionality of a button created using SceneBuilder.
I found the "Code" section in Scene builder and set the name of the method that will launch when I click on the button (e.g. fileSelector). In the method I should use FileChooser variable(I need to take an image from the PC and save it to a "File" variable).
I copied the sample controller skeleton and paste it to my Controller class. Now I don't know how to implement the method, because I need a Stage variable to use with the FileChooser variable, but the stage variable is given for the public void start(Stage primaryStage) method.
My Main Class
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
public class MainExample extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
Pane firstPane = FXMLLoader.load(MainExample.class.getClassLoader().getResource("buttonExample.fxml"));
Scene firstScene = new Scene(firstPane);
primaryStage.setScene(firstScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My Controller Class
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class MyController {
#FXML
private Button selectFile;
#FXML
void fileSelector(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(stage);
}
}
The FXML from the button
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="135.0" prefWidth="280.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyController">
<children>
<Button fx:id="selectFile" layoutX="102.0" layoutY="55.0" mnemonicParsing="false" onAction="#fileSelector" text="SelectFile" />
</children>
</Pane>
You need a Window instance to show a FileChooser dialog, you can get it from the event like this:
#FXML
void fileSelector(ActionEvent event) {
Window window = ((Node) (event.getSource())).getScene().getWindow();
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(window);
event.consume();
}

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?

Cannot load an image from internet with JavaFX

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

Categories

Resources