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

匆匆过客 提交于 2019-11-29 02:39:56

Figured it out! Thanks for the idea James, but I went a bit of a different way. 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);
                                }
                            }
                        }
                    };
                }
            });

The param.getTableView().getItems().get(currentIndex) was key.. had to drill into the parent a bit there, but it got the job done. The biggest challange there was finding the index. Felt a bit silly when I figure out that the indexProperty() function existed.. lol. Didn't think to look at the class level functions that were available. Happy coding!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!