JavaFx TableView doesnt show data - java

I know there are many questions about this, but the answers didn't solve my problem.
I have a TableView with 4 columns. The data is read out of a txt file.
In the initialize method from the Controller the CellValueFactory is set, and another method is called, which reads the Data from the file and creates an observableList of the model.
Then the list is added to the items of the table, but no data is displayed, and no placeholder is there. Does anyone know what is wrong?
Controller class
#FXML
private TableView<Data> dataTable;
#FXML
private TableColumn<Data, String> dataType;
#FXML
private TableColumn<Data, String> dataName;
#FXML
private TableColumn<Data, String> dataDate;
#FXML
private TableColumn<Data, String> dataInformation;
#FXML
public void initialize() {
dataType.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataType()));
dataName.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataName()));
dataDate.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataDate()));
dataInformation.setCellValueFactory(value -> new SimpleStringProperty(value.getValue().getDataInformation()));
initData();
}
private void initData() {
ArrayList<Data> data = new ArrayList<>();
try {
Path filePath = Paths.get("src/main/resources/kl/fla/decrypted.txt");
if (filePath.toFile().exists()) {
data = (ArrayList<Data>) Files.readAllLines(filePath, StandardCharsets.UTF_8).stream()
.map(Data::new).collect(Collectors.toList());
}
} catch (Exception e) {
e.printStackTrace();
}
dataTable.setItems(FXCollections.observableArrayList(data));
dataTable.refresh();
}
FXML File
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0"
prefWidth="1080.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="kl.fla.controller.TableController"
>
<children>
<Label alignment="CENTER" layoutX="393.0" layoutY="34.0" prefHeight="70.0" prefWidth="295.0" text="Hidden Gate">
<font>
<Font size="48.0" />
</font>
</Label>
<TableView fx:id="dataTable" fixedCellSize="1.0" layoutX="36.0" layoutY="141.0" prefHeight="439.0" prefWidth="840.0">
<columns>
<TableColumn fx:id="dataType" prefWidth="135.0" resizable="false" text="Datatype" />
<TableColumn fx:id="dataName" minWidth="4.0" prefWidth="135.0" resizable="false" text="Name" />
<TableColumn fx:id="dataDate" prefWidth="135.0" resizable="false" text="Date" />
<TableColumn fx:id="dataInformation" minWidth="0.0" prefWidth="285.0" resizable="false" text="Information" />
</columns>
</TableView>
</children>
</Pane>
Screenshot of the empty TableView

The data are (probably) there; you just can’t see them because you have constrained every row to be just one pixel high:
fixedCellSize="1.0"
Remove that attribute and just use the default setting, and it should work (assuming there are no other errors in code you haven’t posted).

Related

Dynamically added view wont fit Pane

I want to make a application with a menu on the left and the view on the right. I have different fxml file. 1 main, and multiple for the view.
Right now I load different view but the view on right doesn't his parent.
Main view
<GridPane
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.Controller">
<columnConstraints>
<ColumnConstraints percentWidth="30.0" />
<ColumnConstraints percentWidth="70.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<GridPane maxWidth="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="0">
<columnConstraints>
<ColumnConstraints percentWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Button fx:id="devices" maxWidth="Infinity" mnemonicParsing="false" text="Devices" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Button fx:id="inventory" maxWidth="Infinity" mnemonicParsing="false" text="Inventory" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</children>
</GridPane>
<Pane
fx:id="container"
minWidth="Infinity"
maxWidth="Infinity"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.columnIndex="1"
GridPane.columnSpan="1"
GridPane.rowIndex="0">
</Pane>
</children>
</GridPane>
Custom view load in container
<GridPane
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.DevicesController"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS">
<children>
<TableView
GridPane.columnIndex="0"
GridPane.columnSpan="1"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.rowIndex="0">
<columns>
<TableColumn text="Devices" />
<TableColumn text="C2" />
</columns>
</TableView>
</children>
</GridPane>
Current form
Controller
public class Controller implements Initializable {
#FXML private Pane container;
#FXML private Button devices;
#FXML private Button inventory;
private HashMap<Views, Node> views;
private enum Views {
DEVICES,
INVENTORY
}
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
views = new HashMap<>();
views.put(Views.DEVICES, FXMLLoader.load(getClass().getResource("../view/" + "view-devices.fxml")));
views.put(Views.INVENTORY, FXMLLoader.load(getClass().getResource("../view/" + "view-inventory.fxml")));
devices.setOnMouseClicked(v -> load(Views.DEVICES));
inventory.setOnMouseClicked(v -> load(Views.INVENTORY));
} catch (IOException e) {
e.printStackTrace();
}
}
public void load(Views view){
if (views.containsKey(view)) {
Node newView = views.get(view);
if (!container.getChildren().contains(newView)) {
container.getChildren().add(newView);
} else {
newView.resize(container.getMaxWidth(), container.getMaxHeight());
newView.toFront();
}
} else {
System.out.println("Error: key not found in map");
}
}
}
I want to the right view to fit the container.
The Pane you are using for the container for the right side of the display performs no layout, so it cannot make the content fill the available space (no matter what settings you use).
Use a BorderPane for the container instead:
<BorderPane
fx:id="container"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.columnIndex="1"
GridPane.columnSpan="1"
GridPane.rowIndex="0">
</BorderPane>
and then in the controller you can simply set the new content as the center each time:
public class Controller implements Initializable {
#FXML private BorderPane container;
// ...
public void load(Views view){
if (views.containsKey(view)) {
Node newView = views.get(view);
container.setCenter(newView);
} else {
System.out.println("Error: key not found in map");
}
}
}

fxml tableview just showing blank rows

I have a FXML file that looks like this:
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
xmlns:fx="http://javafx.com/fxml"
fx:controller="GUI.Controller">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<HBox spacing="10" alignment="bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="Sign In"
onAction="#handleSubmitButtonAction"/>
</HBox>
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="0">
<columns>
<TableColumn fx:id="nameColumn" text="Event Name">
</TableColumn>
</columns>
</TableView>
and a controller class like this:
public class Controller implements Initializable {
#FXML
private TableView<String> tableView;
#FXML
private TableColumn<String, String> nameColumn;
#FXML
private ObservableList <String> dataArray;
#Override
public void initialize(URL location, ResourceBundle resources) {
dataArray = FXCollections.observableArrayList();
nameColumn.setCellValueFactory(
new PropertyValueFactory<String, String>("nameColumn")
);
dataArray.add("hello");
dataArray.add("goodbye");
tableView.setItems(dataArray);
}
When I run this, I get two columns(not sure why - one is called "Event Name" and the other is blank) and two rows, but the rows are blank. You can click on the rows, but there is no data in them. When I remove the "hello" entry or the "goodbye" entry from the code, the table just displays one blank row instead of two.
I'm new to Java and not exactly sure why this is happening. Why are they rows showing up, but the data isn't displaying?
The best way to use table is use class for representing data in table. In your example I changed String to TableModel where you have one PropertyString name greetings. Now you can easy use this class to show data in tabe.
Controller:
public class Controller implements Initializable {
#FXML private TableView<TableModel> tableView;
#FXML private TableColumn<TableModel, String> nameColumn;
#FXML private ObservableList<TableModel> dataArray;
#Override public void initialize(URL location, ResourceBundle resources) {
//add data to Observable List
dataArray = FXCollections.observableArrayList(new TableModel("hello"), new TableModel("goodbye"));
//crete one column, name "greetings" is the same name property which is in TableModel
nameColumn.setCellValueFactory(new PropertyValueFactory<TableModel, String>("greetings"));
tableView.setItems(dataArray);
}
public static class TableModel {
StringProperty greetings = new SimpleStringProperty();
public TableModel(String greetings) {
this.greetings = new SimpleStringProperty(greetings);
}
public String getGreetings() {
return greetings.get();
}
public StringProperty greetingsProperty() {
return greetings;
}
public void setGreetings(String greetings) {
this.greetings.set(greetings);
}
}
}
You must add this code to resolve problem with two columns.
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
Here is your FXML with small changes.
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml" fx:controller="GUI.Controller">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="0">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
<columns>
<TableColumn fx:id="nameColumn" text="Event Name"/>
</columns>
</TableView>
<HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="1" GridPane.rowIndex="1">
<Button text="Sign In" onAction="#handleSubmitButtonAction"/>
</HBox>
</GridPane>

JavaFX TableView pagination - can not create fxml based solution

I am using JavaFX in my project (also Spring, Hibernate e.t.c.).
I am trying to create Pagination for TableView. I found many reasonable solutions, but all off them are controller-based solutions. The best of them in this solution.
But in my situation - I am using multiple fxml files which were created in SceneBuilder with all huge design options and which contains the TabPane and for one Tab I have one FXML-file and one controller for it.
I have one basic file which imports others, and in my Main class I am loading it:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="592.0" prefWidth="920.0" xmlns="http://javafx.com/javafx/8"
fx:controller="com.varinsia.statistical.controller.GenericController">
<children>
<TabPane layoutX="3.0" layoutY="50.0" prefHeight="542.0" prefWidth="914.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Statistics">
<content>
<fx:include fx:id="statistics" source="/fxml/child/statistics.fxml"/>
</content>
</Tab>
<Tab text="Shedule">
<content>
<fx:include fx:id="shedule" source="/fxml/child/shedule.fxml"/>
</content>
</Tab>
<Tab text="Ponab devices">
<content>
<fx:include fx:id="ponabDevices" source="/fxml/child/ponabDevices.fxml"/>
</content>
</Tab>
<Tab text="Als devices">
<content>
<fx:include fx:id="alsDevices" source="/fxml/child/alsDevices.fxml"/>
</content>
</Tab>
<Tab text="Search">
<content>
<fx:include fx:id="search" source="/fxml/child/search.fxml"/>
</content>
</Tab>
<Tab text="Settings">
<content>
<fx:include fx:id="settings" source="/fxml/child/settings.fxml"/>
</content>
</Tab>
</tabs>
</TabPane>
<Label layoutX="127.0" layoutY="9.0" text="..."
underline="true">
<font>
<Font size="18.0"/>
</font>
</Label>
</children>
</AnchorPane>
In my controller class for my statistics.fxml I am creating the TableView and columns:
#FXML
public TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> objectColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> noteColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> stageColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> dateColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> vagonColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> repeatColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, Integer> remarkIdColumn;
And all other things like in this solution.
Information from the entity is added to my table and everything works fine. But!
Problem starts when i am trying to add Pagination, because I can't understand how I should do this. My Main method looks like this:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring/application-context.xml");
context.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);
primaryStage.setTitle(Constants.MAIN_TITLE);
primaryStage.setScene(new Scene((Parent) context.getBean(SpringFXMLLoader.class).load(Constants.FXML_PATH), 914, 542));
primaryStage.show();
}
}
In all TableView-pagination examples Pagination were added in the Main class, like in the code from other valid and checked example:
private Node createPage(int pageIndex) {
int fromIndex = pageIndex * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
return new BorderPane(table);
}
#Override
public void start(final Stage stage) throws Exception {
Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
pagination.setPageFactory(this::createPage);
Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
stage.setScene(scene);
stage.setTitle("Table pager");
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
Is there some way to pass this issue and to add Pagination to my TableView?
I'll be very glad to any answers on this subject, the problem does not suffer the first day.
RESOLVED!:
Thanks to James_D and his solution.
I have tried this variant and it worked fine. My TableView is now with pagination.
I have added to my statistical.fxml:
<Pagination fx:id="statisticsTableViewPagination" layoutX="2.0" layoutY="188.0" prefHeight="275.0"
prefWidth="912.0">
<fx:define>
<TableView fx:id="statisticsTableView" layoutX="2.0" layoutY="188.0" prefHeight="301.0" prefWidth="912.0">
<placeholder>
<Label text="No search results found."/>
</placeholder>
<columns>
<TableColumn fx:id="objectColumn" prefWidth="131.0" text="Object"/>
<TableColumn fx:id="noteColumn" minWidth="0.0" prefWidth="167.0" text="Remark"/>
<TableColumn fx:id="stageColumn" prefWidth="282.0" text="Stage"/>
<TableColumn fx:id="dateColumn" prefWidth="72.0" text="Date"/>
<TableColumn fx:id="vagonColumn" prefWidth="133.0" text="Laboratory"/>
<TableColumn fx:id="repeatColumn" prefWidth="125.0" text="Repeats"/>
<TableColumn fx:id="remarkIdColumn" minWidth="9.0" prefWidth="15.0" text="Id" visible="false"/>
</columns>
</TableView>
</fx:define>
</Pagination>
And in my controller I have added the same parts as in this example. And everything is working fine. Thanks a lot!
Just define the Pagination in statistics.fxml:
<Pagination fx:id="pagination"/>
and then configure it in the controller for that class:
#FXML
private TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
private Pagination pagination ;
public void initialize() {
pagination.setPageFactory(this::createPage);
// etc...
}
Since the table itself is placed in the scene graph by the page factory, it isn't part of the scene graph until after the initialize() method is invoked, i.e. after the FXML has been parsed. So you can either just define the table in the controller directly (and not include it in the FXML file at all):
private TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
private Pagination pagination ;
public void initialize() {
statisticsTableView = new TableView<>();
// create columns, etc...
pagination.setPageFactory(this::createPage);
// etc...
}
or you can define it in FXML in a <fx:define> block:
<Pagination fx:id="pagination">
<fx:define>
<TableView fx:id="statisticsTableView">
<columns>
<TableColumn fx:id="objectColumn" ... />
<!-- etc -->
</columns>
</TableView>
</fx:define>
</Pagination>
and then inject it into the controller with #FXML in the usual way.

Populating TableView in JavaFX From Data in SQLite

I am trying to populate a TableView from data in SQLite database but I am experiencing a very weird scenario that I cannot understand what is causing it.
The tableview only populates two columns and does not populate the rest. The Tablecolumns with 'NO' and 'Date Created' do not get populated when the TableView is finally displayed.
This code however displays data from SQLite database in 'Title' and 'Description' TableView columns.
Please someone with a hawk eye help me identify where I am going wrong on this code. I have spent the better part of the day trying to figure out where I am going wrong but I do not seem to figure out what it is that I am not doing it right. I will gladly appreciate any help on this.
Here is my code for
Main class
Blockquote
public class Notedb extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("ListNotesUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Blockquote
FXML
Blockquote
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="notedb.test.ListNotesUIController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.5" layoutX="186.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="196.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Button alignment="TOP_CENTER" contentDisplay="TEXT_ONLY" layoutX="484.0" layoutY="22.0" mnemonicParsing="false" onAction="#newNote" prefHeight="54.0" prefWidth="66.0" text="New Note" textAlignment="CENTER" wrapText="true" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="126.0" layoutY="2.0" prefHeight="94.0" prefWidth="596.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="441.0" minWidth="10.0" prefWidth="441.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="292.0" minWidth="10.0" prefWidth="155.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="m_search" onAction="#searchNotes" />
<Label fx:id="labelNOs" alignment="CENTER" prefHeight="17.0" prefWidth="94.0" text="4 Notes" GridPane.columnIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="181.0" layoutY="98.0" prefHeight="196.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<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>
<children>
<Pane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<Button layoutX="95.0" layoutY="24.0" mnemonicParsing="false" prefHeight="54.0" prefWidth="100.0" text="Delete" />
<Button fx:id="btn_medit" layoutX="389.0" layoutY="24.0" mnemonicParsing="false" onAction="#editNoteRow" prefHeight="54.0" prefWidth="94.0" text="Edit" />
</children>
</Pane>
<TableView id="tableNotes" fx:id="tableNotes" editable="true" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn id="noCol" fx:id="noCol" text="NO">
</TableColumn>
<TableColumn id="titleCol" fx:id="titleCol" text="Title">
</TableColumn>
<TableColumn id="dateCreatedCol" fx:id="dateCreatedCol" text="Date Created">
</TableColumn>
<TableColumn id="descriptionCol" fx:id="descriptionCol" text="Description">
</TableColumn>
</columns>
</TableView>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
Blockquote
Controller class
Blockquote
public class ListNotesUIController implements Initializable {
#FXML
private Label label;
#FXML
private Label labelNOs;
#FXML
private Button newNote;
#FXML
private Button btn_medit;
#FXML
private TextField m_search;
#FXML
private TableView tableNotes;
#FXML
private TableColumn titleCol;
#FXML
private TableColumn descriptionCol;
#FXML
private TableColumn dateCreatedCol;
#FXML
private TableColumn noCol;
//START | SQLITE
private static Connection con;
private static Statement stat;
private PreparedStatement prep;
//END | SQLITE
private ObservableList <Note> dataNotes;
DataBank dbank = new DataBank();
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#FXML
private void editNoteRow(ActionEvent event) {
}
#FXML
private void newNote(ActionEvent event) throws IOException {
}
#FXML
private void searchNotes(ActionEvent event){
}
#Override
public void initialize(URL url, ResourceBundle rb) {
dataNotes = FXCollections.observableArrayList();
noCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("idno")
);
dateCreatedCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("datecreated")
);
titleCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("title")
);
descriptionCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("description")
);
try {
SQLiteConfig config = new SQLiteConfig();
con = DriverManager.getConnection("jdbc:sqlite:Note.db");
stat = con.createStatement();
stat.executeUpdate("CREATE TABLE IF NOT EXISTS NotesDB (idno INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Title VARCHAR(500), Description VARCHAR(1000), DateCreated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);");
ResultSet rs = con.createStatement().executeQuery("SELECT idno, Title, DateCreated, Description FROM NotesDB");
while (rs.next()) {
Note nt = new Note();
nt.idno.set(rs.getString("idno"));
nt.title.set(rs.getString("Title"));
nt.datecreated.set(rs.getString("DateCreated"));
nt.description.set(rs.getString("Description"));
dataNotes.add(nt);
}
tableNotes.setItems(dataNotes);
} catch (SQLException ex) {
Logger.getLogger(ListNotesUIController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Blockquote
DataModel class
Blockquote
public class Note {
public SimpleStringProperty title = new SimpleStringProperty();
public SimpleStringProperty description = new SimpleStringProperty();
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
public String getTitle() {
return title.get();
}
public void setTitle(String titleStr) {
title.set(titleStr);
}
public String getDescription() {
return description.get();
}
public void setDescription(String descriptionStr) {
description.set(descriptionStr);
}
public String getDateCreated() {
return datecreated.get();
}
public void setDateCreated(String datecreatedStr) {
datecreated.set(datecreatedStr);
}
public String getIdNO() {
return idno.get();
}
public void setIdNO(String idnoStr) {
idno.set(idnoStr);
}
}
Blockquote
There is a mistake regarding your property naming.
Your function for getDateCreated and IdNO don't correlate with
the naming convention.
Replace
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
with
public SimpleStringProperty dateCreated = new SimpleStringProperty();
public SimpleStringProperty idNO = new SimpleStringProperty();
And have a look at the naming conventions for properties

passing parametres to FXML file from the controller

Hello I'm new to javaFX, and trying to create a simulation application of the scheduling algorithms.
I did the logic package, but still have problems with the UI. what i want to do is to allow the user to enter the number of jobs and in the next window i want to display text fields where he can put the executing time of each job, in other words i should display the text fields n time as the number of jobs given by the user.
this is what i did for the first interface :
<?xml version="1.0" encoding="UTF-8"?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" alignment="center" hgap="10" styleClass="mainFxmlClass" vgap="10" fx:controller="ui.FXMLController">
<padding><Insets bottom="10" left="25" right="25" top="25" /></padding>
<stylesheets>
<URL value="#fxml.css" />
</stylesheets>
<children>
<Text id="welcome-text" text="Welcome" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="0" />
<Label text="Le nombre de Processus:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="textField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Choisissez un Algorithm d'Ordonancement :" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<HBox alignment="bottom_right" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text fx:id="actiontarget" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<Button onAction="#handleButtonAction" text="Next" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<MenuButton mnemonicParsing="false" prefHeight="25.0" prefWidth="150.0" text="Algo ... " GridPane.columnIndex="1" GridPane.rowIndex="2">
<items>
<MenuItem mnemonicParsing="true" text="Fifo" onAction="#handleButtonAction" />
<MenuItem mnemonicParsing="false" text="Tourniquet" />
<MenuItem mnemonicParsing="false" text="PCTER" />
<MenuItem mnemonicParsing="false" text="PCTE" />
</items>
</MenuButton>
</children>
</GridPane>
this is my controller :
public class FXMLController implements Initializable {
#FXML
private Label label;
#FXML
private Button button;
#FXML
private TextField textField;
#FXML
private Integer i;
#FXML
public void handleButtonAction() throws IOException{
String t = textField.getText();
IntegerStringConverter a = new IntegerStringConverter();
this.i = a.fromString(t);
Parent window = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
Scene secondScene = new Scene(window);
Stage stage = new Stage();
stage.setScene(secondScene);
stage.show();
stage.show();
}
}
i dont know if there is a way to pass parameters from the controller to the FXML file and to do a loop in the FXML.. please help
If I understand you correctly, you want to request user input and create nodes in the GUI according to the input.
You can't create loops respectively control structures in a fxml file, but you can dynamically add and remove nodes (i.e. TextFields) by
defining a Vertical Box / Horizontal Box,
<VBox id="HBox" fx:id="myVBox" ...>
get the input as an integer,
int sum = Integer.parseInt(textField.getText());
and add the nodes in a for-loop,
for(int i=0, i<sum, i++) {
myVBox.getChildren().add(new TextField("executing time"));
}
and update the view.
Hope it helps.
AFAIK, you can't do that. You have to reverse the controL flow:
The controller should create the necessary controls (labels, textfields etc.) in a loop, possibly using a different FXML, which just defines that part.

Categories