JavaFX TableView:在特定列中添加单个值 [英] JavaFX TableView: adding individual values in specific column

查看:237
本文介绍了JavaFX TableView:在特定列中添加单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法为此找到解决方案,并且新的java / javafx:

I cant seem to find a solution for this and new to java/ javafx:

我有3列tableview,最后一列是价格列。
每当在tableview中添加或删除行时,我想显示price列的运行总计。

I have a 3 column tableview , last column is a price column. I would like to display a running total of the price column whenever a row is added or deleted from the tableview.

TableView是从一个ObservableList填充的每行3个字段对象。
String id,String product,Double price .........这是我希望在单独的textField中保持运行总计的价格

TableView is populated from an ObservableList which holds a 3 field object per row. String id,String product, Double price.........it is the price I would like to keep a running total of in a separate textField

推荐答案

由于tableview的项目是ObservableList,您可以跟踪ListChangeListener,并更新计算的总价格:

Since the items of tableview are ObservableList, you may track for ListChangeListener, and update the calculated total price:

public class Sample extends Application
{

    @Override
    public void start( Stage primaryStage )
    {
        // items set to tableview
        ObservableList<Product> products = FXCollections.observableArrayList();

        DoubleProperty totalProperty = new SimpleDoubleProperty( 0 );

        products.addListener(( ListChangeListener.Change<? extends Product> change ) ->
        {
            while ( change.next() )
            {
                if ( change.wasAdded() )
                {
                    for ( Product p : change.getAddedSubList() )
                    {
                        totalProperty.set( totalProperty.get() + p.getPrice() );
                    }
                }
                else if ( change.wasRemoved() )
                {
                    for ( Product p : change.getRemoved() )
                    {
                        totalProperty.set( totalProperty.get() - p.getPrice() );
                    }
                }
            }
        });

        TextField textField = new TextField();
        textField.textProperty().bind( totalProperty.asString() );

        Random random = new Random();

        Button btnAdd = new Button( "Add product" );
        btnAdd.setOnAction( ( ActionEvent event ) ->
        {
            products.add( new Product( "new", ( double ) random.nextInt( 100 ) ) );
        } );

        Button btnRemove = new Button( "Remove product" );
        btnRemove.setOnAction( ( ActionEvent event ) ->
        {
            if ( products.size() > 0 )
            {
                products.remove( random.nextInt( products.size() ) );
            }
        } );

        VBox root = new VBox();
        root.getChildren().addAll( textField, btnAdd, btnRemove );

        Scene scene = new Scene( root, 300, 250 );

        primaryStage.setScene( scene );
        primaryStage.show();
    }


    public static class Product
    {
        String name;
        Double price;


        public Product( String name, Double price )
        {
            this.name = name;
            this.price = price;
        }


        public String getName()
        {
            return name;
        }


        public void setName( String name )
        {
            this.name = name;
        }


        public Double getPrice()
        {
            return price;
        }


        public void setPrice( Double price )
        {
            this.price = price;
        }

    }


    public static void main( String[] args )
    {
        launch( args );
    }

}

这篇关于JavaFX TableView:在特定列中添加单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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