I'm really struggling to understand JavaFX controllers, my aim is to write to a TextArea to act as a log.
My code is below, but I want to be able to change values ETC from another class that I can call when needed. I have tried to create a controller class that extents Initializable but i cant get it to work. Could some one steer me in the correct direction?
I want to move the #FXML code at the bottom to another class and it update the Scene.
package application;
import javafx.event.ActionEvent;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,504,325);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public Thread thread = new Thread(new webimporter());
#FXML
public Label runningLabel;
#FXML
public TextArea txtArea;
#FXML
void runClick(ActionEvent event) throws IOException{
changeLabelValue("Importer running...");
thread.start();
}
#FXML
protected void stopClick(ActionEvent event){
changeLabelValue("Importer stopped...");
thread.interrupt();
}
#FXML
void changeLabelValue(String newText){
runningLabel.setText(newText);
}
void changeTextAreaValue(String newText1){
txtArea.setText(newText1);
}
}
Don't make the Application class a controller. It's a sin. There are other questions and answers which address this, but my search skills cannot find them at this time.
The reason it is a sin is:
You are only supposed to have one Application instance, and, by default, the loader will make a new instance, so you end up with two application objects.
Referencing the member objects is confusing, because the original launched application doesn't have the #FXML injected fields, but the loader created application instance does have #FXML inject fields.
Also, unrelated advice: Don't start trying to write multi-threaded code until you have the application at least working to the extent where it displays your UI.
A multi-threaded logger for JavaFX is in the answer to Most efficient way to log messages to JavaFX TextArea via threads with simple custom logging frameworks, though unfortunately it is not straight-forward in its implementation and comes with little documentation.
textlogger/Root.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="400.0" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textlogger.ImportController">
<children>
<HBox alignment="BASELINE_LEFT" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0">
<children>
<Button mnemonicParsing="false" onAction="#run" text="Run" />
<Button mnemonicParsing="false" onAction="#stop" text="Stop" />
<Label fx:id="runningLabel" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
<TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
textlogger.ImportController.java
package textlogger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.io.IOException;
public class ImportController {
#FXML
private Label runningLabel;
#FXML
private TextArea textArea;
private WebImporter importer;
#FXML
void run(ActionEvent event) throws IOException {
changeLabelValue("Importer running...");
if (importer == null) {
importer = new WebImporter(textArea);
Thread thread = new Thread(
importer
);
thread.setDaemon(true);
thread.start();
}
}
#FXML
void stop(ActionEvent event){
changeLabelValue("Importer stopped...");
if (importer != null) {
importer.cancel();
importer = null;
}
}
private void changeLabelValue(String newText){
runningLabel.setText(newText);
}
}
textlogger.WebImporter.java
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.TextArea;
import java.time.LocalTime;
public class WebImporter extends Task<Void> {
private final TextArea textArea;
public WebImporter(TextArea textArea) {
this.textArea = textArea;
}
#Override
protected Void call() throws Exception {
try {
while (!isCancelled()) {
Thread.sleep(500);
Platform.runLater(
() -> textArea.setText(
textArea.getText() + LocalTime.now() + "\n"
)
);
}
} catch (InterruptedException e) {
Thread.interrupted();
}
return null;
}
}
textlogger.TextLoggingSample.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TextLoggingSample extends Application {
#Override
public void start(Stage stage) {
try {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(
getClass().getResourceAsStream(
"Root.fxml"
)
);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Related
I'm really struggling to understand JavaFX controllers, my aim is to write to a TextArea to act as a log.
My code is below, but I want to be able to change values ETC from another class that I can call when needed. I have tried to create a controller class that extents Initializable but i cant get it to work. Could some one steer me in the correct direction?
I want to move the #FXML code at the bottom to another class and it update the Scene.
package application;
import javafx.event.ActionEvent;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,504,325);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public Thread thread = new Thread(new webimporter());
#FXML
public Label runningLabel;
#FXML
public TextArea txtArea;
#FXML
void runClick(ActionEvent event) throws IOException{
changeLabelValue("Importer running...");
thread.start();
}
#FXML
protected void stopClick(ActionEvent event){
changeLabelValue("Importer stopped...");
thread.interrupt();
}
#FXML
void changeLabelValue(String newText){
runningLabel.setText(newText);
}
void changeTextAreaValue(String newText1){
txtArea.setText(newText1);
}
}
Don't make the Application class a controller. It's a sin. There are other questions and answers which address this, but my search skills cannot find them at this time.
The reason it is a sin is:
You are only supposed to have one Application instance, and, by default, the loader will make a new instance, so you end up with two application objects.
Referencing the member objects is confusing, because the original launched application doesn't have the #FXML injected fields, but the loader created application instance does have #FXML inject fields.
Also, unrelated advice: Don't start trying to write multi-threaded code until you have the application at least working to the extent where it displays your UI.
A multi-threaded logger for JavaFX is in the answer to Most efficient way to log messages to JavaFX TextArea via threads with simple custom logging frameworks, though unfortunately it is not straight-forward in its implementation and comes with little documentation.
textlogger/Root.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="400.0" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textlogger.ImportController">
<children>
<HBox alignment="BASELINE_LEFT" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0">
<children>
<Button mnemonicParsing="false" onAction="#run" text="Run" />
<Button mnemonicParsing="false" onAction="#stop" text="Stop" />
<Label fx:id="runningLabel" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
<TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
textlogger.ImportController.java
package textlogger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.io.IOException;
public class ImportController {
#FXML
private Label runningLabel;
#FXML
private TextArea textArea;
private WebImporter importer;
#FXML
void run(ActionEvent event) throws IOException {
changeLabelValue("Importer running...");
if (importer == null) {
importer = new WebImporter(textArea);
Thread thread = new Thread(
importer
);
thread.setDaemon(true);
thread.start();
}
}
#FXML
void stop(ActionEvent event){
changeLabelValue("Importer stopped...");
if (importer != null) {
importer.cancel();
importer = null;
}
}
private void changeLabelValue(String newText){
runningLabel.setText(newText);
}
}
textlogger.WebImporter.java
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.TextArea;
import java.time.LocalTime;
public class WebImporter extends Task<Void> {
private final TextArea textArea;
public WebImporter(TextArea textArea) {
this.textArea = textArea;
}
#Override
protected Void call() throws Exception {
try {
while (!isCancelled()) {
Thread.sleep(500);
Platform.runLater(
() -> textArea.setText(
textArea.getText() + LocalTime.now() + "\n"
)
);
}
} catch (InterruptedException e) {
Thread.interrupted();
}
return null;
}
}
textlogger.TextLoggingSample.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TextLoggingSample extends Application {
#Override
public void start(Stage stage) {
try {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(
getClass().getResourceAsStream(
"Root.fxml"
)
);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I am just trying to build a simple Gui with SceneBuilder and JavaFx however I can't figure out why I can't populate my TableView, it just stays empty even after inserting simple Testobjects. Here is the main Class and the Goal class.
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ResourceBundle;
import Objects.Goal;
import UtilityClasses.GoalManager;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
public class Main extends Application implements Initializable {
private Stage primaryStage;
private AnchorPane mainLayout;
public static ObservableList<Goal> goalsData = FXCollections.observableArrayList();
#FXML
static TableView<Goal> goalTable = new TableView<Goal>();
#FXML
static TableColumn<Goal, String> goalsColumn = new TableColumn<>();
#FXML
static TableColumn<Goal, String> statusColumn = new TableColumn<>();
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("MainWindow");
try {
showMainView();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
try {
setDefaultSettings();
launch(args);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void showMainView() throws IOException {
SettingControlls mainController = new SettingControlls();
FXMLLoader loader = new FXMLLoader();
loader.setController(mainController);
loader.setLocation(Main.class.getResource("mainFXML.fxml"));
mainLayout = loader.load();
Scene scene = new Scene(mainLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
private static void setDefaultSettings() throws IOException {
File f = new File("Goals.txt");
if(!f.exists()) {
OutputStream os = new FileOutputStream("Goals.txt");
Writer w = new OutputStreamWriter(os);
w.close();
}
}
private static ObservableList<Goal> getObservableList() throws FileNotFoundException, IOException {
ObservableList<Goal> ol = FXCollections.observableArrayList(new Goal("testGoal"));
return ol;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
goalsData.add(new Goal("test"));
System.out.println(goalsData.get(0).getGoal());
goalsColumn.setCellValueFactory(new PropertyValueFactory<Goal, String>("goal"));
statusColumn.setCellValueFactory(new PropertyValueFactory<Goal, String>("status"));
goalTable.setItems(goalsData);
}
}
Here is the Goal class:
package Objects;
import javafx.beans.property.SimpleStringProperty;
public class Goal {
private SimpleStringProperty goal;
private SimpleStringProperty status;
public Goal(String goal) {
this.goal = new SimpleStringProperty(goal);
this.status = new SimpleStringProperty("ongoing");
}
public Goal(String goal, String status) {
this.goal = new SimpleStringProperty(goal);
this.status = new SimpleStringProperty(status);
}
public String getGoal() {
return this.goal.get();
}
public String getStatus() {
return this.status.get();
}
public void setGoal(String newGoal) {
this.goal = new SimpleStringProperty(newGoal);
}
public void setStatus(String newStatus) {
this.status = new SimpleStringProperty(newStatus);
}
}
Because the TableView and the columns get declared in my fxml file it seemed weird to me to generate them with new however if I don't do that I get an Nullpointerexception.
Edit: Added the fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ContextMenu?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="406.0" prefWidth="721.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<MenuBar layoutX="-7.0" layoutY="14.0" prefHeight="25.0" prefWidth="733.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" onAction="#GoalsClicked" style="-fx-font-size: 23;" text="Goals" />
<Menu disable="true" mnemonicParsing="false" style="-fx-font-size: 23;" text="Matchups" />
</menus>
</MenuBar>
<Text layoutX="14.0" layoutY="87.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Your current Goals:">
<font>
<Font size="24.0" />
</font>
</Text>
<TableView fx:id="goalTable" layoutY="103.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0">
<columns>
<TableColumn fx:id="goalsColumn" maxWidth="1.7976931348623157E308" minWidth="0.0" prefWidth="625.0" text="Goals" />
<TableColumn fx:id="statusColumn" maxWidth="1.7976931348623157E308" minWidth="0.0" prefWidth="72.0" text="Status" />
</columns>
<contextMenu>
<ContextMenu>
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
<MenuItem mnemonicParsing="false" text="Set status" />
</items>
</ContextMenu>
</contextMenu>
</TableView>
<Button layoutX="619.0" layoutY="315.0" mnemonicParsing="false" onAction="#addGoal" text="+" />
<Button layoutX="655.0" layoutY="315.0" mnemonicParsing="false" onAction="#removeGoal" text="-" />
</children>
</AnchorPane>
You have many problems there :
It is not recommended to use your Main class as a Controller class.
Your Nodes used in .fxml are never static since they belong to the instance not to the class so remove them.
Don't instantiate the Nodes defined in the .fxml file. There is nothing to do with new. The FXMLLoader does the work for you.
So rewrite the following part:
#FXML
static TableView<Goal> goalTable = new TableView<Goal>();
#FXML
static TableColumn<Goal, String> goalsColumn = new TableColumn<>();
#FXML
static TableColumn<Goal, String> statusColumn = new TableColumn<>();
I would also suggest splitting the Main class into a Main and a Controller class.
In Main you should just load the file, and in the Controller do the UI related stuff.
You can split the following way:
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
AnchorPane pane = loader.load();
primaryStage.setScene(new Scene(pane, 400, 400));
primaryStage.show();
}
}
Controller:
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
and the .fxml
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.dummy.Controller">
</AnchorPane>
Then you can complete these classes respecting those two rules I mentioned at 2. and 3.
I am trying to populate a TableView from a method call on the click of a button from a Modal Window. I hope this is possible but I am not having any luck. The TableView is already created in an FXML file. Whenever I call upon the method, I receive a null pointer exception. Any ideas, or suggestions? Sorry if my question format is bad, I don't ask many questions.
MainController.java
#FXML TableView<Part> partsTableView;
#FXML ObservableList<Part> parts;
#FXML public TableColumn<Part, Integer> partIDColumn;
#FXML public TableColumn<Part, String> partNameColumn;
#FXML public TableColumn<Part, Integer> partILColumn;
#FXML public TableColumn<Part, Double> partPriceColumn;
#FXML
public void getPartData(){
partIDColumn.setCellValueFactory(new PropertyValueFactory<>("id")); (Line 112)
partNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
partILColumn.setCellValueFactory(new PropertyValueFactory<>("instock"));
partPriceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
partsTableView.setItems(generateData());
}
private ObservableList<Part> generateData(){
parts = FXCollections.observableArrayList();
parts.add(new Part(0, "Part" , 1 , 25.00));
return parts;
}
main.fxml
<TableView fx:id="partsTableView" layoutX="13.0" layoutY="68.0" prefHeight="344.0" prefWidth="556.0">
<columns>
<TableColumn fx:id="partIDColumn" editable="false" prefWidth="138.0" resizable="false" text="Part ID" />
<TableColumn fx:id="partNameColumn" editable="false" prefWidth="139.0" resizable="false" text="Part Name" />
<TableColumn fx:id="partILColumn" editable="false" prefWidth="119.0" resizable="false" text="Inventory Level" />
<TableColumn fx:id="partPriceColumn" editable="false" prefWidth="159.0" resizable="false" text="Price / Cost Per Unit" />
</columns>
</TableView>
Error: Caused by: java.lang.NullPointerException
at ims.MainController.getPartData(MainController.java:112)
I am using IntelliJ and I build my stage / scene in Scene Builder. I apologize if this is really simple and I'm overlooking something, but, I am just having such a tough time figuring this out.
Thank you in advance for your time and assistance!
I have prepared a sample. You can follow its logic. Main point is to manage main dialog and modal dilog controllers. You can check DialogController dialogController = fxmlLoader.<DialogController>getController(); line in AppMainController.java. In this sample main controller (AppMainController) obtains dialog controller (DialogController) and sets its observablelist to dialog controller. Sample class and fxmls are below:
AppMain.java:
package populatetable;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class AppMain extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("AppMain.fxml"));
Scene scene = new Scene(root, 500,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
AppMain.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane minHeight="500.0" minWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="populatetable.AppMainController">
<children>
<TableView fx:id="tvData" layoutX="91.0" layoutY="159.0" prefHeight="300.0" prefWidth="300.0">
<columns>
<TableColumn fx:id="colId" prefWidth="75.0" text="ID" />
<TableColumn fx:id="colName" prefWidth="75.0" text="Name" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<Button layoutX="211.0" layoutY="85.0" mnemonicParsing="false" onAction="#onOpenDialog" text="Open Dialog" />
</children>
</AnchorPane>
AppMainController.java
package populatetable;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class AppMainController implements Initializable {
#FXML
private TableView<Data> tvData;
#FXML
private TableColumn colId;
#FXML
private TableColumn colName;
private ObservableList<Data> tvObservableList = FXCollections.observableArrayList();
// Open dialog button click event
#FXML
void onOpenDialog(ActionEvent event) throws IOException {
System.out.println("onOpenDialog clicked");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Dialog.fxml"));
Parent parent = fxmlLoader.load();
DialogController dialogController = fxmlLoader.<DialogController>getController();
dialogController.setAppMainObservableList(tvObservableList);
Scene scene = new Scene(parent, 300, 200);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
#Override
public void initialize(URL location, ResourceBundle resources) {
colId.setCellValueFactory(new PropertyValueFactory<>("id"));
colName.setCellValueFactory(new PropertyValueFactory<>("name"));
tvData.setItems(tvObservableList);
}
public ObservableList<Data> getTvObservableList() {
return tvObservableList;
}
}
DialogController.java
package populatetable;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class DialogController {
private ObservableList<Data> appMainObservableList;
//fill table button click event
#FXML
void fillTable(ActionEvent event) {
Data data = new Data(1, "Name1");
appMainObservableList.add(data);
}
public void setAppMainObservableList(ObservableList<Data> tvObservableList) {
this.appMainObservableList = tvObservableList;
}
}
Dialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane minHeight="200.0" minWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="populatetable.DialogController">
<children>
<Button layoutX="115.0" layoutY="89.0" mnemonicParsing="false" onAction="#fillTable" text="Fill Table" />
</children>
</AnchorPane>
Data.java
package populatetable;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Data {
private final SimpleIntegerProperty id;
private final SimpleStringProperty name;
public Data(int id, String name) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
}
public int getId() {
return id.get();
}
public void setId(int ID) {
id.set(ID);
}
public String getName() {
return name.get();
}
public void setName(String nme) {
name.set(nme);
}
#Override
public String toString() {
return "id: " + id.get() + " - " + "name: " + name.get();
}
}
Hope it is useful.
I am getting this error
the controller "classname" has no event slot "methodname"
if i add "/" before application in fx:controller="application.MainController">
this error goes but creates
class failed to load
/Users/myname/Documents/workspace/Javafx%20tesr/bin/application/Main.fxml:7
here's the xml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="450" prefWidth="500" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button fx:id="genratebutton" layoutX="160.0" layoutY="225.0" mnemonicParsing="false" onAction="#generaterandom" prefHeight="27.0" prefWidth="180.0" text="generate random" />
<Label fx:id="myMessage" layoutX="138.0" layoutY="104.0" prefHeight="89.0" prefWidth="225.0" />
</children>
</AnchorPane>
(sorry new to GUI only know basics of swing)
this is main class
package application;
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) {
try {
Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
this is another class whose method i want to invoke
package application;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class MainController {
#FXML
private Label myMessage;
void generaterandom (ActionEvent event){
Random rand=new Random();
int myrand=rand.nextInt(50)+1;
myMessage.setText(Integer.toString(myrand));
System.out.println(Integer.toString(myrand));
}
}
Since the controller method is not public, you need to annotate it #FXML:
public class MainController {
#FXML
private Label myMessage;
#FXML
void generaterandom (ActionEvent event){
Random rand=new Random();
int myrand=rand.nextInt(50)+1;
myMessage.setText(Integer.toString(myrand));
System.out.println(Integer.toString(myrand));
}
}
This question already has answers here:
Javafx - Can application class be the controller class
(2 answers)
Closed 7 years ago.
How can I update the text of the label currentPlayerFileLabel from the if statement, so it gets the path from the file (if it is there); otherwise gets the default String?
Code as of now:
package sample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import sample.controllers.Util;
import sample.model.Config;
import java.io.File;
import java.net.URL;
import java.security.spec.ECField;
import java.util.ResourceBundle;
public class Main extends Application implements Initializable {
private Stage primaryStage = new Stage();
private BorderPane rootLayout;
private AnchorPane anchorPanePlayer;
private BorderPane borderPaneGame;
private Config config = new Config();
private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\"");
#FXML
private Button playersButton;
#FXML
private Button gamesButton;
#FXML
private Button quitButton;
#FXML
private Label currentPlayerFileLabel = new Label();
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Main window");
initLayout();
initConfig();
}
//----------- MY QUESTION IS ABOUT THIS METHODE -------------------------------
public void initConfig() {
File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt");
if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called
config = Util.initConfigFile();
isPlayerFileThere.setValue(config.getPlayerFileLocation().toString());
currentPlayerFileLabel.setText(getIsPlayerFileThere());
} else {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
}
//----------- MY QUESTION IS ABOUT THIS METHODE -------------------------------
public void initLayout() {
try {
//Load root layout from fxml
FXMLLoader loader = new FXMLLoader(); //Makes a new FXMLLoader
loader.setLocation(Main.class.getResource("view/mainView.fxml")); //sets the location of the main fxml file
rootLayout = loader.load(); //Loads the anchorpane from the loader, (AnchorPane) is redundent.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.getStackTrace();
}
}
public void initPlayerLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/playerEdit.fxml")); // Gets the new layout.
anchorPanePlayer = loader.load(); //Loades the new layout
Scene playerScene = new Scene(anchorPanePlayer); //adds a new scene with the loaded layout
Stage prevStage = (Stage) playersButton.getScene().getWindow(); //Get the stage from where we come from.
prevStage.close(); //Closes the prev stage
primaryStage.setScene(playerScene); //Sets new stage with the new layout
primaryStage.show(); //Shows new stage
} catch (Exception e) {
e.printStackTrace();
}
}
public void initGameLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/editGame.fxml"));
borderPaneGame = loader.load();
Scene gameScene = new Scene(borderPaneGame);
Stage prevStage = (Stage) gamesButton.getScene().getWindow();
prevStage.close();
primaryStage.setScene(gameScene);
primaryStage.show();
} catch (Exception e) {
e.getStackTrace();
}
}
public void quitProgram() {
Stage stageToQuit = (Stage) quitButton.getScene().getWindow();
stageToQuit.close();
}
public Stage getPrimaryStage() {
return primaryStage;
}
public AnchorPane getBorderPanePlayer() {
return anchorPanePlayer;
}
public Config getConfig() {
return config;
}
public void addPlayerFile() {
config.setPlayerFileLocation(Util.addPlayerFile());
}
public String getIsPlayerFileThere() {
return isPlayerFileThere.get();
}
public StringProperty isPlayerFileThereProperty() {
return isPlayerFileThere;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
public static void main(String[] args) {
launch(args);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<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.Main">
<bottom>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="40.0" BorderPane.alignment="CENTER">
<children>
<Region HBox.hgrow="ALWAYS" />
<Button fx:id="playersButton" mnemonicParsing="false" onAction="#initPlayerLayout" text="Players" />
<Button fx:id="gamesButton" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#initGameLayout" text="Games" />
<Button fx:id="quitButton" layoutX="69.0" layoutY="10.0" mnemonicParsing="false" onAction="#quitProgram" text="Quit" />
<Region layoutX="10.0" layoutY="10.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</bottom>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="addPlayerFileMenu" mnemonicParsing="false" onAction="#addPlayerFile" text="Add new player file" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER_RIGHT">
<children>
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="Welcom to the main menu!" />
<Label fx:id="currentPlayerFileLabel" text="Label" />
</children>
</VBox>
</children>
</HBox>
</center>
</BorderPane>
UPDATE Have now worked out how to do it, with the help from the comments on this question. To others with the same problem, here is the code that I got to work:
Main class:
package sample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import sample.controllers.MainController;
import sample.controllers.Util;
import sample.model.Config;
import java.io.File;
import java.net.URL;
import java.security.spec.ECField;
import java.util.ResourceBundle;
public class Main extends Application {
MainController mainController = new MainController();
Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/mainView.fxml"));
loader.setController(mainController);
Parent root = loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
The controller for the layout of the main window and the part that is interesting:
...
private Stage primaryStage = new Stage();
private AnchorPane anchorPanePlayer;
private BorderPane borderPaneGame;
private Config config = new Config();
private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\"");
#FXML
private Button playersButton;
#FXML
private Button gamesButton;
#FXML
private Button quitButton;
#FXML
private Label currentPlayerFileLabel;
...
#Override
public void initialize(URL location, ResourceBundle resources) {
File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt");
if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called
config = Util.initConfigFile();
isPlayerFileThere.setValue(config.getPlayerFileLocation().toString());
currentPlayerFileLabel.setText(getIsPlayerFileThere());
} else {
currentPlayerFileLabel.setText(getIsPlayerFileThere());
}
}
...
The FXML line that has been updated:
<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" >
You are overriding the label, so you do no longer use the instance that is defined in your FXML:
#FXML
private Label currentPlayerFileLabel = new Label();
Remove the assignment to a new label.