Akshay
Akshay

Reputation: 2785

Adding two arrays to two columns in JavaFX TableView

I have successfully added one array to one column in TableView with the following code:

data = FXCollections.observableArrayList();

    String[] a = {"a", "b"};

    yearColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Year"));
    interestColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Interest"));
    principalColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Principal"));
    balanceColumn.setCellValueFactory(new PropertyValueFactory<test3, String>("Balance"));

    paymentsAnnual.setItems(data);

    for (String anA : a) {
        test3 test31 = new test3();
        test31.year.setValue(anA);
        data.add(test31);
    }

test3.java

import javafx.beans.property.SimpleStringProperty;

public class test3 {

    public SimpleStringProperty year = new SimpleStringProperty();
    public SimpleStringProperty interest = new SimpleStringProperty();
    public SimpleStringProperty principal = new SimpleStringProperty();
    public SimpleStringProperty balance = new SimpleStringProperty();

    public String getYear() {
        return year.get();
    }


    public String getInterest() {
        return interest.get();
    }


    public String getPrincipal() {
        return principal.get();
    }


    public String getBalance() {
        return balance.get();
    }

}

Now I have a problem when I try to insert a new array to a different column at a time. say I have an array String[] b = {"hello", "hi"}, then I add it using the same for each loop, something like this

for (String anA : a) {
            test3 test31 = new test3();
            test31.year.setValue(anA);
            data.add(test31);
        }
for (String anA1 : b) {
            test3 test31 = new test3();
            test31.balance.setValue(anA1);
            data.add(test31);
        }

this adds the array but I get an output something like this

enter image description here

Can anyone tell me how to add them together?

UPDATE

How should I add array a and b together, which would give an output like this enter image description here

UPDATE-2

I do have one way of doing it , by making it a double array

String[] a = {{"a", "b"},{"hello","hi"}};
for (int i = 1; i < a[0].length; i++){
     test3 test31 = new test3();
     test31.year.setValue(a[0][i]);
     test31.balance.setValue(a[1][i]);
     data.add(test31);
}

this way I am able to add the contents at a time in its correct row, what if its a single array?

Upvotes: 0

Views: 697

Answers (1)

VGR
VGR

Reputation: 44414

Every item in a TableView's items list represents one row.

Your first loop adds two rows, which each have a year defined but have no other properties defined.

Your second loop adds two more rows, which each have a balance defined but have no other properties defined.

You must modify the existing objects in your List instead of adding more rows.

Upvotes: 2

Related Questions