0

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?

1
  • 1
    What exactly is the question? Do you want the "hello, hi" in the same row than "a, b"? If so, you need to get the already created objects from your data first and then add the "hello, hi" to your objects. Commented Sep 16, 2015 at 5:16

1 Answer 1

2

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.

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

Comments

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.