JavaFX TableView: formatting one cell based on the value of another in a row

I have a class named TransactionWrapper that I am using to populate my ObservableList for a TableView in my application. This wrapper has an attribute (enumeration) indicating whether it is a waste or a deposit. I need to get to this in order to display / format the quantity cell (display it in red or green depending on the nature of the transaction) and I don't find anything there that helps me in this battle.

Basically what I want to do is look at the line and say if the type is unset, color the text in red and if it is the deposit color green ... I hope someone here can help me with this. I'll post below my attempt with setCellFactory as I've found elsewhere. This approach allows me to format the cell and display it, but the problem is inside the updateItem function, I can get the value of my transaction type.

amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>, TableCell<TransactionWrapper, String>>()
{
    @Override
    public TableCell<TransactionWrapper, String> call(
            TableColumn<TransactionWrapper, String> param)
    {
        return new TableCell<TransactionWrapper, String>()
        {
            @Override
            protected void updateItem(String item, boolean empty)
            {
                if (!empty)
                {
                    // should be something like (transaction.getType().equals(TransactionTypes.DEPOSIT) ? true : false;)
                    boolean isDeposit = true;
                    setText(item);                          
                    if(isDeposit) // should be if type is deposit
                    {
                        setTextFill(Color.GREEN);
                    }
                    else                            
                    {
                        setTextFill(Color.RED);
                    }
                }
            }
        };
    }
});

      

And this is how I set up my column:

amountCol.setCellValueFactory(cellData -> cellData.getValue().getAmountString());

      

This is disabling an object called TransactionWrapper with:

private final StringProperty transactionTypeString;
private final StringProperty dateString;
private final StringProperty amountString;
private final StringProperty payeeString;
private final StringProperty categoryString;
private final StringProperty notesString;
private Transaction transaction;

      

Any ideas on this would be much appreciated.: D

Thanks, John

+3


source to share


1 answer


I thought! Thanks for James's idea, but I went differently. Here's the code for anyone in the future reading this post:

amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>, 
            TableCell<TransactionWrapper, String>>()
            {
                @Override
                public TableCell<TransactionWrapper, String> call(
                        TableColumn<TransactionWrapper, String> param)
                {
                    return new TableCell<TransactionWrapper, String>()
                    {
                        @Override
                        protected void updateItem(String item, boolean empty)
                        {
                            if (!empty)
                            {
                                int currentIndex = indexProperty()
                                        .getValue() < 0 ? 0
                                        : indexProperty().getValue();
                                TransactionTypes type = param
                                        .getTableView().getItems()
                                        .get(currentIndex).getTransaction()
                                        .getTransactionType();
                                if (type.equals(TransactionTypes.DEPOSIT))
                                {
                                    setTextFill(Color.GREEN);
                                    setText("+ " + item);
                                } else
                                {
                                    setTextFill(Color.RED);
                                    setText("- " + item);
                                }
                            }
                        }
                    };
                }
            });

      



Param.getTableView () parameter. getItems (). get (currentIndex) was key. It had to expand the parent a bit, but it did the job. The biggest challenge is finding the index. Felt a bit silly when it turned out that there was an indexProperty () function. Lol. Didn't think to look at the class level features that were available. Happy coding!

+7


source







All Articles