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.

pavankumar
  • 25
  • 1
  • 9
  • Have you tried to follow the official doc ? http://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" – Roberto Mar 19 '14 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. – pavankumar Mar 19 '14 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 ? – Roberto Mar 19 '14 at 12:26
  • i have added code please view – pavankumar Mar 19 '14 at 12:57

2 Answers2

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

Community
  • 1
  • 1
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
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);
Artest113
  • 97
  • 2
  • 10
  • 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 . – pavankumar Mar 19 '14 at 10:27
  • oh, sorry man, your request was "now i want to fill data at row 1 and coloumn 2" , I thought this should do it – Artest113 Mar 19 '14 at 14:43