Stevantti
Stevantti

Reputation: 504

JavaFX: Can I access Nodes it contains?

So I have a BorderPane that has a TextArea that is set in the centre. It displays information about the current year's sales:

public static BorderPane addTransactionPanel() {

    BorderPane pane = new BorderPane();
    pane.setPrefSize(500, 320);
    final String[] monthName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    final TextArea middle = new TextArea();
    middle.setPrefRowCount(1000);
    middle.setEditable(false);

    pane.setBottom(addBox);
    pane.setCenter(middle);

    for (int i = 2; i < li.get(index).getIncomeBook().getExpense().size(); i++) {
        if (li.get(index).getIncomeBook().getExpense().get(i).getDate().getYear() == year) {
                middle.appendText("New expense of: $" + moneyDisplay(li.get(index).getIncomeBook().getExpense().get(i).getValue()) + " on " + monthName[li.get(index).getIncomeBook().getExpense().get(i).getDate().getMonth() - 1] + "\n");
        }
    }

    return pane;
 }

This compares the contents of the IncomeBook's expenses (and the year it was spent on), and if it matches the current year, it appends the text onto the TextArea. But now, when I change the year (which is a public variable), I want it to update. I'm trying to create another method:

public void updateTransactionPanel(BorderPane transactionPanel){
    // I want to clear transactionPanel.middle and write in it again.
    // Can I access the "middle" Node from another method?
}

Upvotes: 2

Views: 86

Answers (1)

jewelsea
jewelsea

Reputation: 159416

Access the "middle" Node of transactionPanel from another method, clear it and write in it again.

public void updateTransactionPanel(BorderPane transactionPanel){
    TextArea middle = (TextArea) transactionPanel.getCenter();
    middle.setText("Updated transaction panel text");
}

Upvotes: 2

Related Questions