JavaFX TableView:根据行中另一个单元格的值格式化一个单元格 [英] JavaFX TableView: format one cell based on the value of another in the row

查看:47
本文介绍了JavaFX TableView:根据行中另一个单元格的值格式化一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 TransactionWrapper 的类,用于为我的应用程序中的 TableView 填充 ObservableList.这个包装器有一个属性(枚举),指示它是取款还是存款.我需要去渲染/格式化金额单元格(根据交易的性质将其显示为红色或绿色),但我没有找到任何可以帮助我应对这场战斗的东西.

I have a class named TransactionWrapper I'm using to populate my ObservableList for the TableView in my application. This wrapper has an attribute (enum) indicating whether it is a withdrawal or a deposit. I need to get to that to render/format the amount cell (display it in red or green based on the nature of the transaction) and I'm not finding anything out there that is helping me with that battle.

基本上我想要做的是查看该行并说如果类型是提款,则将文本涂成红色,如果是存款,则将其涂成绿色......我希望这里有人可以帮助我解决这个问题.我将在下面发布我在其他地方发现的 setCellFactory 尝试.这种方法允许我格式化单元格及其显示方式,但问题出在 updateItem 函数内部,我可以得到我的事务类型的值.

Basically what I want to do is look at the row and say if the type is withdrawal, color the text red and if it's a deposit color it green... I'm hoping someone here can help me out with this. I'll post below my attempt at it with setCellFactory as I found in other places. This approach allows me to format the cell and how it is displayed but the problem is inside of the updateItem function, I can get to my 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 here's how I'm setting up my column:

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

这是运行一个名为 TransactionWrapper 的对象,其 fol:

That is running off of an object called TransactionWrapper with the fol:

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;

对此的任何想法将不胜感激.:D

Any ideas on this would be much appreciated. :D

谢谢,乔恩

推荐答案

想通了!感谢詹姆斯的想法,但我采取了一些不同的方式.这是以后阅读这篇文章的任何人的代码:

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);
                                }
                            }
                        }
                    };
                }
            });

param.getTableView().getItems().get(currentIndex) 是关键.. 必须在那里钻取到父级,但它完成了工作.最大的挑战是找到索引.当我发现 indexProperty() 函数存在时感觉有点傻……哈哈.没有考虑查看可用的类级别功能.编码愉快!

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!

这篇关于JavaFX TableView:根据行中另一个单元格的值格式化一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆