0

I have a table-view which gets populated every time nicely,

My main controller:

@Override
    public void initialize(URL location, ResourceBundle resources) {
        TableColumn tblc = new TableColumn("Test");
        tblc.setCellValueFactory(new PropertyValueFactory<Test, Integer>("TestData"));
        tbl1.getColumns().setAll(tblc);
        tbl1.setItems(aHandler.getInstance().getData());
        ...

My Test Class:

public class Test extends Thread {
    public Integer TestData=1;
    ....

Test1 class which extends Test class and overwrite its run:

public class Test1 extends Test {
    @Override
    public void run() {
         Timer timer = new Timer();
         timer.scheduleAtFixedRate(new TimerTask(){
                public void run() {
                    setTestData(getTestData()+1);
                    System.out.println(getTestData());
                }
            }, new Date(), 1000);
...

My aHandler:

public class aHandler {
    private ObservableList<Test> TestObservableList = FXCollections.<Test>observableArrayList();

    public ObservableList<Test> getData() {
            return TestObservableList;
        }

Since the tbl1 is binded to aHandler.getInstance().GetData(), the first time I initialize the class, I see column Test populated with value 1, as TestData=1; but when the timer start to change TestData, the table view does not get the new assigned value. What I am doing wrong?

I tried the best I can to explain the problem, please let me know if its still unclear, I will explain more.

EDIT: to better explain, I want to update TableView automatically everytime when the TestData value changes.. That's my end goal.

Emily Wong
  • 297
  • 3
  • 10
  • Please provide a [mcve] that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them – kleopatra Feb 24 '19 at 17:02
  • I think what I posted is enough to understand, the names are just for this question and please dont ignore them, as they are all different in the actual project – Emily Wong Feb 24 '19 at 17:05
  • 99% of "data not-showing" is due to https://stackoverflow.com/questions/18971109/javafx-tableview-not-showing-data-in-all-columns or a variant thereof, update problems can be as simple as populating another table than your think you do or concurrency problems due to violating the update-on-fx-application-thread rule (that's what your snippet seems to be doing) – kleopatra Feb 24 '19 at 17:07
  • *head-shaking ... you know that you have a logical problem with a statement like _I think what I posted is enough to understand_? If that would be true, you could solve the problem yourself. It not, you have to take my word for what I need to solve your problem .. – kleopatra Feb 24 '19 at 17:09
  • _Unrelated:_ Avoid using [raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). Your `TableColumn` should declare its generic parameters—`TableColumn`. – Slaw Feb 24 '19 at 17:13
  • @kleopatra, nvm dont shake your head too much, solved already. – Emily Wong Feb 24 '19 at 17:13
  • Solution is to use TableView.refresh()... – Emily Wong Feb 24 '19 at 17:13

1 Answers1

0

Solution was to use TableView.refresh(); in a Timer.

 Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                Platform.runLater(() -> {
                    tableView.refresh();
                });
            }
        }, new Date(), 1000);
Emily Wong
  • 297
  • 3
  • 10
  • This may cease to work after a certain time unless you take specal precaucious when designing `Test` to prevent java from creating a copy of the data for every thread. – fabian Feb 24 '19 at 17:38
  • no, refresh is nearly always wrong and just masking an error in your setup – kleopatra Feb 24 '19 at 18:22
  • @kleopatra, please let others answer and don't spam here, thanks. – Emily Wong Feb 25 '19 at 08:44
  • it's not spam: you are doing something fundamentally wrong (vs fixing your underlying problem) and sell that as a solution - I'm warning future readers to not follow you into that dead end. And you better stop harrassing me! – kleopatra Feb 25 '19 at 09:48
  • @kleopatra, What is the problem??, believe me no one understand what you talking about and when I am asking what is the solution you are telling me find and fix and bla bla, You need to: "Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem". Can you explain properly what you are referring to and what is the problem? I am looking for good Solution but don't have time to argue with people...... Please come up with a small snippet of your proposed solution if you have any. – Emily Wong Feb 25 '19 at 14:57