So I was just trying to add a checkbox to my programm. Only problem is, I don't know how to do that using FXML and normal javaFX. Can you guys help me?
heres my Main class
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/sample/sample.fxml"));
AnchorPane rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
//rootLayout.prefWidthProperty().bind(scene.widthProperty()); not needed
//rootLayout.prefHeightProperty().bind(scene.heightProperty());in these scenario
primaryStage.setScene(scene);
primaryStage.getIcons().add(new Image("/sample/img/Java.png"));
primaryStage.setTitle("Tool-Downloader");
primaryStage.show();
primaryStage.setResizable(false);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
My Controller:
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import static java.util.ServiceLoader.load;
public class Controller {
#FXML
private void downloadButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("grievous.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
stage.setResizable(false);
} catch(Exception e) {
e.printStackTrace();
}
}
#FXML
private void close(ActionEvent onClick){
((Stage)(((javafx.scene.control.Button)onClick.getSource()).getScene().getWindow())).close();
}
#FXML
public void selection(){
}
}
My FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.CheckBox?>
<?import sample.Programs?>
<?import sample.Selection?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.image.Image?>
<AnchorPane maxHeight="-Infinity" maxWidth="1000" minHeight="650" minWidth="900" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" style="-fx-background-color: white" fx:controller="sample.Controller"
stylesheets="/sample/style1.css">
<HBox alignment="CENTER_LEFT" layoutX="6.0" layoutY="14.0" spacing="52.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" styleClass="background">
<padding>
<Insets left="10.0" right="3.0" top="5.0"/>
</padding>
<ComboBox fx:id="versionCombo" prefWidth="150.0" promptText="Choose OS" styleClass="combox2">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Windows"/>
<String fx:value="Mac OS X"/>
<String fx:value="Ubuntu"/>
</FXCollections>
</items>
</ComboBox>
<Button onAction="#downloadButton" minWidth="80.0" mnemonicParsing="false" text="Download" styleClass="button1"/>
<HBox>
<padding>
<Insets left="550.0" right="0.0" top="0.0"/>
</padding>
<Button onAction="#close" minWidth="80.0" mnemonicParsing="false" text="close" styleClass="close"/>
</HBox>
</HBox>
<TableView fx:id="tableView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="40.0" styleClass="table-view">
<columns>
<TableColumn fx:id="selection" prefWidth="100.0" maxWidth="100.0" minWidth="100" text="Selection" styleClass="table1" >
<cellValueFactory>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="date" prefWidth="652" maxWidth="652" minWidth="652" text="Program" styleClass="table2">
<cellValueFactory>
<PropertyValueFactory property="programs"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="release" prefWidth="150" maxWidth="150" minWidth="150" text="Date" styleClass="table3">
<cellValueFactory>
<PropertyValueFactory property="date"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="opsystem" prefWidth="100" minWidth="100" maxWidth="100" text="OS" styleClass="table4">
<cellValueFactory>
<PropertyValueFactory property="operatingSystem"/>
<PropertyValueFactory property="java"/>
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Programs programs="Google Chrome is one of the most popular webbrowsers to date. It has many addons and features." date="06.07.2017" operatingSystem="Windows"/>
</FXCollections>
</items>
</TableView>
</AnchorPane>
And Programs Class:ยจ
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Programs {
private final SimpleStringProperty firstName = new SimpleStringProperty("");
private final SimpleStringProperty lastName = new SimpleStringProperty("");
private final SimpleStringProperty email = new SimpleStringProperty("");
public Programs() {
this("", "", "");
}
public Programs(String firstName, String lastName, String email) {
setPrograms(firstName);
setDate(lastName);
setOperatingSystem(email);
}
public String getPrograms() {
return firstName.get();
}
public void setPrograms(String fName) {
firstName.set(fName);
}
public String getDate() {
return lastName.get();
}
public void setDate(String fName) {
lastName.set(fName);
}
public String getOperatingSystem(){
return email.get();
}
public void setOperatingSystem(String fName){
email.set(fName);
}
}
I have already seaerched for quite some time for this and couldn't find anything.
Related
I'm new in Java. I am writing a JavaFXML application in Netbeans IDE 8.2 that includes a class for connecting to a Derby database (DatabaseHandler.java) that first create a database for me. But when i run the program, this database is not created in my project. Does any code is required to added or modified? Here is my codes. Please help me.
my program has 2 packages: -library.assistant.database -library.assistant.ui.addbook
Codes for FXMLDocumentCntroller.java
package library.assistant.ui.addbook;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import library.assistant.database.DatabaseHandler;
/**
* FXML Controller class
*
* #author pc
*/
public class FXMLDocumentController implements Initializable {
#FXML
private JFXTextField title;
#FXML
private JFXTextField id;
#FXML
private JFXTextField author;
#FXML
private JFXTextField publisher;
#FXML
private JFXButton saveButton;
#FXML
private JFXButton cancelButton;
DatabaseHandler databaseHandler;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
databaseHandler = new DatabaseHandler();
}
#FXML
private void addBook(ActionEvent event) {
}
#FXML
private void cancel(ActionEvent event) {
}
}
Codes for DatabaseHandler.java
package library.assistant.database;
private static DatabaseHandler handler;
private static final String DB_URL = "jdbc:derby:database;create=true";
private static Connection conn = null;
private static Statement stmt = null;
public DatabaseHandler() {
createConnection();
setupBookTable();
}
void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
conn = DriverManager.getConnection(DB_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
void setupBookTable() {
String Table_Name = "Book";
try {
stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet tables = dbm.getTables(null, null, Table_Name.toUpperCase(), null);
if (tables.next()) {
System.out.println("table" + Table_Name + "already exists. Ready for go!");
} else {
stmt.execute("CREATE TABLE " + Table_Name + "("
+ " id varchar(200) primary key,\n"
+ " title varchar(200),\n"
+ " author varchar(200),\n"
+ " publisher varchar(100),\n"
+ " isAvailable boolaen default true"
+ " )");
}
} catch (SQLException e) {
System.err.println(e.getMessage() + " ... setupDatabase");
} finally {
}
}
}
Codes for LibraryAssistant.java
package library.assistant.ui.addbook;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author pc
*/
public class LibraryAssistant extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Here is my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="255.0" prefWidth="368.0"
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="library.assistant.ui.addbook.FXMLDocumentController">
<children>
<VBox prefHeight="363.0" prefWidth="368.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<children>
<JFXTextField fx:id="title" labelFloat="true" promptText="Book Title">
<VBox.margin>
<Insets left="10.0" right="10.0" top="20.0" />
</VBox.margin>
</JFXTextField>
<JFXTextField fx:id="id" labelFloat="true" layoutX="20.0"
layoutY="30.0" promptText="Book ID">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
</VBox.margin>
</JFXTextField>
<JFXTextField fx:id="author" labelFloat="true" layoutX="20.0"
layoutY="30.0" promptText="Book Author">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</JFXTextField>
<JFXTextField fx:id="publisher" labelFloat="true" layoutX="10.0"
layoutY="55.0" promptText="Publisher">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</JFXTextField>
<HBox prefHeight="58.0" prefWidth="368.0">
<children>
<JFXButton fx:id="saveButton" onAction="#addBook"
prefHeight="58.0" prefWidth="200.0" stylesheets="#addbook.css" text="Save"
/>
<JFXButton fx:id="cancelButton" layoutX="10.0" layoutY="10.0"
onAction="#cancel" prefHeight="58.0" prefWidth="200.0"
stylesheets="#addbook.css" text="Cancel" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
I have a JavaFx project with two stages. The main stage has the ability to launch second stage .In second stage I have a textfield I want to put some data into textfield and send it to the main stage and set it as a label .Here is my code:
MAIN:
package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MAIN CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
private javafx.scene.control.Button kbutton;
#FXML
private javafx.scene.control.Button obutton;
#FXML
public Label label;
public TextField popField;
#FXML
public String text;
#FXML
private void konfButton() throws Exception{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/konfview.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
}
public void myFunction( String text){
popField.setText(text);
}
#FXML
private void oblButton() throws Exception{
try {
// get a handle to the stage
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
Controller Controller=fxmlLoader.getController();
Stage stage = (Stage) obutton.getScene().getWindow();
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
MAIN FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="400.0" prefWidth="600.0">
<children>
<Label layoutX="69.0" layoutY="81.0" text="previous reading" />
<Label layoutX="69.0" layoutY="115.0" text="current reading" />
<Label layoutX="71.0" layoutY="154.0" text="months" />
<Label layoutX="108.0" layoutY="14.0" text="Price">
<font>
<Font name="Comic Sans MS" size="18.0" />
</font>
</Label>
<Label layoutX="97.0" layoutY="51.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Label>
<Button fx:id="kbutton" layoutX="74.0" layoutY="197.0" mnemonicParsing="false" onAction="#konfButton" text="Configure" />
<Button fx:id="obutton" layoutX="197.0" layoutY="197.0" mnemonicParsing="false" onAction="#oblButton" text="OK" />
<TextField fx:id="popField" layoutX="172.0" layoutY="77.0" />
<TextField fx:id="bieField" layoutX="172.0" layoutY="111.0" />
<TextField fx:id="mcField" layoutX="172.0" layoutY="150.0" />
<Label fx:id="label" layoutX="119.0" layoutY="245.0" prefHeight="37.0" prefWidth="333.0" text="text" />
</children>
</Pane>
</children>
</GridPane>
SECOND CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class ControllerConf {
#FXML
public TextField palField;
public Label label;
public String text;
#FXML
private TextField dysField1;
#FXML
private javafx.scene.control.Button cancelB;
#FXML
public javafx.scene.control.Button saveB;
#FXML
private void cancelButton(){
// get a handle to the stage
Stage stage = (Stage) cancelB.getScene().getWindow();
// do what you have to do
stage.close();
}
#FXML
public Stage saveButton(){
try {
FXMLLoader loader=new
FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) loader.load();
Controller Controller=loader.getController();
Controller.myFunction(palField.getText());
Stage primaryStage = (Stage) saveB.getScene().getWindow();
primaryStage.close();
return primaryStage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SECOND FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerConf">
<children>
<Label layoutX="185.0" layoutY="28.0" prefHeight="17.0" prefWidth="314.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="20.0" />
</font>
</Label>
<Label layoutX="48.0" layoutY="95.0" text="Price pre KWh" />
<Label layoutX="48.0" layoutY="135.0" text="Price pre item" />
<TextField fx:id="palField" layoutX="252.0" layoutY="91.0" onAction="#saveButton" />
<TextField fx:id="dysField" layoutX="252.0" layoutY="131.0" />
<Button fx:id="cancelB" layoutX="159.0" layoutY="208.0" mnemonicParsing="false" onAction="#cancelButton" text="Cancel">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
<Button fx:id="saveB" layoutX="292.0" layoutY="209.0" mnemonicParsing="false" onAction="#saveButton" text="Save">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
I created MyFunction wchich set text and it's working fine when created new stage but doesnt for main stage.Do you know how to solve it?Thanks in advance.enter code here
I want my inventory_layout to load when i click on the first button but i am getting load exception there while the same code works fine with other fxmls.
Path of controller is right.
fxml is in the right place.
Here is my code- Inventory
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pharmbooks.Utils.DbSingleton;
import pharmbooks.Utils.UIController;
import java.io.IOException;
public class MainController {
#FXML
private BorderPane borderPane;
#FXML
public void initInventory(ActionEvent actionEvent) throws IOException{
UIController<InventoryController> uiController=new UIController<InventoryController>();
uiController.setUp(borderPane,"inventory_layout.fxml");
}
public void initPurchase(ActionEvent actionEvent) throws IOException{
UIController<PurchaseController> uiController=new UIController<>();
uiController.setUp(borderPane,"purchase_layout.fxml");
}
}
Ui Controller
public class UIController<T> {
private T controller;
private Pane pane;
public T getController() {
return controller;
}
public void setUp(Pane mainPane,String fxmlLocation){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource(fxmlLocation));
pane = loader.load();
controller = loader.getController();
mainPane.getChildren().clear();
mainPane.getChildren().add(pane);
}catch (Exception e){
System.out.println(UIController.class.getSimpleName()+" "+e.toString());
}
} }
This code works well in ubuntu though :/
here is the error :
UIController javafx.fxml.LoadException:
/Users/manyamadan/Desktop/Everything/pharmbooks-v2/target/classes/inventory_layout.fxml
Inventory controller :
public class InventoryController implements Initializable{
public TableColumn batchId;
public TableColumn company;
public TableColumn name;
public TableColumn packing;
public TableColumn quantity;
public TableColumn expiry;
public TableColumn mrp;
#FXML
private TableView<Inventory> personTable;
public void InventoryController()
{
}
public void initialize(URL location, ResourceBundle resources) {
batchId.setCellValueFactory(new PropertyValueFactory<Inventory, String>("BATCH_ID"));
company.setCellValueFactory(new PropertyValueFactory<Inventory, String>("COMPANY"));
name.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PRODUCT_NAME"));
packing.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PACKING"));
quantity.setCellValueFactory(new PropertyValueFactory<Inventory, Integer>("QUANTITY"));
expiry.setCellValueFactory(new PropertyValueFactory<Inventory, String>("EXPIRY_DATE"));
mrp.setCellValueFactory(new PropertyValueFactory<Inventory, String>("MRP"));
personTable.setEditable(true);
personTable.setItems(InventoryData.getInstance().getList());
// name.setOnEditCommit(TableColumn.CellEditEvent(Inventory,String)event);
Inventory inventory_data = personTable.getSelectionModel().getSelectedItem();
System.out.println(inventory_data.getPRODUCT_NAME()+ "" + inventory_data.getBATCH_ID());
personTable.setOnKeyPressed(event -> {
TablePosition<Inventory, ?> pos = personTable.getFocusModel().getFocusedCell() ;
if (pos != null) {
AnchorPane anchorPane= null;
try {
anchorPane = FXMLLoader.load(getClass().getClassLoader().getResource("edit_inventory_data.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Stage stage=new Stage();
stage.setTitle("Apna Dialog");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(StageManager.getInstance().getPrimaryStage());
Scene scene = new Scene(anchorPane);
stage.setScene(scene);
stage.showAndWait();
}
});
name.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Inventory, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<Inventory, String> t) {
System.out.println("ON edit commit" + t);
((Inventory) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setPRODUCT_NAME(t.getNewValue());
}
}
);
}
#FXML
public void addData(ActionEvent actionEvent) throws IOException {
AnchorPane anchorPane= FXMLLoader.load(getClass().getClassLoader().getResource("add_data_inventory.fxml"));
Stage stage=new Stage();
stage.setTitle("Apna Dialog");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(StageManager.getInstance().getPrimaryStage());
Scene scene = new Scene(anchorPane);
stage.setScene(scene);
stage.showAndWait();
personTable.setItems(InventoryData.getInstance().getList());
}
}
inventory_layout.fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane maxHeight="600.0" maxWidth="800.0" prefHeight="600.0" prefWidth="800.0" stylesheets="#beautify_inventory.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pharmbooks.controllers.InventoryController">
<center>
<TableView fx:id="personTable" prefWidth="10000.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" />
<TableColumn fx:id="packing" prefWidth="75.0" text="packing" />
<TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" />
<TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" />
<TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" />
</columns>
</TableView>
</center>
<top>
<HBox prefHeight="67.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<TextField prefHeight="47.0" prefWidth="163.00009155273438">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
</TextField>
<Button mnemonicParsing="false" onAction="#addData" text="Button">
<HBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</HBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="121.0" text="Button">
<HBox.margin>
<Insets bottom="20.0" right="20.0" top="20.0" />
</HBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="129.0" text="Button">
<HBox.margin>
<Insets bottom="20.0" top="20.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</top>
</BorderPane>
In inventory_layout.fxml you set fx:id attribute to all table columns:
<TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" />
<TableColumn fx:id="packing" prefWidth="75.0" text="packing" />
<TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" />
<TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" />
<TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" />
but in InventoryController you does not annotate this components with #FXML. Witout this annotations FXMLLoader will not be able to inject correct objects to controller. Correct code:
#FXML
public TableColumn batchId;
#FXML
public TableColumn company;
#FXML
public TableColumn name;
#FXML
public TableColumn packing;
#FXML
public TableColumn quantity;
#FXML
public TableColumn expiry;
#FXML
public TableColumn mrp;
I'm guessing that you get NullPointerException in initalize() method when you try to setCellValueFactory() on table columns.
I assume that you are accessing fxml resource files correctly.
I am trying to get my javafx(using scene builder) to read in a list of employee id and name. Most of it works, but I want the names that have been added to the list to show when I click the show button. Also, I created a tableview in scene builder and I want that window to pop up when the "show" button is clicked. Is the only way to do this with a obeservablelist? How can accomplish this. My code so far is below. I didn't attach the employee class or the primary fxml file, but i Can if it helps.
enter
package queue5230;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
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.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class FXMLDocumentController {
Queue<Employee> empQu = new LinkedList<>();
String name;
String startTime;
String endTime;
int id;
#FXML
private TextField employeeId;
#FXML
private TextField employeeName;
#FXML
private Button insertButton;
#FXML
private Button removeButton;
#FXML
private Button showButton;
#FXML
private TableView<Employee> showTable;
#FXML
private TableColumn<Employee, String> idField;
#FXML
private TableColumn<Employee, String> nameField;
#FXML
private void handleButtonAction(ActionEvent event) {
}
#FXML
private void handleTextInsert(ActionEvent event) throws IOException{
if(insertButton == event.getSource())
{
//get values entered
id = Integer.parseInt(employeeId.getText());
name = employeeName.getText();
//get time entered
Date d = new Date();
//get the date format
startTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(d);
//Create employee object
Employee emp = new Employee(id, name, startTime);
//ad object to queue
empQu.add(emp);
}else if(removeButton == event.getSource()){
Employee empr = empQu.remove();
Date end = new Date();
endTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(end);
String elapse = getElapsedTime(empr.getTime(), endTime);
}else if(showButton == event.getSource()){
Stage stage;
Parent root;
String str = "ID NAME TIME START\n\n";
Iterator<Employee> it = empQu.iterator();
while(it.hasNext()){
//str += it.next() + "\n";
}
stage = new Stage();
root = FXMLLoader.load(getClass().getClassLoader().getResource("Show.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
}
public String getElapsedTime(String starting, String ending)
{
String startToken[] = starting.split(":");
String endToken[] = ending.split(":");
int hours = Math.abs(Integer.parseInt(startToken[0])-Integer.parseInt(endToken[0]));
int minutes = Math.abs(Integer.parseInt(startToken[1])-Integer.parseInt(endToken[1]));
int seconds = Math.abs(Integer.parseInt(startToken[2])-Integer.parseInt(endToken[2]));
return hours + ":" + minutes + ":" + seconds;
}
public void initialize() {
}
}
code here
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
<children>
<Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
<children>
<TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
<columns>
<TableColumn fx:id="idField" prefWidth="75.0" text="ID" />
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" />
</columns>
</TableView>
</children>
</Pane>
</children>
</AnchorPane>
fine in this page https://docs.oracle.com/javafx/2/ui_controls/table-view.htm, there are example of how to show a object in the TableView, but you can define in the fxml:
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="the field name"/>
</cellValueFactory>
</TableColumn>
your fxml code, it will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
<children>
<Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
<children>
<TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
<columns>
<TableColumn fx:id="idField" prefWidth="75.0" text="ID" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="id"/>
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory property="name"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</Pane>
</children>
</AnchorPane>
Can Anybody tell me how to construct HiddenSidesPane in FXML not in the controller?
I am having the basic controller code for this but I am not able to understand how to create fxml structure from that.
Can I have something like this? Below code;
<HiddenSidesPane prefWidth="800.0" pinnedSide="TOP">
<content>
<HBox fillHeight="false" nodeOrientation="RIGHT_TO_LEFT"
prefHeight="27.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="3.0" StackPane.alignment="TOP_RIGHT">
<children>
<Label prefHeight="14.0" prefWidth="94.0" text="Value Date From">
<HBox.margin>
<Insets right="2.0" top="5.0" />
</HBox.margin>
</Label>
</children>
<StackPane.margin>
<Insets top="2.0" />
</StackPane.margin>
</HBox>
</content>
</HiddenSidesPane>
This how I made a fast example with the offical FXSampler of ControlsFX in mind:
Assumptions
You already set up your FXML Project and added the ControlsFX.jar as dependency on your build path.
FXMLDocument.fxml
Watch for the import statements.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import org.controlsfx.control.*?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="200" prefWidth="320" fx:controller="javafxapplication17.FXMLDocumentController">
<children>
<HiddenSidesPane fx:id="pane">
<content>
<Label alignment="CENTER" style="-fx-background-color: white; -fx-border-color: black;" maxHeight="1000.0" maxWidth="1000.0" text="Content Node" />
</content>
<top>
<Label fx:id="pinLabel" style="-fx-background-color: rgba(0,255,0,.25);" text="(Click to pin / unpin)" alignment="CENTER" prefHeight="50.0" prefWidth="50.0" onMouseClicked="#handleMouseClicked" />
</top>
</HiddenSidesPane>
</children>
</StackPane>
FXMLController.java
Inject your variables pane and pinLabel to set them.
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import org.controlsfx.control.HiddenSidesPane;
public class FXMLDocumentController implements Initializable {
#FXML
private HiddenSidesPane pane;
#FXML
private Label pinLabel;
#FXML
private void handleMouseClicked(MouseEvent event) {
if (pane.getPinnedSide() != null) {
pinLabel.setText("(unpinned)");
pane.setPinnedSide(null);
} else {
pinLabel.setText("(pinned)");
pane.setPinnedSide(Side.TOP);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
JavaFXApplication17.java
Sorry for that name :-)
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication17 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
As far as you already know here is the JavaDoc of HiddenSidesPane: http://controlsfx.bitbucket.org/org/controlsfx/control/HiddenSidesPane.html
And if you need an example download the Zip http://fxexperience.com/downloads/controlsfx-8.40.9.zip and unzip it, there is a file controlsfx-samples-8.40.9.jar inside. Double click it and show the sources.