Multiple fxmls in borderpane, cant get parent or share controller - java

ToolBar.java
package util;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
public class ToolBar {
private static BorderPane borderPane;
public void setBorderPane(BorderPane borderPane) {
this.borderPane = borderPane;
}
public static void initialize(BorderPane borderPane) {
ToolBar toolBar = new ToolBar();
toolBar.setBorderPane(borderPane);
}
public static void show(Parent parent) {
borderPane.setLeft(parent);
}
public static void hide() {
borderPane.setLeft(null);
}
}
HomeController.java
package main;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import util.MainContent;
import util.ToolBar;
import java.net.URL;
import java.util.ResourceBundle;
public class HomeController implements Initializable {
#FXML
private Button editorButton;
#FXML
private BorderPane borderPane;
private MainContent mainContent;
#Override
public void initialize(URL location, ResourceBundle resources) {
mainContent = new MainContent(borderPane);
}
#FXML
private void goToEditor() throws Exception {
mainContent.show(getClass().getResource("editor.fxml"));
ToolBar.show(FXMLLoader.load(getClass().getResource("toolBar.fxml")));
}
}
I am trying to use toolbar buttons to access the borderpane. Such as loading new content to the parent borderpane but setCenter with another fxml file.
I tried using an element with the toolbar with
borderpane = (BorderPane) toolBar.getParent();
borderpane.setCenter("someFXML.fxml");
But NullPointerException error occurs, any suggestions? thanks.

Related

JavaFX Scene 2 button on actions not working

I have 2 scenes. One of the scenes is Login and the other is Register. When I click the register button on the Log in screen it switches me to the register scene. The actions I have made in the controller for the button is not working. Do I need to do something in the main or controller to get the Register.fxml to work?
ModelController:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.sql.*;
public class ModelController extends Main{
#FXML
private Label LblStatus;
#FXML
private TextField UserEmail;
#FXML
private TextField UserPassword;
#FXML
private TextField RegisterEmail;
#FXML
private TextField RegisterPassword;
#FXML
private TextField RegisterFirst;
#FXML
private TextField RegisterLast;
#FXML
private TextField RegisterSSN;
#FXML
private TextField RegisterAddress;
#FXML
private TextField RegisterCity;
#FXML
private TextField RegisterState;
#FXML
private TextField RegisterZip;
#FXML
private Button LoginRegister;
public void Login(ActionEvent event) throws Exception {
if(UserEmail.getText().equals("User") && UserPassword.getText().equals("pass")) {
LblStatus.setText("Login Success");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
else {
LblStatus.setText("Login Failed");
}
}
public void handleButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Register.FXML"));
Parent root1 = (Parent) fxmlLoader.load();
Scene Register = new Scene(root1);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Register);
window.show();
}
catch(Exception e) {
}
}
public void CompleteRegistration(ActionEvent event) {
}
Main:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.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();
}
}
public static void main(String[] args) {
launch(args);
}
}
The 2nd scene was not pulling in the controller.

Getting syntax error while binding label to StringProperty

I am just starting to learn javafx.I am trying to make a simple application where you enter into a textfield and simultaniously give it out via the label.
Heres the main class
package testing;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class testing extends Application {
private Stage primaryStage;
private BorderPane RootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("First Application");
initRootLayout();
initProjectTest();
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void initRootLayout() {
try{ FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/RootLayout.fxml"));
RootLayout=(BorderPane)loader.load();
primaryStage.setScene(new Scene(RootLayout));
primaryStage.show();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void initProjectTest() {
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/ProjectTest.fxml"));
AnchorPane ProjectTest=(AnchorPane)loader.load();
RootLayout.setCenter(ProjectTest);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Here is the controller class
package testing.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class ProjectTestController {
#FXML
private Label label1=new Label();
#FXML
private TextField textfield1=new TextField();
label1.textProperty().bind(textfield1.textProperty());
}
If I try to implement it via a button where the text is updated everytime I click the button(via a onAction function) the code works fine. However I get syntax error when I try to bind. I tried to search for answers but none of were having syntax errors.

(JavaFX) How do I initialize my TreeView?

Im not able to initialize my treeview,my fx:id is right but the output just gives me a blank treeview.
I am using scene builder with fxml controllers.
This is my Fxml Controller.
///////////////////////////////////////////////////////////////////
import java.net.URL;
import static java.time.Clock.system;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
/**
*
* #author Man Eesh
*/
public class FXMLDocumentController implements Initializable {
#FXML
private TreeView<String> treeView;
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event){
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// System.out.print("here");
label.setText("Hello World!");
TreeItem<String> root = new TreeItem<>("Root Node");
root.setExpanded(true);
System.out.print("here");
root.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
treeView.setRoot(root);
}
}

I want to add items seperate from a ListView

Newbie question. I'm trying to make a javafx to do list class but i need to add a button and textfield to insert data to the listview. I want to add these but I can't add them directly to the listview. I dunno how to do it.
package learning;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class ToDoList extends Application{
private ObservableList<String> doList = FXCollections.observableArrayList();
private String text="";
public void start(Stage primaryStage){
primaryStage.setTitle("2DoList");
TextField userTextField = new TextField();
Button enter=new Button("Add");
enter.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
text= userTextField.getText();
doList.add(text);
userTextField.setText("");
text="";
}
});
ListView<String> root=new ListView<String>(doList);
Scene scene=new Scene(root,250,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

java fx imageslideshow using animations

i'm trying to make simple slideshow using java fx. I'm implementing methods which allow me after pressing button to show one image after another until list is over.
For now i have a code like this...it works but it shows images too fast i dont know why.Maybe someone can help me and tell me how can i slow down this timeline variable or timer.thanks in advance:
package pl.gallery.controller;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import pl.gallery.model.ImageParser;
public class MainPaneController implements Initializable {
#FXML
private BorderPane borderPane;
#FXML
private Button nextButton;
#FXML
private MenuBar menuBar;
#FXML
private AnchorPane anchorPaneTop;
#FXML
private HBox hBox;
#FXML
private Button previousButton;
#FXML
private MenuItem openFolder;
#FXML
private AnchorPane anchorPaneCenter;
#FXML
private ImageView imageView;
#FXML
private Button slideShowButton;
#FXML
private Menu menu;
private Image image;
private ImageParser parser;
private ObservableList<Image> imagesList;
private int indexPrev = 0;
private int indexNext = 0;
private Timeline timeline;
private AnimationTimer timer;
//variable for storing actual frame
private Integer i=0;
#Override
public void initialize(URL location, ResourceBundle resources)
throws IndexOutOfBoundsException {
parser = new ImageParser();
imagesList = FXCollections.observableArrayList();
DirectoryChooser dc = new DirectoryChooser();
openFolder.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
/*
* parser = new ImageParser(); imagesList = new
* ArrayList<Image>(); DirectoryChooser dc = new
* DirectoryChooser(); File dir = dc.showDialog(new Stage());
* parser.createFileList(dir);
*
*
* imagesList.add(new Image(parser.getFilesList().get(0)
* .toURI().toString())); imageView.setImage(imagesList.get(0));
*/
File dir = dc.showDialog(new Stage());
imagesList = parser.createImagesListFromFileList(dir);
imageView.setImage(imagesList.get(0));
}
});
slideShowButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
Duration duration = new Duration(1000);
timeline.setDelay(duration);
timer = new AnimationTimer() {
#Override
public void handle(long l) {
imageView.setImage(imagesList.get(i));
i++;
}
};
timeline.play();
timer.start();
}
});
}
}

Categories

Resources