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

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

问题描述

我有一个名为TransactionWrapper的类,用于在我的应用程序中填充TableView的ObservableList。这个包装器有一个属性(枚举),表明它是一个提款还是存款。我需要去渲染/格式化数量单元格(根据事务的性质以红色或绿色显示),而且我没有发现任何可以帮助我进行这个战斗的东西。

b
$ b基本上,我想要做的就是看行,并说如果类型是撤回,颜色文本红色,如果它的存款颜色绿色...我希望有人在这里可以帮助我这个。我会在我尝试使用setCellFactory的时候在其他地方发现。这种方法允许我格式化单元格以及它是如何显示的,但问题在于updateItem函数内部,我可以得到我的交易类型的值。

  amountCol.setCellFactory(new Callback< TableColumn< TransactionWrapper,String> ;, TableCell< TransactionWrapper,String>> (b 
@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)
{
//应该是类似于(transaction.getType()。equals(TransactionTypes.DEPOSIT)?true:false;)
boolean isDeposit = true; $ b $ setText(item);
if( isDeposit)//如果类型是存款
{
setTextFill(Color.GREEN);
}
else
{
setTextFill(Color.RED);
}
}
}
};
}
});

以下是我如何设置我的专栏:

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

这就是一个叫做TransactionWrapper的对象,它包含了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;
私人交易交易;

有关这方面的任何想法将不胜感激。 :D



感谢,
Jon

解决方案

出来!感谢詹姆斯的想法,但我采取了一些不同的方式。这里是任何人在未来阅读这篇文章的代码:

pre $ 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()函数存在时感觉有点傻..大声笑。没有想到要看可用的课程级功能。快乐的编码!


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.

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

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;

Any ideas on this would be much appreciated. :D

Thanks, Jon

解决方案

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!

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

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