0

i am having tableview with 5 columns, and 3 rows . now i want to fill data at row 1 and coloumn 2.please show me some sample code.

This is my code person.java

public class Person {

static int x = 1;
private final SimpleStringProperty firstName = new SimpleStringProperty("");
private final SimpleStringProperty lastName = new SimpleStringProperty("");
private final SimpleStringProperty emailid = new SimpleStringProperty("");
private final Integer iNumber = new Integer(x);

public Person(){
    this("","","");
}

public Person(String fn,String ln,String eid){

    x++;
    setFirstName(fn);
    setLastName(ln);
    setEmailId(eid);  
}

public void setFirstName(String fn){
    firstName.set(fn);
}

public void setLastName(String ln){
    lastName.set(ln);
}

public void setEmailId(String id){
    emailid.set(id);
}

public String getFirstName(){
    return firstName.get();
}

public String getLastName(){
     return lastName.get();
}

public String getEmailId(){
     return emailid.get();
}
}

AddressBookDocumentController.java code

public class AddressBookDocumentController implements Initializable {

 public AddressBookDocumentController(){
this.personData = FXCollections.observableArrayList(
            new Person("X","Y","Q"),
            new Person("A","B","W"),
            new Person("HAi","Bye","E")

    );
 }

@Override
public void initialize(URL url, ResourceBundle rb) {

    firstName.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
    lastName.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
    emailId.setCellValueFactory(new PropertyValueFactory<Person,String>("emailId"));
     tableId.setItems(personData);

}    

final ObservableList<Person> personData ;
@FXML TableView<Person> tableId;
@FXML TableColumn<Person, String> firstName;
@FXML TableColumn<Person,String> lastName;
@FXML TableColumn<Person,String>emailId;
@FXML TextField tfName;
@FXML TextField tlName;
@FXML TextField tEmail;


@FXML public void handleButtonSave(){

    tableId.getItems().get(0).setFirstName("JOHN"); //HERE CHANGES SHOULD REFLECT BUT IT's NOT HAPPENING 
}
}

Thanks, pavan.

4
  • Have you tried to follow the official doc ? docs.oracle.com/javafx/2/ui_controls/table-view.htm There is a difference between "ask for help with your code" and "ask for do your job" Commented Mar 19, 2014 at 12:10
  • i followed this link "docs.oracle.com/javafx/2/ui_controls/table-view.htm" only. i am able to add data to tableview through ObservableList. But i want to update particular cell value through button click event. i tried this also through tableview.getItems().get(0).setFirstName("JOHN"); . But changes are not reflecting. Commented Mar 19, 2014 at 12:21
  • Ok, in such case, It would help post your code or a fragment that show how are you doing. BTW, have you tried to update the ObservableList ? Commented Mar 19, 2014 at 12:26
  • i have added code please view Commented Mar 19, 2014 at 12:57

2 Answers 2

1

Add the following JavaFX specific getters to your Person class and then try again:

public SimpleStringProperty firstNameProperty(){
    return firstName;
}

public SimpleStringProperty lastNameProperty(){
    return lastName;
}

public SimpleStringProperty emailidProperty(){
    return emailid;
}

For more info refer to
Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller) and
Autoupdating rows in TableView from model

Sign up to request clarification or add additional context in comments.

Comments

0
 private final ObservableList<Person> data =
    FXCollections.observableArrayList(
        new Person("", "Smith", ""),

    );



TableColumn col1= new TableColumn("Column1");
    firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("Column1"));

TableColumn col2= new TableColumn("Column2");
    lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("Column2"));

TableColumn col3= new TableColumn("Column3");
    lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("Column3"));

 table.setItems(data);
     table.getColumns().addAll(col1, col2, col3);

2 Comments

my question is how to change value of smith to "john" after inserting the row with data as ("","smith","") to ---> ("","john","").i.e how to change at particular table cell in table view .
oh, sorry man, your request was "now i want to fill data at row 1 and coloumn 2" , I thought this should do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.