JavaFX TreeView is null why? - java

I want to load the TreeView defined in my FXML file into a TreeView variable in my java code.
But always when I try it the TreeView is NULL.
I looked everywhere, Google, so many questions and answers here but nothing worked out.
I need the TreeView to add dynamicly TreeItems to it.
Here my IconOverview.FXML file located in src/rn.IconTool.view:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import rn.IconTool.model.*?>
<?import javafx.scene.control.TreeView?>
<AnchorPane prefHeight="584.0" prefWidth="966.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="rn.IconTool.model.IconOverviewControllerClass">
<children>
<SplitPane dividerPositions="0.229253112033195" layoutX="2.0" layoutY="34.0" prefHeight="547.0" prefWidth="966.0">
<items>
<AnchorPane fx:id="splitPaneMenu" minHeight="0.0" minWidth="0.0" prefHeight="545.0" prefWidth="353.0">
<children>
<TreeView fx:id="treeview" layoutX="-5.0" onMouseClicked="#ShowContextMenu" prefHeight="545.0" prefWidth="218.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane prefHeight="533.0" prefWidth="675.0">
<children>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="545.0" prefWidth="702.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ImageView fitHeight="95.0" fitWidth="104.0" layoutX="22.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
<ToolBar layoutY="-1.0" prefHeight="30.0" prefWidth="968.0" />
</children>
</AnchorPane>
my RootLayout.fxml, located in .view too
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import rn.IconTool.model.*?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="rn.IconTool.model.NewCategoryClass">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" onAction="#NewtCategory" text="New Category" />
</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>
</BorderPane>
the CreateNewCategoryScreen.fxml in .view too
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import rn.IconTool.model.*?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="rn.IconTool.model.NewCategoryClass">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" onAction="#NewtCategory" text="New Category" />
</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>
</BorderPane>
And my ContextMenuPane.fxml although located in .view:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="254.0" prefWidth="166.0" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8">
<!-- TODO Add Nodes -->
</AnchorPane>
And here for the Controller Class where I try to get the TreeView src/rn.IconTool.model IconOverviewControllerClass:
package rn.IconTool.model;
import java.io.IOException;
import rn.IconTool.MainApp;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class IconOverviewControllerClass {
#FXML TreeView treeview;
#FXML private AnchorPane splitPaneMenu;
public IconOverviewControllerClass(){
}
#FXML
public void ShowContextMenu(MouseEvent r){
GetInTouchWithContextMenu git = new GetInTouchWithContextMenu();
// treeview = (TreeView<String>) getScene().lookup("#treeview");
if(r.getButton() == r.getButton().SECONDARY){
System.out.println("Rechtsklick");
ContextMenu contextM = git.getTheContextMenu();
System.out.println(contextM.toString());
// treeview.setContextMenu(contextM);
//
// contextM.show(treeview, r.getX(), r.getY());
// System.out.println(contextM.isShowing());
System.out.println("X: " + r.getX() + " Y: " + r.getY());
}
}
public TreeView<?> getTreeView() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/IconOverview.fxml"));
AnchorPane iconOverview = (AnchorPane) loader.load();
treeview = (TreeView<?>) iconOverview.lookup("#treeview");
System.out.println("TreeIconOver: " + treeview + " iconOverView: " + iconOverview);
return treeview;
}
}
And my class for creating a Contextmenu what isn´t working but no so worse. src/rn.IconTool.model
GetInTouchWithContextMenu:
package rn.IconTool.model;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.stage.WindowEvent;
public class GetInTouchWithContextMenu {
private ContextMenu contextM = null;
public GetInTouchWithContextMenu(){
getContextMenu();
}
private void getContextMenu(){
contextM = new ContextMenu();
contextM.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing");
}
});
contextM.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown");
}
});
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
contextM.getItems().addAll(item1);
}
public ContextMenu getTheContextMenu(){
return contextM;
}
}
And here is my screen to create new TreeItems for my TreeView src/rn.IconTool.model NewCategoryClass
package rn.IconTool.model;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import rn.IconTool.MainApp;
public class NewCategoryClass {
private AnchorPane categoryPane;
private Stage stage;
private Scene scene;
private Node rootIcon;
#FXML private javafx.scene.control.Button closeButton;
#FXML private javafx.scene.control.TextField categoryName;
#FXML private javafx.scene.control.TextField categoryIconName;
#FXML private javafx.scene.control.TreeView<String> treeview;
public NewCategoryClass(){
//System.out.println("Neue Kategorie erstellen.");
}
#FXML
private void NewtCategory() throws IOException{
System.out.println("Neue Kategorie angelegt.");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/CreateNewCategoryScreen.fxml"));
categoryPane = (AnchorPane) loader.load();
scene = new Scene(categoryPane);
closeButton = (Button) scene.lookup("#closeButton");
stage = new Stage();
stage.setTitle("Set New Category");
stage.setScene(scene);
stage.show();
}
#FXML
private void FinishButtonlistener() throws IOException{
// get a handle to the stage
Stage stage = (Stage) closeButton.getScene().getWindow();
// do what you have to do
System.out.println("Textfeld: " + categoryName.getText());
// leeres Textfeld = leerer String
TreeItem<String> rootItem;
if(categoryName.getText().equals("")){
}else{
if(categoryIconName.getText().equals("")){
rootItem = new TreeItem<String> (categoryName.getText());
}else{
setRootIcon(categoryIconName.getText());
rootItem = new TreeItem<String> (categoryName.getText(), getRootIcon());
}
IconOverviewControllerClass iocc = new IconOverviewControllerClass();
iocc.getTreeView();
// treeview.setRoot(rootItem);
//System.out.println("AnchorPane: " + splitPaneMenu);
System.out.println("Treeview: " + treeview);
System.out.println("TreeItem: " + rootItem);
stage.close();
}
}
private void setRootIcon(String iconName){
rootIcon = new ImageView( new Image(this.getClass().getResourceAsStream(iconName)));
}
public Node getRootIcon(){
return rootIcon;
}
public Stage getStage(){
return stage;
}
public Scene getScene(){
return scene;
}
}
My main located in src/rn.IconTool
package rn.IconTool;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private AnchorPane iconOverview;
private Scene scene;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Icon Place");
initRootLayout();
showIconOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showIconOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/IconOverview.fxml"));
iconOverview = (AnchorPane) loader.load();
rootLayout.setCenter(iconOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
public Scene getScene(){
return scene;
}
}
I hope someone of you can help me
If you need the other classes to build the complete program then ask but I hope you have some ideas that the TreeView is not null ;)

You don't have to lookup the treeview with :
treeview = (TreeView) iconOverview.lookup("#treeview");
You automatically retrieve the treeView with the fx:id defined in the fxml file and the annotation #FXML in the controller.

I know why my TreeView was NULL. I forgot to implements javafx.fxml.Initializable and add the unimplemented method.
Here is my new IconOverviewControllerClass and dont worry about the static TreeView is only for test purpose ;)
package rn.IconTool.model;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class IconOverviewControllerClass implements javafx.fxml.Initializable{
#FXML public TreeView<String> treeview;
#FXML private AnchorPane splitPaneMenu;
private static TreeView<String> statictreeView;
public IconOverviewControllerClass(){
}
#FXML
public void initMyComponents(MouseEvent r) throws IOException{
if(getTreeView() == null){
System.out.println(treeview);
statictreeView = treeview;
System.out.println("static init: " + statictreeView);
}
ShowContextMenu(r);
}
public void ShowContextMenu(MouseEvent r){
if(r.getButton() == r.getButton().SECONDARY){
System.out.println("Rechtsklick");
GetInTouchWithContextMenu git = new GetInTouchWithContextMenu();
ContextMenu contextM = git.getTheContextMenu();
System.out.println(contextM.toString());
treeview.setContextMenu(contextM);
contextM.show(treeview, r.getX(), r.getY());
System.out.println(contextM.isShowing());
System.out.println("X: " + r.getX() + " Y: " + r.getY());
}
}
public TreeView<String> getTreeView() throws IOException{
return statictreeView;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
//System.out.println(treeview);
statictreeView = treeview;
}
}

Related

Adding dynamic buttons on GridPane FXML JavaFX

I've started a project requested by our instructor building an application in JavaFX.
I used SceneBuilder for FXML Part when I want to add buttons on gridpane, but it didn't work. The connection with the database is okay. On the first part of the application, the user logs in. After that he/she comes on UserWindow, but the buttons aren't displayed.
File Main.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 Main extends Application {
Stage window;
Controller c;
ControllerB cB;
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
FXMLLoader f = new FXMLLoader();
FXMLLoader f2 = new FXMLLoader();
f.setLocation(Main.class.getResource("sample.fxml"));
f2.setLocation(Main.class.getResource("UserWindow.fxml"));
Parent root = f.load();
Parent root2 = f2.load();
Scene s = new Scene(root, 500, 340);
// Controllers
c = f.getController();
c.setMain(this);
c.setScene2(new Scene(root2, 900, 500));
cB = f2.getController();
cB.setMain(this);
cB.setScene(s);
window.setTitle("Remote Works");
window.setScene(s);
window.show();
}
public static void main(String[] args) {
launch(args);
}
public void setUserData(Object o) {
window.setUserData(o);
}
public void setScene(Scene scene) {
window.setScene(scene);
window.show();
}
public void setScene2(Scene scene) {
window.setScene(scene);
window.show();
}
}
File ControllerB.java:
package sample;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.*;
public class ControllerB implements Initializable {
#FXML
private GridPane Box;
#FXML
private Label nameUser;
#FXML
private Label matriculeUser;
#FXML
private Label fonctionUser;
#FXML
private Button deconnect;
private Scene scene1;
private Main main;
ObservableList<String> ecoles;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
try {
schoolretrieve();
int size = ecoles.size();
Node[] n = new Node[size];
for (int i = 0; i < n.length; i++) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Bloc.fxml"));
ControllerItem controller = new ControllerItem();
loader.setController(controller);
n[i] = loader.load();
controller.setButtonInst(ecoles.get(i)); // Until this part all works
Box.getChildren().add(n[i]); // But it doesn't add buttons to GridPane
}
} catch (IOException | NullPointerException | SQLException e) {
//System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
File ControllerItem.java:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class ControllerItem {
#FXML
private Button buttonInst;
public Button getButtonInst() {
return buttonInst;
}
public void setButtonInst(String s) {
buttonInst.setText(s);
}
}
File Bloc.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<Button fx:id="buttonInst" mnemonicParsing="false" prefHeight="132.0" prefWidth="201.0" stylesheets="#../styles.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" />
File UserWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="497.0" prefWidth="710.0" styleClass="pane" stylesheets="#../styles.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerB">
<left>
<Pane prefHeight="497.0" prefWidth="156.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="150.0" fitWidth="151.0" layoutX="11.0" pickOnBounds="true" preserveRatio="true" />
<Circle fill="#e1ebf5" layoutX="78.0" layoutY="88.0" opacity="0.18" radius="74.0" stroke="BLACK" strokeType="INSIDE" />
<Label fx:id="nameUser" layoutX="10.0" layoutY="196.0" prefHeight="50.0" prefWidth="136.0" text="Nom Utilisateur :" />
<Label fx:id="fonctionUser" layoutX="11.0" layoutY="259.0" prefHeight="37.0" prefWidth="136.0" text="Statut : " />
<Label fx:id="matriculeUser" layoutX="10.0" layoutY="306.0" prefHeight="37.0" prefWidth="136.0" text="Identifiant : " />
<Button fx:id="deconnect" layoutX="20.0" layoutY="364.0" mnemonicParsing="false" onAction="#onAction" prefHeight="27.0" prefWidth="117.0" text="deconnexion" />
</children>
</Pane>
</left>
<center>
<Pane opacity="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #f7cc3d;"
BorderPane.alignment="CENTER">
<GridPane fx:id="Box" alignment="TOP_CENTER" hgap="3.0" layoutX="70.0" layoutY="96.0"
nodeOrientation="LEFT_TO_RIGHT" opacity="0.95" prefHeight="305.0" prefWidth="415.0"
styleClass="gridpane" stylesheets="#../ss.css" vgap="3.0"/>
</Pane>
</center>
</BorderPane>
So you have file UserWindow.fxml and you have declared a GridPane in it. I suggest you to don't make a separate .fxml file for the button.
Just do it like this in your ControllerB:
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
schoolretrieve();
int size = ecoles.size();
//Node[] n = new Node[size];
for (int i = 0; i < size; i++) {
//FXMLLoader loader = new FXMLLoader(getClass().getResource("Bloc.fxml"));
//ControllerItem controller = new ControllerItem();
//loader.setController(controller);
//n[i] = loader.load();
//controller.setButtonInst(ecoles.get(i));
//Box.getChildren().add(n[i]);
Button btn = new Button(ecoles.get(i));
Box.add(btn, 0, i); // Button is added to next row every time.
}
}

I need to access and clear a Pane from another controller class (javafx and SceneBuilder)

I know that there are a lot of questions already answered for this, but I just cant get my head around it. It is a possible duplicate of:
accessing a Pane from another class in javafx
JavaFX change Pane color from a different class
In my app I want to clear the changablePane (StackPane) in the MainWindowController from a mouseEvent in the NotesScreenController so that only the marked as done notes will be displayed.
MainWindowController.java
package gui;
import gui.mainWindow.issues.NotesScreenController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Stack;
public class MainWindowController implements Initializable {
private Stage stage = null;
private StackPane paneNotes = null;
private NotesScreenController secondPane;
#FXML
private AnchorPane mainContainer;
#FXML
private VBox vBoxContainer;
#FXML
private MenuBar menuBarTop;
#FXML
private HBox hBoxContainer;
#FXML
private StackPane navigationSection;
#FXML
private TreeView<Button> treeView;
#FXML
private StackPane changablePane;
public static Button issuesNotes;
//TAB SO FILTRI
#Override
public void initialize(URL location, ResourceBundle resources) {
createTreeView();
}
private void createTreeView() {
Button treeViewHeader = new Button("Zdravo");
TreeItem<Button> treeViewHeaderItem = new TreeItem<>(treeViewHeader);
treeViewHeader.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
treeViewHeaderItem.setExpanded(true);
//ROOT FOR ISSUES TRACKING
Button rootForIssues = new Button("Issues Tracker");
Button issuesTable = new Button("Issues Table");
issuesNotes = new Button("Request Notes");
TreeItem<Button> rootForIssuesItem = new TreeItem<>(rootForIssues);
TreeItem<Button> issuesTableItem = new TreeItem<>(issuesTable);
TreeItem<Button> issuesNotesItem = new TreeItem<>(issuesNotes);
rootForIssues.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
issuesTable.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
issuesNotes.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
rootForIssuesItem.setExpanded(true);
rootForIssuesItem.getChildren().addAll(issuesTableItem, issuesNotesItem);
//ROOT ZA NEKOE DRUGO - PROBNO
Button buttonA = new Button("Proba");
Button buttonB = new Button("Proba");
Button buttonC = new Button("Proba");
TreeItem<Button> nodeA = new TreeItem<>(buttonA);
TreeItem<Button> nodeB = new TreeItem<>(buttonB);
TreeItem<Button> nodeC = new TreeItem<>(buttonC);
buttonA.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
buttonB.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
buttonC.getStylesheets().add("styles/Notes/TreeViewStyles/TreeButton.css");
nodeA.setExpanded(true);
nodeA.getChildren().addAll(nodeB, nodeC);
//ADDING ALL ROOTs OF THE TREEVIEW
treeViewHeaderItem.getChildren().addAll(rootForIssuesItem, nodeA);
issuesNotes.setOnAction(event ->{
clearPane();
URL paneOneUrl = getClass().getClassLoader().getResource("gui/mainWindow/issues/NotesScreen.fxml");
FXMLLoader loader = new FXMLLoader();
NotesScreenController nsc = new NotesScreenController();
loader.setController(nsc);
try {
paneNotes = loader.load(paneOneUrl);
changablePane.getChildren().add(paneNotes);
} catch (IOException e) {
e.printStackTrace();
}
});
issuesTable.setOnAction(event -> {
clearPane();
});
//TREE VIEW
treeView.setId("tree-view-issues");
treeView.getStylesheets().addAll("styles/Notes/TreeViewStyles/TreeView.css");
treeView.setRoot(treeViewHeaderItem);
}
public void clearPane() {
changablePane.getChildren().clear();
}
public void getMainScreenController() {
}
public static Button getIssuesNotes() {
return issuesNotes;
}
public void setStage(Stage stage) {
this.stage = stage;
stage.setResizable(true);
stage.setTitle("SoloStats - Welcome");
}
public void closeStage() {
if (this.stage != null) {
this.stage.close();
}
}
}
MainWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="mainContainer" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MainWindowController">
<children>
<VBox fx:id="vBoxContainer" layoutX="530.0" layoutY="230.0" prefHeight="25.0" prefWidth="1200.0" AnchorPane.bottomAnchor="572.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar fx:id="menuBarTop" prefHeight="25.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</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>
</children>
</VBox>
<HBox fx:id="hBoxContainer" layoutX="384.0" layoutY="238.0" prefHeight="600.0" prefWidth="1200.0" style="-fx-background-color: rgb(247, 247, 247);" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
<children>
<StackPane fx:id="navigationSection" prefHeight="150.0" prefWidth="300.0" style="-fx-background-color: #222;">
<children>
<TreeView fx:id="treeView" fixedCellSize="24.0" prefHeight="200.0" prefWidth="200.0">
<StackPane.margin>
<Insets left="-25.0" />
</StackPane.margin>
</TreeView>
</children>
</StackPane>
<StackPane fx:id="changablePane" prefHeight="575.0" prefWidth="950.0" stylesheets="#../styles/MainWindow/StackPaneChangable.css" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</AnchorPane>
NotesScreenController.java
package gui.mainWindow.issues;
import gui.MainWindowController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class NotesScreenController extends AnchorPane implements Initializable {
private NotesDirectory notesDirectory = new NotesDirectory();
private MainWindowController mainController;
#FXML
private StackPane mainContainer;
#FXML
private VBox vBoxContainer;
#FXML
private HBox hBoxFilterContainer;
#FXML
private ScrollPane scrollPane;
#FXML
private TilePane tilePaneNotesScreen;
#FXML
private ComboBox<String> generalSortBox;
#FXML
private ComboBox<String> sortByNameBox;
#Override
public void initialize(URL location, ResourceBundle resources) {
loadScreen();
}
public void loadScreen() {
try {
notesDirectory.insertNotesToTilePane(tilePaneNotesScreen, notesDirectory.deserializedNotesList(notesDirectory.getFileName(new File("src/notesDirectory")), "src/notesDirectory/"));
} catch (IOException e) {
e.printStackTrace();
}
generalSortBox.setItems(FXCollections.observableArrayList("Flagged", "Date added", "Done Notes"));
sortByNameBox.setItems(FXCollections.observableArrayList("Priority", "Priority", "Priority", "Priority",
"Priority", "Priority", "Priority", "Priority", "Priority", "Priority", "Priority", "Priority",
"Priority", "Priority"));
generalSortBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> selected, String oldValue, String newValue) {
if (newValue != null) {
switch (newValue) {
case "Done Notes": try {
//THE ACTION NEED TO TAKE PLACE HERE
notesDirectory.insertNotesToTilePane(tilePaneNotesScreen, notesDirectory.deserializedNotesList(notesDirectory.getFileName(new File("src/notesRecycleBin")), "src/notesRecycleBin/"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
public void setMainController(MainWindowController controller) {
this.mainController = controller;
}
}
NotesScreen
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.TilePane?>
<?import javafx.scene.layout.VBox?>
<StackPane fx:id="mainContainer" prefHeight="575.0" prefWidth="950.0" stylesheets="#../../../styles/MainWindow/StackPaneChangable.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.mainWindow.issues.NotesScreenController">
<children>
<VBox fx:id="vBoxContainer" prefHeight="575.0" prefWidth="950.0">
<children>
<HBox fx:id="hBoxFilterContainer" stylesheets="#../../../styles/MainWindow/ChoiceBox/HBoxFilters.css">
<children>
<ComboBox fx:id="generalSortBox" prefHeight="25.0" prefWidth="220.0" promptText="Sort by..." stylesheets="#../../../styles/MainWindow/ChoiceBox/ComboBox.css" />
<ComboBox fx:id="sortByNameBox" prefHeight="25.0" prefWidth="220.0" promptText="Choose name" stylesheets="#../../../styles/MainWindow/ChoiceBox/ComboBox.css" />
</children>
</HBox>
<ScrollPane fx:id="scrollPane" fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER" prefHeight="575.0" prefWidth="950.0" stylesheets="#../../../styles/MainWindow/StackPaneChangable.css" VBox.vgrow="ALWAYS">
<content>
<TilePane fx:id="tilePaneNotesScreen" hgap="25.0" prefColumns="4" prefHeight="575.0" prefTileWidth="228.0" prefWidth="950.0" stylesheets="#../../../styles/MainWindow/StackPaneChangable.css" vgap="25.0">
<padding>
<Insets bottom="25.0" left="25.0" right="25.0" top="35.0" />
</padding>
</TilePane>
</content>
</ScrollPane>
</children>
</VBox>
</children>
</StackPane>
Basically all you are missing is setting the main screen controller in the NotesScreenController instance:
FXMLLoader loader = new FXMLLoader();
NotesScreenController nsc = new NotesScreenController();
loader.setController(nsc);
nsc.setMainController(this);
And then from the NotesScreenController you can clear the pane simply by calling
mainController.clearPane();
i.e. you can do:
generalSortBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> selected, String oldValue, String newValue) {
if (newValue != null) {
switch (newValue) {
case "Done Notes": try {
//THE ACTION NEED TO TAKE PLACE HERE
mainController.clearPane();
notesDirectory.insertNotesToTilePane(tilePaneNotesScreen, notesDirectory.deserializedNotesList(notesDirectory.getFileName(new File("src/notesRecycleBin")), "src/notesRecycleBin/"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
Note you have one subtle bug: when you call
paneNotes = loader.load(paneOneUrl);
you are calling the static FXMLLoader.load(URL) method. Since it's a static method, it's not invoked on the FXMLLoader instance you created, and so the previous call to setController(...) is effectively ignored. You need to set the location of the FXMLLoader instance, and then call the no-arg load() method. So you should have:
issuesNotes.setOnAction(event ->{
clearPane();
URL paneOneUrl = getClass().getClassLoader().getResource("gui/mainWindow/issues/NotesScreen.fxml");
FXMLLoader loader = new FXMLLoader();
NotesScreenController nsc = new NotesScreenController();
nsc.setMainController(this);
loader.setController(nsc);
loader.setLocation(paneOneUrl);
try {
// note call to no-arg load() method:
paneNotes = loader.load();
changablePane.getChildren().add(paneNotes);
} catch (IOException e) {
e.printStackTrace();
}
});
Finally, since you are setting the controller from the Java code, you need to remove the fx:controller attribute from the NotesScreen.fxml root element:
<StackPane fx:id="mainContainer" prefHeight="575.0" prefWidth="950.0" stylesheets="#../../../styles/MainWindow/StackPaneChangable.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<!-- ... -->
</StackPane>

Why is my stage not clickable

This is my first app and i have no idea why is my stage not clickable along with the menuBar. I use SceneBuilder for creating the fxml and I added the tree items in the controllers initialize method.
MainWindowController.java
package gui;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class MainWindowController extends AbstractController implements Initializable {
private Stage stage = null;
#FXML
private AnchorPane mainContainer;
#FXML
private VBox vBoxContainer;
#FXML
private MenuBar menuBarTop;
#FXML
private HBox hBoxContainer;
#FXML
private StackPane navigationSection;
#FXML
private TreeView<String> treeView;
final private TreeItem<String> rootIssues = new TreeItem<String>("IssueTracker");
final private TreeItem<String> issuesTable = new TreeItem<String>("IssuesTable");
final private TreeItem<String> stickers = new TreeItem<String>("Stickers");
#Override
public void initialize(URL location, ResourceBundle resources) {
rootIssues.setExpanded(true);
rootIssues.getChildren().addAll(issuesTable, stickers);
treeView.setRoot(rootIssues);
}
public void setStage(Stage stage) {
this.stage = stage;
stage.setResizable(true);
stage.setTitle("SoloStats - Welcome");
}
public void closeStage() {
if (this.stage != null) {
this.stage.close();
}
}
}
MainWindow.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.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="mainContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MainWindowController">
<children>
<VBox fx:id="vBoxContainer" layoutX="530.0" layoutY="230.0" prefHeight="25.0" prefWidth="1200.0" AnchorPane.bottomAnchor="572.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar fx:id="menuBarTop" prefHeight="25.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</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>
</children>
</VBox>
<HBox fx:id="hBoxContainer" layoutX="384.0" layoutY="238.0" prefHeight="600.0" prefWidth="1200.0" style="-fx-background-color: rgb(247, 247, 247);" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
<children>
<StackPane fx:id="navigationSection" prefHeight="150.0" prefWidth="300.0" style="-fx-background-color: #222;">
<children>
<TreeView fx:id="treeView" fixedCellSize="24.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</StackPane>
</children>
</HBox>
</children>
</AnchorPane>
The only thing different from my other stages is the TreeView. If i make it in the main class it works fine. I think that the problem is within the initialize method and the way I build the treeView, but I have no idea what it can be.
After trying what Gash suggested and running my MainWindow.fxml from Main, the MainWindowController and MainWindow.fxml worked fine. Now the only other thing that stands between the working example and my version is the way i start the stage and I'm doing that after successful login in the starting stage of the app.
HomeScreenController
package gui;
import connectivity.DataBaseHandler;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class HomeScreenController extends AbstractController implements Initializable {
private DataBaseHandler dbHandler = new DataBaseHandler();
#FXML
private AnchorPane homeWindow;
#FXML
private TextField insertNameField;
#FXML
private PasswordField insertPasswordField;
#FXML
private TextField insertDepartmentField;
#FXML
private Button signInButton;
#FXML
private Button signupButton;
private Main main;
#FXML
private Label mainAlertText;
private Stage stage = null;
#Override
public void initialize(URL url, ResourceBundle rb) {
signupButton.setOnAction((event)->{
showPopupWindow();
});
signInButton.disableProperty().bind(Bindings.createBooleanBinding( () -> (insertNameField.getText().isEmpty()
|| insertPasswordField.getText().isEmpty() || insertDepartmentField.getText().isEmpty()),
insertNameField.textProperty(), insertPasswordField.textProperty(), insertDepartmentField.textProperty()));
signInButton.setOnAction((event -> {
if (dbHandler.login(insertNameField.getText(), insertDepartmentField.getText(), insertPasswordField.getText())) {
mainAlertText.setTextFill(Color.GREEN);
mainAlertText.setText("Login Successfull");
closeStage();
showMainWidow();
} else {
mainAlertText.setTextFill(Color.RED);
mainAlertText.setText("One or more of the values are not correct");
}
}));
}
private void showPopupWindow() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("SignUpPopUp.fxml"));
// initializing the controller
SignUpPopUpController popupController = new SignUpPopUpController();
loader.setController(popupController);
Parent layout;
try {
layout = loader.load();
Scene scene = new Scene(layout);
// this is the popup stage
Stage popupStage = new Stage();
popupStage.setResizable(false);
// Giving the popup controller access to the popup stage (to allow the controller to close the stage)
popupController.setStage(popupStage);
if(this.main != null) {
popupStage.initOwner(main.getPrimaryStage());
}
popupStage.initModality(Modality.APPLICATION_MODAL);
popupStage.setScene(scene);
popupStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showMainWidow() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainWindow.fxml"));
//initializing the controller
MainWindowController mainWindowController = new MainWindowController();
Parent layout;
try {
layout = loader.load();
Scene scene = new Scene(layout);
//the main stage
Stage mainStage = new Stage();
mainWindowController.setStage(mainStage);
if (this.main != null) {
mainStage.initOwner(main.getPrimaryStage());
}
mainStage.initModality(Modality.NONE);
mainStage.setScene(scene);
mainStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showHomeScreen() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("HomeScreen.fxml"));
loader.setController(this);
Parent layout;
try {
layout = loader.load();
Scene scene = new Scene(layout);
//this is the stage
Stage mainStage = new Stage();
this.setStage(mainStage);
mainStage.initModality(Modality.APPLICATION_MODAL);
mainStage.setScene(scene);
mainStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setStage(Stage stage) {
this.stage = stage;
this.stage.setResizable(false);
this.stage.setTitle("SoloStats");
}
public Stage getStage() {
return this.stage;
}
public void closeStage() {
this.stage.close();
}
}
I should have provided this class earlier. Sorry for that.
It looks like your missing an import for your AbstractController, which should throw a compile error. I pasted your code into NetBeans and changed
public class MainWindowController extends AbstractController implements Initializable {
to
public class MainWindowController implements Initializable
Everything works as it should.
If you need the AbstractController, you will need to add the import. For example:
import org.springframework.web.servlet.mvc.AbstractController;
My working code looks nearly identical to yours. I'm not able to see any difference, but below are the copies of your code working for me.
MainWindow.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.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="mainContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MainWindowController">
<children>
<VBox fx:id="vBoxContainer" layoutX="530.0" layoutY="230.0" prefHeight="25.0" prefWidth="1200.0" AnchorPane.bottomAnchor="572.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar fx:id="menuBarTop" prefHeight="25.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</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>
</children>
</VBox>
<HBox fx:id="hBoxContainer" layoutX="384.0" layoutY="238.0" prefHeight="600.0" prefWidth="1200.0" style="-fx-background-color: rgb(247, 247, 247);" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
<children>
<StackPane fx:id="navigationSection" prefHeight="150.0" prefWidth="300.0" style="-fx-background-color: #222;">
<children>
<TreeView fx:id="treeView" fixedCellSize="24.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</StackPane>
</children>
</HBox>
</children>
</AnchorPane>
MainWindowController:
package gui;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class MainWindowController implements Initializable {
private Stage stage = null;
#FXML
private AnchorPane mainContainer;
#FXML
private VBox vBoxContainer;
#FXML
private MenuBar menuBarTop;
#FXML
private HBox hBoxContainer;
#FXML
private StackPane navigationSection;
#FXML
private TreeView<String> treeView;
final private TreeItem<String> rootIssues = new TreeItem<String>("IssueTracker");
final private TreeItem<String> issuesTable = new TreeItem<String>("IssuesTable");
final private TreeItem<String> stickers = new TreeItem<String>("Stickers");
#Override
public void initialize(URL location, ResourceBundle resources) {
rootIssues.setExpanded(true);
rootIssues.getChildren().addAll(issuesTable, stickers);
treeView.setRoot(rootIssues);
}
public void setStage(Stage stage) {
this.stage = stage;
stage.setResizable(true);
stage.setTitle("SoloStats - Welcome");
}
public void closeStage() {
if (this.stage != null) {
this.stage.close();
}
}
}
Gui.java: Added stage.setTitle here for window title to work.
package gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Gui extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
stage.setTitle("SoloStats - Welcome");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I got a null pointer exception with your closeStage() call. Instead of using this.stage, try setting the stage using the signInButton and then close it. I'm guessing your login window is APPLICATION_MODAL and isn't closing properly which would prevent input events to your new window. You might find the Stage Docs very helpful.
Here's a snip for your closeStage:
public void closeStage() {
Stage stage = (Stage) signInButton.getScene().getWindow();
stage.close();
}

Listing file names using FXML TableView - unable to populate data

I am trying to populate filesnames in a tabular format using FXML.
I am able to display the table but the rows are not getting displayed.
Directory name will be selected by user during runtime.
.
Main.java
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
AnchorPane root =(AnchorPane)FXMLLoader.load(getClass().getResource("Utility.fxml"));
Scene scene = new Scene(root,600,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("testing");
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
AppController.java
import java.io.File;
import java.util.Arrays;
import java.util.List;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.DirectoryChooser;
public class AppController
{
#FXML private Label BrowseStatus;
#FXML private TextField TDpath;
#FXML private Button CreateH2H;
#FXML private TableView<String> FileListTable;
#FXML private TableColumn<FilesInDir,String> FileNameCol;
#FXML private ObservableList<String> fidlist;
#FXML
protected void handleBrowseWindowsExplorer(ActionEvent event){
DirectoryChooser TestDataDir = new DirectoryChooser();
TestDataDir.setTitle("Select path");
File selectedDir = TestDataDir.showDialog(null);
if(selectedDir == null){
BrowseStatus.setText("Nothing choosen");
TDpath.setText("");
} else {
TDpath.setText(selectedDir.getAbsolutePath());
FileListTable = new TableView<String>();
FileNameCol.setCellValueFactory(new PropertyValueFactory<FilesIndDir,String>("FileName"));
FileListTable.setPlaceholder(BrowseStatus);
BrowseStatus.setText("Folder has been selected");
File tFile = new File(TDpath.getText());
File[] listOfFiles = tFile.listFiles();
fidlist = FXCollections.observableArrayList();
List<String> fileNameList = null;
for (int i=0; i<listOfFiles.length; i++) {
fileNameList = Arrays.asList(listOfFiles[i].getName());
}
fidlist.addAll(fileNamesList);
FileListTable.setItems(fidlist);
}
}
}
Utility.fxml
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collection.*?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane fx:controller="application.AppController" id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns:fx="http://javafx.com/fxml">
<children>
<GridPane>
<children>
<MenuBar maxWidth="-Infinity" prefWidth="800.0" GridPane.ColumnIndex="0" GridPane.RowIndex="0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
</menus>
</MenuBar>
<BorderPane GridPane.ColumnIndex="0" GridPane.RowIndex="1">
<top>
<TabPane PrefHeight="700.0" PrefWidth="900.0" tabClosingPolicy="UNAVAILABLE">
<Tab text="Files tab ">
<content>
<GridPane hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25" /></padding>
<children>
<Label text="Path :" GridPane.ColumnIndex="0" GridPane.RowIndex="1" />
<TextField fx:id="TDpath" GridPane.ColumnIndex="0" GridPane.RowIndex="1" />
<Button text="Browse" onAction="#handleBrowseWindowsExplorer" GridPane.ColumnIndex="3" GridPane.RowIndex="1" />
<VBox prefHeight="300.0" prefWidth="400.0" spacing="6.0" VBox.vgrow="ALWAYS" GridPane.ColumnIndex="0" GridPane.RowIndex="3" GridPane.columnSpan="4" >
<children>
<TableView fx:id="FileListTable" >
<placeholder><Label fx:id="BrowseStatus" text="No files in selected directory" /></placeholder>
<columns>
<TableColumn fx:id="FileNameCol" text="File Names" prefWidth="400">
<cellValueFactory>
<PropertyValueFactory property="FileName" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</VBox>
</children>
</GridPane>
</content>
</Tab>
</TabPane>
</top>
</BorderPane>
</children>
</GridPane>
</children>
</AnchorPane>
FilesInDir.java
import javafx.beans.property.SimpleStringProperty;
public class FilesInDir {
private final SimpleStringProperty FileName = new SimpleStringProperty();
public FilesInDir(String fName) {
setFileName(fName);
}
public String FileNameProperty() {
return FileName.get();
}
public void setFileName(String fName) {
this.Filename.set(fName);
}
}
In this answer I'll just ignore all the typos, since there seems to be a compilable version of the code and point out the other errors:
FilesInDir
If I got your intention, this class should contain the info for a file that should be displayed. Besides ignoring the naming convention of starting identifiers of class members with a lowercase letter, the real issue here is the FileNameProperty. The property method with the suffix Property has to return the property itself and PropertyValueFactory relies on that fact. It should look like this:
public StringProperty fileNameProperty() {
return FileName;
}
Also you did not use the class as type parameter of your TableView:
#FXML
private TableView<FilesInDir> FileListTable;
AppController.handleBrowseWindowsExplorer
The main issue is in this method:
There's no need to need to make fidlist a field, let alone annotate it with #FXML. In fact there's no need to create a new ObservableList at all.
You recreate the TableView, that was created and injected by the FXMLLoader and do not insert it to the scene graph, but you work with the new TableView, which is not the one that is displayed on screen.
If no file is selected, the TextField is emptied, but the items in the TableView from a previously selected directory remain, leading to inconsistent behaviour.
fileNameList = Arrays.asList(listOfFiles[i].getName()); in the for loop replaces the list with a new List containing a single element every time it's executed.
File tFile = new File(TDpath.getText()); recreates the file from the String that was created by converting the selected file to a String. That's just unnecessary; you should simply use selectedDir
Changing the method like this should work, assuming you fixed the points mentioned for FilesInDir too:
#FXML
protected void handleBrowseWindowsExplorer(ActionEvent event) {
DirectoryChooser TestDataDir = new DirectoryChooser();
TestDataDir.setTitle("Select path");
File selectedDir = TestDataDir.showDialog(null);
if (selectedDir == null) {
BrowseStatus.setText("Nothing choosen");
FileListTable.getItems().clear();
TDpath.setText("");
} else {
TDpath.setText(selectedDir.getAbsolutePath());
BrowseStatus.setText("Folder has been selected");
File[] listOfFiles = selectedDir.listFiles();
ArrayList<FilesInDir> fidlist = new ArrayList<>(listOfFiles.length);
for (File listOfFile : listOfFiles) {
fidlist.add(new FilesInDir(listOfFile.getName()));
}
FileListTable.getItems().setAll(fidlist);
}
}

Java Fx, Edit the textarea of a specific tab

Well, my question is simple.
I am building an application with Java Fx, and I have a TabPane.
When I open a new Tab, that Tab gets it's content by a fxml file.
tab.setContent((Node)FXMLLoader.load(this.getClass().getResource("/main/textEditor.fxml")))
Fine, that works well, it always loads the same file on all Tabs, so, all textarea on all tabs have the same id.
The problem is, with java, I can only get the information of the textarea of the first Tab.
How can i edit specifically the textarea of one tab in particular?
An example of what i want to do :
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) {
Application.launch(Main.class, args);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/tabPane/test.fxml"));
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setScene(new Scene(root));
stage.show();
}
}
test.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tabPane.controller">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" onAction="#test" text="File">
<items>
<MenuItem fx:id="insert" mnemonicParsing="false" onAction="#test" text="Insert" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="
" text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<font>
<Font size="18.0" />
</font>
</Label>
<TabPane fx:id="tabPane" prefHeight="375.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<TextArea fx:id="textarea" prefHeight="200.0" prefWidth="200.0" text="a" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<TextArea fx:id="textarea1" prefHeight="200.0" prefWidth="200.0" text="a" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
</children>
<stylesheets>
<URL value="#../../../../BasicApplicatio11n_css/BasicApplication.css" />
controller.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
public class controller implements Initializable{
#FXML
private TextArea textarea;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void test(ActionEvent event){
textarea.appendText("Text");
}
}
There are two tabs on this example, when the button is pressed, I want to add the text on the current selected tab.
There are a few different ways to do this. One way is to let the controller for the tab content expose the textProperty from the text area. Then in your "main controller", create a StringProperty field. When you create a new tab, just observe its selected property and update the string property field to point to the one from the current controller. Then you can easily load text into the "current" pane.
Here's a simple example of this:
EditorController:
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
public class EditorController {
#FXML
private TextArea textArea ;
public StringProperty textProperty() {
return textArea.textProperty() ;
}
}
with textEditor.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TextArea?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="EditorController">
<center>
<TextArea fx:id="textArea"/>
</center>
</BorderPane>
And then the MainController could look like:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.FileChooser;
public class MainController {
#FXML
private TabPane tabPane ;
private StringProperty currentText ;
public void initialize() throws IOException {
// load an initial tab:
newTab();
}
#FXML
private void newTab() throws IOException {
Tab tab = new Tab("Untitled text");
FXMLLoader loader = new FXMLLoader(getClass().getResource("textEditor.fxml"));
tab.setContent(loader.load());
EditorController controller = loader.getController() ;
tab.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
currentText = controller.textProperty();
}
});
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);
}
#FXML
private void load() {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(tabPane.getScene().getWindow());
if (file != null) {
Path path = file.toPath();
try {
currentText.set(String.join("\n", Files.readAllLines(path)));
} catch (IOException e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setContentText("Unable to load file "+path);
alert.setTitle("Load error");
alert.showAndWait();
}
tabPane.getSelectionModel().getSelectedItem().setText(path.getFileName().toString());
}
}
}
with main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainController">
<center>
<TabPane fx:id="tabPane"/>
</center>
<bottom>
<HBox alignment="CENTER" spacing="5">
<padding>
<Insets top="5" right="5" left="5" bottom="5" />
</padding>
<Button text="Load..." onAction="#load" />
<Button text="New" onAction="#newTab"/>
</HBox>
</bottom>
</BorderPane>
For completeness, a simple application class:
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 Main extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
There are other approaches, e.g. you could just change the button's onAction property when the tab selection changes, etc.

Categories

Resources