Reputation: 182
I am adding a new row in tableview but it doesn't show any value and i don't know why??
codes:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
public class TestController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private TableView<person> table;
@FXML
void add(ActionEvent event) {
ObservableList<person> data=table .getItems();
data.add(new person("dsdsd", "sdfsdf"));
}
private class person {
private final SimpleStringProperty t1= new SimpleStringProperty("");
private final SimpleStringProperty t2= new SimpleStringProperty("");
public person(String t1,String t2) {
set1Name(t1);
set2Name(t2);
}
public String get1Name() {
return t1.get();
}
public void set1Name(String fName) {
t1.set(fName);
}
public String get2Name() {
return t2.get();
}
public void set2Name(String fName) {
t2.set(fName);
}
}
}
FXML
<?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.scene.control.cell.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="schoolmanagement2.TestController">
<children>
<TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="t1" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="C2">
<cellValueFactory>
<PropertyValueFactory property="t2" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
</children>
</AnchorPane>
and i also not get any error in output window Please help me how would i add a row in table view.
Thank you.
Upvotes: 0
Views: 1863
Reputation: 159566
What's Wrong
Your issue is the thing you are asking to reflect on, e.g. t1
, does not have public access methods in the person
class following Java naming conventions.
So, in this case, following Java style conventions not only makes your program more readable to other developers used to reading Java, but it also makes it work with the expected behavior.
Related Question
This question is really a duplicate of: Javafx tableview not showing data in all columns, however I'll put an additional answer here because it deserves some commentary for defining the PropertyValueFactories in FXML.
Background
A PropertyValueFactory works based on reflecting on your data class using Java camel case naming conventions.
Do read the PropertyValueFactory javadoc I linked, it explains how it works with a sample.
Although numeric characters are probably permitted in camel case, I haven't really seen them used much and I'm not sure how they relate (due to them being neither upper nor lower case). So I'd advise replacing the numeric characters in your identifiers, using the same name for the same data field everywhere it is referenced and making sure the methods required for reflection by PropertyValueFactory are publicly accessible. This will allow your example to work.
Sample Code
Here is a version of the code from your question which works.
All code should be placed in a source package named formatter
.
people.fxml
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="formatter.PeopleTableController">
<children>
<TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="Given Name">
<cellValueFactory>
<PropertyValueFactory property="firstName" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="Surname">
<cellValueFactory>
<PropertyValueFactory property="surname" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
</children>
</AnchorPane>
PeopleApp.java
package formatter;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PeopleApp extends Application {
@Override
public void start(Stage stage) throws Exception{
FXMLLoader loader = new FXMLLoader();
Pane mainPane = loader.load(
getClass().getResourceAsStream(
"people.fxml"
)
);
stage.setScene(
new Scene(
mainPane
)
);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
PeopleTableController.java
package formatter;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
public class PeopleTableController {
@FXML
private TableView<Person> table;
@FXML
void add(ActionEvent event) {
table.getItems().add(new Person("Bill", "Jones"));
}
}
Person.java
package formatter;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
private final StringProperty firstName = new SimpleStringProperty("");
private final StringProperty surname = new SimpleStringProperty("");
public Person(String firstName, String surname) {
setFirstName(firstName);
setSurname(surname);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String name) {
this.firstName.set(name);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getSurname() {
return surname.get();
}
public void setSurname(String name) {
surname.set(name);
}
public StringProperty surnameProperty() {
return surname;
}
}
Upvotes: 2