When an item in a listview is clicked, show details - javafx

I am going to show details about a movie when the movie is clicked in
a listview.
Currently, nothing happens when I click the movie.
Does anyone know how I can make this happen?
I have no idea how I am going to do this.
My code is included below.
Controller:
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import no.hiof.aleksar.oblig4.data.DataHandler;
import no.hiof.aleksar.oblig4.model.Film;
public class FilmController {
#FXML
private Label navnFilmLabel;
#FXML
private TextField beskrivelseTextField, utgivelsesdatoTextField, spilletidTextField;
#FXML
private ListView listeMedFilmer;
private int valgtFilm = 0;
private Film denValgteFilmen;
private ObservableList<Film> filmListe;
#FXML
public void initialize() {
listeMedFilmer.setItems(DataHandler.hentFilmData());
filmListe = DataHandler.hentFilmData();
if (valgtFilm < filmListe.size()) {
Film enFilm = filmListe.get(valgtFilm);
fyllUtFilm(enFilm);
}
}
private void fyllUtFilm(Film enFilm) {
if (enFilm != null) {
navnFilmLabel.setText(enFilm.getTittel());
beskrivelseTextField.setText(enFilm.getBeskrivelse());
utgivelsesdatoTextField.setText(enFilm.getUtgivelsesdato().toString());
spilletidTextField.setText(String.valueOf(enFilm.getSpilletid()));
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.hiof.aleksar.oblig4.controller.FilmController">
<children>
<ListView fx:id="listeMedFilmer" prefHeight="400.0" prefWidth="291.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="300.0" AnchorPane.topAnchor="0.0" />
<Button layoutX="310.0" layoutY="373.0" mnemonicParsing="false" text="Ny" />
<Button layoutX="348.0" layoutY="373.0" mnemonicParsing="false" text="Rediger" />
<Button layoutX="412.0" layoutY="373.0" mnemonicParsing="false" text="Slett" />
<Label fx:id="navnFilmLabel" layoutX="312.0" layoutY="14.0" text="Film/Serie/Episode">
<font>
<Font name="System Bold" size="27.0" />
</font>
</Label>
<GridPane layoutX="312.0" layoutY="68.0" prefHeight="208.0" prefWidth="276.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Beskrivelse:" />
<Label text="Utgivelsesdato:" GridPane.rowIndex="1" />
<Label text="Spilletid:" GridPane.rowIndex="2" />
<TextField fx:id="beskrivelseTextField" prefHeight="50.0" prefWidth="138.0" GridPane.columnIndex="1">
<font>
<Font size="10.0" />
</font></TextField>
<TextField fx:id="utgivelsesdatoTextField" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="10.0" />
</font></TextField>
<TextField fx:id="spilletidTextField" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="10.0" />
</font></TextField>
</children>
</GridPane>
</children>
</AnchorPane>
Datahandler:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import no.hiof.aleksar.oblig4.model.Film;
import no.hiof.aleksar.oblig4.model.Person;
import java.time.LocalDate;
public class DataHandler {
private final static ObservableList<Film> filmListe = FXCollections.observableArrayList();
public static ObservableList<Film> hentFilmData() {
if (filmListe.isEmpty()) {
genererFilmData();
}
return filmListe;
}
private static void genererFilmData() {
filmListe.add(new Film("Inception", "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO." , 148, LocalDate.of(2010, 7, 21), new Person("Leonardo", "DiCaprio", LocalDate.of(1974, 11, 11))));
filmListe.add(new Film("The Imitation Game", "During World War II, the English mathematical genius Alan Turing tries to crack the German Enigma code with help from fellow mathematicians." , 114, LocalDate.of(2014, 12, 25), new Person("Benedict", "Cumberbatch", LocalDate.of(1976, 7, 19))));
filmListe.add(new Film("Jobs", "The story of Steve Jobs' ascension from college dropout into one of the most revered creative entrepreneurs of the 20th century." , 128, LocalDate.of(2013, 11, 1), new Person("Ashton", "Kutcher", LocalDate.of(1978, 2, 7))));
}
}

Related

My FXML page is not loading, I cannot figure out why

I am having problems figuring out why a page I am trying to load with my JFX FXML loader the page does not load..
With regards to my my FXML load class, my java code complies, but when I execute the code the "More Details page does not load and display when I click on the "More Details" button...here is the code segment that loads the "More Details" page...
// Now set up the stage and place the "post_details_update" on it.
Stage unilink_home_stage = (Stage) btnl_posting_more_details.getScene().getWindow();
unilink_home_stage.close();
// This closes the Login dialog
// now call the post_details_update.fxml
try {
//Parent post_details = FXMLLoader.load( getClass().getResource("/unilink_home/post_details/post_details_update.fxml"));
Parent post_details = FXMLLoader.load( getClass().getResource("/view/unilink_home/post_details/post_details_update.fxml"));
// Note: the path is not relative to the package that this file is in
// but the CLASSPATH from the src/
Stage post_details_stage = new Stage();
post_details_stage.setTitle("Welcome to UniLink");
post_details_stage.setScene(new Scene(post_details));
post_details_stage.show();
} //close the try block
catch(Exception e)
{
//System.out.print(e);
//e.printStackTrace();
System.out.println( e.getMessage() );
}// close the catch block
And the folder structure is...
and the FXML page is.... "post_details_update.fxml":
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<AnchorPane
fx:id="post_details"
xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/11.0.1"
fx:controller="controller/unilink_home/post/details/unilink_home_post_detailsController"
>
<children>
<VBox prefHeight="401.0" prefWidth="850.0">
<children>
<ToolBar prefHeight="40.0" prefWidth="850.0">
<items>
<Button mnemonicParsing="false" text="Back to Main Window" />
<Label text="Review Post Details">
<padding>
<Insets left="250.0" />
</padding>
</Label>
</items>
</ToolBar>
<GridPane prefHeight="183.0" prefWidth="849.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="474.0" minWidth="10.0" prefWidth="215.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="541.0" minWidth="10.0" prefWidth="101.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="541.0" minWidth="10.0" prefWidth="534.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="144.0" minHeight="10.0" prefHeight="144.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="71.0" minHeight="10.0" prefHeight="39.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fitHeight="135.0" fitWidth="222.0" pickOnBounds="true" preserveRatio="true">
<GridPane.margin>
<Insets left="50.0" />
</GridPane.margin>
<image>
<Image url="#../../../../images/under_construction.png" />
</image>
</ImageView>
<GridPane prefHeight="131.0" prefWidth="277.0" GridPane.columnIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Post ID:" />
<Label layoutX="10.0" layoutY="25.0" text="Title:" GridPane.rowIndex="1" />
<Label layoutX="10.0" layoutY="72.0" text="Description:" GridPane.rowIndex="2" />
<Label layoutX="10.0" layoutY="89.0" text="Creator:" GridPane.rowIndex="3" />
<Label layoutX="10.0" layoutY="99.0" text="Date Created:" GridPane.rowIndex="4" />
<Label layoutX="10.0" layoutY="105.0" text="Status:" GridPane.rowIndex="5" />
</children>
</GridPane>
<GridPane layoutX="225.0" layoutY="10.0" prefHeight="131.0" prefWidth="277.0" GridPane.columnIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="47.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="46.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="46.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<Button mnemonicParsing="false" text="Upload Image" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="70.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" text="Close Post" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" text="Delete Post" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="180.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" text="Save" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="350.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
<ListView prefHeight="200.0" prefWidth="200.0" />
</children>
</VBox>
</children>
</AnchorPane>
And the "unilink_home_post_detailsController.java" is just a shell at this stage...
package controller.unilink_home.post.details;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import model.connectionTest;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ChoiceBox;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
//import javafx.event.Event;
//Import the classes for database query
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import model.table_setup_view_postings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import java.net.URL;
import java.util.ResourceBundle;
import model.posting;
public class unilink_home_post_detailsController implements Initializable
{
//database connection related variables,
private String query;
//This is the SQL query string
PreparedStatement ps;
//PreparedStatement ps2;
// This is the prepared statement for the SQL query
private Connection db_connect;
// this is the database connection string for SQL query results
final String DB_NAME = "testDB";
// This is the database name
#FXML // fx:id="post_details"
private AnchorPane post_details; // Value injected by FXMLLoader
//===================================================================================================================
/*
* This method is the class constructor and sets up the controller.
*/
public void unilink_home_post_detailsController()
{
}// close public void unilink_home_post_detailsController()
//===================================================================================================================
/*
* This method will initialiise the UI
*/
//#FXML
#Override
public void initialize(URL location, ResourceBundle resources)
{
}// close public void initialize(URL location, ResourceBundle resources)
}// close public class unilink_home_post_detailsController
With this:
1. The code will compile.
2. When I click the button "More Details"... nothing happens and I am also challenged how I can debug it because in Eclipse with Java JFX the step in/over buttons are greyed out, and it is not showing the variables so I can see the state of these vraibles?
Can anyone help, as I am somewhat new to JFX, and I have read that the FXML loader can be finnicky...
Sincerely.
I found the problem was in the path that I used to point to the controller... somehow I had "/" instead of "." when I made the change it worked. Thank you to those whoresponded.

Error in Setting Vbox inside Border pane Center on click of button

I am designing an javafx application using BorderPane as Rootlayout inside the border pane there is button on left. on Click of which i need to set the borderpane(rootlayout) center to a different addprodct.fxml . i want to add a vbox on Click on Insert Button inside Center of BorderPane.
I checked if my fxml is getting loaded properly or not with this
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml")); its print exact path to fxml. i tried to Setting Stage with the new fxml to see if it loads properly and it worked properly.
FXMLLoader loader = new FXMLLoader();
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml"));
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
tryvbox = loader.load();
main.getPrimaryStage().setScene(new Scene(tryvbox));
Yes, i know same question already have been asked but i implemented the solution given neither helped my problem.
Load new fxml in borderpane center
JavaFX: How to update the center view of a borderpane with new values
Main.java
package sample;
import Controllers.RootController;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#FXML
private BorderPane rootLayout;
private Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
mainwindow();
}
public void mainwindow() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/Views/Rootlayout.fxml"));
rootLayout = loader.load();
RootController rootController = loader.getController();
rootController.setMain(this);
Scene scene= new Scene(rootLayout);
primaryStage.setTitle("Inventory Manager");
primaryStage.setScene(scene);
primaryStage.show();
}
public BorderPane getRootLayout() {
return rootLayout;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
RootController.java
package Controllers;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import sample.Main;
import java.io.IOException;
public class RootController {
#FXML
private Button rootBtnInsrt;
#FXML
private Button rootBtnUpdate;
#FXML
private BorderPane rootLayout;
private Main main;
public void setMain(Main main) {
this.main = main;
}
#FXML
void btnInsrtClick(MouseEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
AddProductController addProductController= loader.getController();
addProductController.setMain(main);
main.getRootLayout().setCenter(rootLayout);
}
}
rootlayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="rootLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.RootController">
<top>
<MenuBar prefHeight="21.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<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>
</top>
<bottom>
<AnchorPane prefHeight="29.0" prefWidth="600.0" style="-fx-background-color: Black;" BorderPane.alignment="CENTER" />
</bottom>
<left>
<VBox alignment="CENTER" prefHeight="345.0" prefWidth="164.0" spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="rootBtnInsrt" mnemonicParsing="false" onMouseClicked="#btnInsrtClick" prefHeight="50.0" prefWidth="132.0" text="Insert " />
<Button fx:id="rootBtnUpdate" mnemonicParsing="false" onMouseClicked="#btnUpdateClick" prefHeight="50.0" prefWidth="135.0" text="Update" />
<Button mnemonicParsing="false" prefHeight="50.0" prefWidth="158.0" text="View" />
</children>
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</VBox>
</left>
</BorderPane>
Addproduct.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="addproduct" alignment="CENTER" prefHeight="249.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" text="Add Product" VBox.vgrow="ALWAYS" />
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Product Code" />
<Label text="Product Name :" GridPane.rowIndex="1" />
<Label text="Product Price :" GridPane.rowIndex="2" />
<Label text="Product Quantity :" GridPane.rowIndex="3" />
<TextField GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</GridPane>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="ADD" HBox.hgrow="ALWAYS" />
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Reset" HBox.hgrow="ALWAYS" />
<Button maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Back" HBox.hgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</HBox>
</children>
</VBox>
when tried running the following code i get error.
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
all in same error pane
this is my directory structure.
Directory Structure
So basically after all the work and help the problem arose due to the Try/Catch block.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
i removed the try/catch and added exception to the method i.e void btnInsrtClick(MouseEvent event) throws IOException

How to create Javafx save, read information from text file and letting user to edit their information

How to create JavaFx RegisterGUI save user information(Username,password,name,email,phone number,address) and load into UserProfile GUI that allow the user to edit their profile information after login with their username and password? Hope you guys can help me with the problems.
UserGUI:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.GUIButton">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button id="btnEdit" fx:id="btnEdit" mnemonicParsing="false" onAction="#setOnAction" text="Edit Profile" GridPane.columnIndex="1" GridPane.rowIndex="9" />
<Button id="btnSave" fx:id="btnSave" mnemonicParsing="false" onAction="#setOnAction" prefHeight="26.0" prefWidth="73.0" text="Save" GridPane.columnIndex="2" GridPane.rowIndex="9" />
<Button id="btnCancel" fx:id="btnCancel" mnemonicParsing="false" onAction="#setOnAction" prefHeight="26.0" prefWidth="66.0" text="Cancel" GridPane.columnIndex="3" GridPane.rowIndex="9" />
<Text id="txtName" strokeType="OUTSIDE" strokeWidth="0.0" text="Name" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text id="txtPhoneNumber" strokeType="OUTSIDE" strokeWidth="0.0" text="Phone Number" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Text id="txtEmail" strokeType="OUTSIDE" strokeWidth="0.0" text="Email" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text id="txtAddress" strokeType="OUTSIDE" strokeWidth="0.0" text="Address" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<TextField id="txtfName" onAction="#setOnAction" prefHeight="26.0" prefWidth="203.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextField id="txtfPhoneNumber" onAction="#setOnAction" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<TextField id="txtfEmail" onAction="#setOnAction" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextArea id="txtaAddress" onDragDetected="#setOnAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<ImageView id="imgUser" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.rowIndex="2" />
</children>
</GridPane>
RegisterGUI :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>
<GridPane id="RegisterGUI" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" fx:controller="application.RegisterButton">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="193.5999755859375" minWidth="10.0" prefWidth="151.20003662109374" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="193.5999755859375" minWidth="10.0" prefWidth="151.20003662109374" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="447.9999633789063" minWidth="10.0" prefWidth="447.9999633789063" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Email" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Username" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Name" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="IC Number" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Phone Number" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Address" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<TextField id="txtfEmail" fx:id="txtfEmail" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextField id="txtfUsername" fx:id="txtfUsername" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<TextField id="txtfName" fx:id="txtfName" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<TextField id="txtfIC" fx:id="txtfIC" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextField id="txtfPhone" fx:id="txtfPhone" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<TextArea id="txtaAddress" fx:id="txtaAddress" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<Button id="btnRegister" fx:id="btnRegister" mnemonicParsing="false" onAction="#OnRegister" text="Register" GridPane.columnIndex="2" GridPane.rowIndex="9" />
<Button id="btnExit" fx:id="btnExit" mnemonicParsing="false" onAction="#OnExit" prefHeight="26.0" prefWidth="56.0" text="Exit" GridPane.columnIndex="1" GridPane.rowIndex="9" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField id="txtfPassword" fx:id="txtfPassword" GridPane.columnIndex="2" GridPane.rowIndex="3" />
</children>
</GridPane>
Ok build a small login, Signup, and Edit app for you, You can modify it as u like
So we have 4 class files. and 2 FXML files and Textfile
Luncher.java
package loginApplication;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Luncher extends Application{
public static Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LoginController.java
package loginApplication;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class LoginController
{
#FXML
private TextField email_textfield;
#FXML
private PasswordField password_textfield;
#FXML
private Label error_lab;
#FXML
public void signupButton(ActionEvent event) {
openWindow();
}
#FXML
public void loginButton(ActionEvent event) throws IOException
{
//Path to text file
Path path = Paths.get("TextFiles/data.txt");
//Counts number of line in text file
long count = Files.lines(path).count();
/// read each line
for (int i = 0; i < count; i++)
{
String line = Files.readAllLines(path).get(i);
if(!line.trim().equals(""))
{
//According to format Name, Email, Password, Age, Gender
String[] profile = line.split(",");
String name = profile[0];
String email = profile[1];
String password = profile[2];
String age = profile[3];
String gender = profile[4];
String contact = profile[5];
//Email Matched!
if(email.trim().equals(email_textfield.getText()))
{ //Note trim() method remove space from front and behind of string if there is any
//Now checking if password match
if(password.trim().equals(password_textfield.getText()))
{
Alert msg = new Alert(AlertType.CONFIRMATION);
msg.setTitle(email_textfield.getText());
msg.setContentText("Email and password matched");
msg.showAndWait();
//Store values
Storage.setName(name);
Storage.setEmail(email);
Storage.setAge(age);
Storage.setGender(gender);
Storage.setContact(contact);
Storage.setPassword(password);
//open login window
openWindow();
break; //Email match and pass match, Close loop
}
}
}
}
if(Storage.getEmail() == null) {
System.out.println("No such email");
Alert msg = new Alert(AlertType.ERROR);
msg.setTitle(email_textfield.getText());
msg.setContentText("No such Email : " +email_textfield.getText());
msg.showAndWait();
} else if (Storage.getPassword() == null){
System.out.println("No such email");
Alert msg = new Alert(AlertType.ERROR);
msg.setTitle(email_textfield.getText());
msg.setContentText("Wrong password");
msg.showAndWait();
}
}
private void openWindow()
{
System.out.println("Profile open");
try {
Parent root = FXMLLoader.load(getClass().getResource("Profile.fxml"));
Luncher.stage.setScene(new Scene(root));
} catch (IOException e) {
e.printStackTrace();
}
}
}
ProfileController.java
package loginApplication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class ProfileController implements Initializable{
#FXML
private TextField name;
#FXML
private TextField email;
#FXML
private TextField gender;
#FXML
private TextField contact;
#FXML
private TextField age;
#FXML
private PasswordField password;
#Override
public void initialize(URL location, ResourceBundle resources) {
name.setText(Storage.getName());
age.setText(Storage.getAge());
gender.setText(Storage.getGender());
email.setText(Storage.getEmail());
contact.setText(Storage.getContact());
password.setText(Storage.getPassword());
//Open windows for sign up if nall else do this
if(Storage.getEmail() !=null)
{
//Disable everything
name.setDisable(true);
age.setDisable(true);
gender.setDisable(true);
email.setDisable(true);
contact.setDisable(true);
password.setDisable(true);
}
}
#FXML
private Button save;
#FXML
private Button edit;
#FXML
void editButton(ActionEvent event)
{
System.out.println("edit button");
//Active everything
name.setDisable(false);
age.setDisable(false);
gender.setDisable(false);
email.setDisable(false);
contact.setDisable(false);
password.setDisable(false);
}
#FXML
void saveButton(ActionEvent event) {
//Disable everything
name.setDisable(true);
age.setDisable(true);
gender.setDisable(true);
email.setDisable(true);
contact.setDisable(true);
password.setDisable(true);
removeLine();
addLine();
}
private void addLine() {
String line = name.getText() +"," +email.getText() +"," +password.getText() +"," +age.getText() +"," +gender.getText()+"," +contact.getText();
FileWriter file_writer;
try {
file_writer = new FileWriter("TextFiles/data.txt",true);
BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
buffered_Writer.write(line);
buffered_Writer.flush();
buffered_Writer.close();
} catch (IOException e) {
System.out.println("Add line failed!!" +e);
}
}
private void removeLine() {
try
{
BufferedReader file = new BufferedReader(new FileReader("TextFiles/data.txt"));
String line;
String input = "";
while ((line = file.readLine()) != null)
{
//System.out.println(line);
if (line.contains(Storage.getEmail()))
{
line = "";
System.out.println("Line deleted.");
}
input += line + '\n';
}
FileOutputStream File = new FileOutputStream("TextFiles/data.txt");
File.write(input.getBytes());
file.close();
File.close();
}
catch (Exception e)
{
System.out.println("Problem reading file.");
}
}
}
Storage.java
package loginApplication;
public class Storage {
private static String Name;
private static String Age;
private static String Gender;
private static String Email;
private static String Contact;
private static String password;
// This is how u should do it but u can also make field public and use them directly
// for example -> 'Public static String Name' then use it as 'Storage.name'
public static String getName() {
return Name;
}
public static void setName(String name) {
Name = name;
}
public static String getAge() {
return Age;
}
public static void setAge(String age) {
Age = age;
}
public static String getGender() {
return Gender;
}
public static void setGender(String gender) {
Gender = gender;
}
public static String getEmail() {
return Email;
}
public static void setEmail(String email) {
Email = email;
}
public static String getContact() {
return Contact;
}
public static void setContact(String contact) {
Contact = contact;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
Storage.password = password;
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?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.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#loginButton" prefHeight="390.0" prefWidth="304.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginApplication.LoginController">
<children>
<TextField fx:id="email_textfield" layoutX="38.0" layoutY="83.0" prefHeight="34.0" prefWidth="229.0" promptText="Email">
<font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField fx:id="password_textfield" layoutX="38.0" layoutY="152.0" prefHeight="34.0" prefWidth="229.0" promptText="Password" />
<Button layoutX="87.0" layoutY="222.0" mnemonicParsing="false" onAction="#loginButton" prefHeight="51.0" prefWidth="131.0" text="Login" textAlignment="CENTER" textFill="#328d38">
<font>
<Font name="Courier New Bold" size="22.0" />
</font>
</Button>
<Label fx:id="error_lab" layoutX="59.0" layoutY="284.0" prefHeight="29.0" prefWidth="229.0" textFill="#ee0c0c">
<font>
<Font size="18.0" />
</font>
</Label>
<Button layoutX="110.0" layoutY="284.0" mnemonicParsing="false" onAction="#signupButton" prefHeight="29.0" prefWidth="85.0" text="Sign up" textAlignment="CENTER" textFill="#3b21e4">
<font>
<Font name="Courier New Bold" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
Profile.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?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.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="420.0" prefWidth="560.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginApplication.ProfileController">
<children>
<Button fx:id="edit" layoutX="202.0" layoutY="363.0" mnemonicParsing="false" onAction="#editButton" prefHeight="26.0" prefWidth="111.0" text="Edit">
<font>
<Font size="14.0" />
</font>
</Button>
<TextField fx:id="name" layoutX="202.0" layoutY="28.0" prefHeight="25.0" prefWidth="329.0" promptText="Name">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="email" layoutX="202.0" layoutY="242.0" prefHeight="25.0" prefWidth="329.0" promptText="Email">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="gender" layoutX="202.0" layoutY="132.0" prefHeight="25.0" prefWidth="329.0" promptText="Gender">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="contact" layoutX="202.0" layoutY="187.0" prefHeight="25.0" prefWidth="329.0" promptText="Contact">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="age" layoutX="202.0" layoutY="81.0" prefHeight="25.0" prefWidth="329.0" promptText="Age">
<font>
<Font size="18.0" />
</font>
</TextField>
<Button fx:id="save" layoutX="420.0" layoutY="363.0" mnemonicParsing="false" onAction="#saveButton" prefHeight="26.0" prefWidth="111.0" text="Save">
<font>
<Font size="14.0" />
</font>
</Button>
<PasswordField fx:id="password" layoutX="202.0" layoutY="297.0" prefHeight="39.0" prefWidth="329.0" promptText="Password">
<font>
<Font size="18.0" />
</font>
</PasswordField>
<Label layoutX="35.0" layoutY="34.0" text="Name">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="88.0" text="Age">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="139.0" text="Gender">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="194.0" text="Contact">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="249.0" text="Email">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="304.0" text="Password">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
</children>
</AnchorPane>
I hope this help if something wont work or u need to know something ask it :)
I suggest you use an SQLite database and Not textfile.
But using a Text file to make a small database it's a good practice.
To do this you need to create a .txt file that's your database
In a text file, you can store information many ways for example
Name, Email, Password, Age, Gender
Jack, jack#gmail.com, mypassword123, 18, male,
Adam, adam101#gmail.com, adampas1001, 21, female,
in this way you separating data with commas
Now you have to read each line 1 by 1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
public class TextFileReader
{
// Location / count / List
private String text_File_Location; //location
private long count;
public TextFileReader(String url)
{
this.text_File_Location = url;
lineList = new ArrayList<String>();
countLines();
initFontCodeList();
}
//Counts number of lines mean number of profiles
private void countLines()
{
try {
count = Files.lines(Paths.get(text_File_Location)).count();
} catch (IOException e1) { System.err.println("TextFileReader : Count Failed" +e1); }
}
//init List
private void initFontCodeList() {
try
{
Path path = Paths.get(text_File_Location);
for (int i = 0; i < count; i++)
{
String line = Files.readAllLines(path).get(i);
//Acording to format Name, Email, Password, Age, Gender
String[] profile = line.split(",");
String name = profile[0];
String email = profile[1];
String Password = profile[2];
String age = profile[4];
String gender = profile[5];
}
}
catch (IOException e) {System.err.println("TextFileReader : ReadFile Failed");}
}
}
Now just use a couple of logic to check if an information entered on GUI is equal textfile information
Removing profile is just deleting that line
Modifying profile is just deleting that line and creating another line
This is all the basic how to start

JavaFX TableView, Labels and Buttons selection is not working and looks and the table looks disabled

This is my first question in this great community. I started a Java FX tutorial on this blog http://code.makery.ch/library/javafx-8-tutorial/ . But I am using IntelliJ IDEA instead of Eclipse. The program compiles successfully but both the TableView in the left and the labels in the right look like they are disabled ( the font color is gray and not black and the table can not be highlighted on mouse hover or click ). So my question is : Why can not I select the Table view ?
TableView, Labels and Buttons looks disabled
package com.melkojji.controller;
import com.melkojji.model.Person;
import com.melkojji.view.PersonOverviewController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Person> personData = FXCollections.observableArrayList();
public Main() {
this.personData.add(new Person("Mustapha", "HUSAIN"));
this.personData.add(new Person("Mustapha", "EL KOJJI"));
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AdressApp");
this.primaryStage.setMinWidth(615);
this.primaryStage.setMinHeight(365);
initRootLayout();
showPersonOverview();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
} catch (IOException e) {
e.printStackTrace();
}
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
// Give the controller access to the main app.
PersonOverviewController controller = loader.getController();
controller.setMain(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public ObservableList<Person> getPersonData() {
return personData;
}
}
package com.melkojji.view;
import com.melkojji.controller.Main;
import com.melkojji.model.Person;
import com.melkojji.util.DateUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
/**
* Created by melkojji on 1/14/2017.
*/
public class PersonOverviewController {
#FXML
private TableView<Person> personTableView;
#FXML
private TableColumn<Person, String> personFirstNameTableColumn;
#FXML
private TableColumn<Person, String> personLastNameTableColumn;
#FXML
private Label firstNameLabel;
#FXML
private Label lastNameLabel;
#FXML
private Label streetLabel;
#FXML
private Label postalCodeLabel;
#FXML
private Label cityLabel;
#FXML
private Label birthdayLabel;
// Reference to the main application.
private Main main;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public PersonOverviewController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
#FXML
public void initialize() {
// Initialize the person table with the two columns.
this.personFirstNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
this.personLastNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// Clear person details.
showPersonDetails(null);
// Listen for selection changes and show the person details when changed.
personTableView.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> showPersonDetails(newValue)));
}
/**
* Is called by the main application to give a reference back to itself.
*
* #param main
*/
public void setMain(Main main) {
this.main = main;
// Add observable list data to the table.
this.personTableView.setItems(main.getPersonData());
}
/**
* Fills all text fields to show details about the person.
* If the specified person is null, all text fields are cleared.
*
* #param person the person or null
*/
public void showPersonDetails(Person person) {
if (person != null) {
// Fill the labels with info from the person object.
firstNameLabel.setText(person.getFirstName());
lastNameLabel.setText(person.getLastName());
streetLabel.setText(person.getStreet());
postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
cityLabel.setText(person.getCity());
birthdayLabel.setText(DateUtil.format(person.getBirthday()));
// birthdayLabel.setText(...);
} else {
// Person is null, remove all the text.
firstNameLabel.setText("");
lastNameLabel.setText("");
streetLabel.setText("");
postalCodeLabel.setText("");
cityLabel.setText("");
birthdayLabel.setText("");
}
}
}
<?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.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar BorderPane.alignment="CENTER">
<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>
</top>
</BorderPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane disable="true" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.melkojji.view.PersonOverviewController">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fx:id="personTableView" editable="true" onSort="#initialize" prefHeight="298.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="personFirstNameTableColumn" prefWidth="75.0" text="First name" />
<TableColumn fx:id="personLastNameTableColumn" prefWidth="75.0" text="Last name" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Person details :" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
<GridPane AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First name" />
<Label text="Last name" GridPane.rowIndex="1" />
<Label text="Street" GridPane.rowIndex="2" />
<Label text="City" GridPane.rowIndex="3" />
<Label text="Postal code" GridPane.rowIndex="4" />
<Label text="Birthday" GridPane.rowIndex="5" />
<Label fx:id="firstNameLabel" text="Label" GridPane.columnIndex="1" />
<Label fx:id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label fx:id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label fx:id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
<ButtonBar prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<buttons>
<Button mnemonicParsing="false" text="New" />
<Button mnemonicParsing="false" text="Edit" />
<Button mnemonicParsing="false" text="Delete" />
</buttons>
<padding>
<Insets right="5.0" />
</padding>
</ButtonBar>
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
The disable property will disable a node and all child nodes when set. So, because you have
<AnchorPane disable="true" ..>
the anchor pane and all its subnodes, including the table, are disabled.

How to change screens in javafx on detecting radiobutton choice inside a buttonclick listener?

Elaborating my question further, I am developing a question bank in JavaFX. At the home screen, I wish to provide navigation based on radiobutton choice inside a button click.
e.g. If I select a radiobutton choice, and click on the button to proceed ahead, it should direct me to the FXML screen file I have created. To further explain what I am trying to stay, below is the GUI snapshot.
Question Bank GUI Link
I am pasting my code below :
Main HomeScreen FXML File :
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="548.0" prefWidth="721.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="controller.HomefxmlController">
<children>
<GridPane alignment="CENTER" gridLinesVisible="false" layoutX="210.0" layoutY="149.0" prefHeight="171.0" prefWidth="373.0" visible="true">
<children>
<RadioButton fx:id="radioBlanks" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#onRadioBlankClick" text="Fill in the Blank" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="0" GridPane.valignment="CENTER" />
<RadioButton fx:id="radioMcq" mnemonicParsing="false" onAction="#onRadioMcqClick" text="MCQ" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="0" />
<RadioButton fx:id="radioshortNote" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#onRadioSNclick" text="ShortNote" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="1" />
<RadioButton fx:id="radioLongAnswer" mnemonicParsing="false" onAction="#onRadioLNclick" text="LongAnswer" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="1" />
<RadioButton fx:id="radioScenario" mnemonicParsing="false" onAction="#onRadioScenariocClick" text="Scenario" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="2" />
<RadioButton fx:id="radioTF" mnemonicParsing="false" onAction="#onRadioTFclick" text="True/False" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="2" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<Label alignment="CENTER" layoutX="234.0" layoutY="47.0" prefHeight="73.0" prefWidth="211.0" text="Question Bank" textAlignment="CENTER" underline="true" wrapText="false">
<font>
<Font name="Chiller" size="35.0" />
</font>
</Label>
<Button fx:id="btnProceed" layoutX="275.0" layoutY="378.0" mnemonicParsing="false" onAction="#onBtntProceed" prefHeight="48.0" prefWidth="129.0" text="Proceed" textFill="#252285">
<font>
<Font name="Linux Libertine G Regular" size="20.0" />
</font>
</Button>
</children>
<stylesheets>
<URL value="#homefxml.css" />
</stylesheets>
</AnchorPane>
FXML Controller File :
package controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author Vishal
*/
public class HomefxmlController implements Initializable {
#FXML
private RadioButton radioBlanks;
#FXML
private RadioButton radioMcq;
#FXML
private RadioButton radioshortNote;
#FXML
private RadioButton radioLongAnswer;
#FXML
private RadioButton radioScenario;
#FXML
private RadioButton radioTF;
#FXML
private Button btnProceed;
ToggleGroup group;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
toggleGroupAssign();
}
public void toggleGroupAssign() {
group = new ToggleGroup();
radioBlanks.setToggleGroup(group);
radioMcq.setToggleGroup(group);
radioshortNote.setToggleGroup(group);
radioLongAnswer.setToggleGroup(group);
radioScenario.setToggleGroup(group);
radioTF.setToggleGroup(group);
}
#FXML
private void onBtntProceed(ActionEvent event) throws IOException {
// I am selecting just one checkbox at the moment for testing purpose..
if(radioTF.isSelected()){
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/tester.fxml"));
loader.load();
}
}
}
The PROBLEM is that NO ERROR appears at all. No compiletime error, no runtime, nothing ! It just doesn't work !
Kindly help me where exactly I am going wrong ?
loader.load() returns a Node, you don't assign
Take a look at this link
Loading new fxml in the same scene

Resources