java - tutorial - openjfx




javafx:我怎樣才能使TableCell編輯返回double而不是字符串和字體的基礎上改變顏色的條件? (2)

我有類似的問題,我做瞭如下:

SimpleDoubleProperty price = new SimpleDoubleProperty();
price.setValue(Double.parseDouble(EditingDoubleCell().getString()));
ObservableValue<Double> g = price.asObject();
return g;

這個方法預計你可以把你的String解析成double。 為我工作,告訴我,如果它幫助:)

我有一個Trade像類

public class Trade {
    private DoubleProperty price;
    private ReadOnlyBooleanWrapper caution;

    public Trade(double price){
        this.price = new SimpleDoubleProperty(price);
        this.caution = new ReadOnlyBooleanWrapper();
        this.caution.bind(this.volume.greaterThan(0));
    }   

    public double getPrice(){
        return this.price.get();
    }   

    public DoubleProperty priceProperty(){
        return this.price;
    }

    public void setPrice(double price){
        this.price.set(price);
    }
}

在我的控制器類中,我有以下TableViewTableColumn

問題有兩方面:

  1. 價格屬性和價格列僅接受double 。 但是下面的EditingDoubleCell代碼只返回String。 我怎樣才能讓它返回double和所有的用戶輸入的String將被忽略?
  2. 我想要的第二個功能是: Price欄( 談論相同的價格單元格 )的單元格內的字體將caution屬性為true時將其顏色更改為藍色, caution屬性為false時將其顏色更改為紅色?
public class EditingDoubleCell extends TableCell<Trade,String>{

    private TextField textField;

    public EditingDoubleCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.requestFocus();
            //textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setText((String) getItem());
        setGraphic(null);
    }


    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());

                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }

    private void createTextField(){

        Locale locale  = new Locale("en", "UK");
        String pattern = "###,###.###";
        DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
        df.applyPattern(pattern);
        //String format = df.format(123456789.123);
        //System.out.println(format);

        //NumberFormat nf = NumberFormat.getIntegerInstance();        
        textField = new TextField();

        // add filter to allow for typing only integer
        textField.setTextFormatter( new TextFormatter<>( c ->
        {
            if (c.getControlNewText().isEmpty()) {
                return c;
            }
            ParsePosition parsePosition = new ParsePosition( 0 );
            Object object = df.parse( c.getControlNewText(), parsePosition );

            if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() )
            {
                return null;
            }
            else
            {
                return c;
            }
        } ) );

        textField.setText( getString() );

        textField.setMinWidth( this.getWidth() - this.getGraphicTextGap() * 2 );

        // commit on Enter
        textField.setOnAction( new EventHandler<ActionEvent>()
        {
            @Override
            public void handle( ActionEvent event )
            {
                commitEdit( textField.getText() );
            }
        } );

        textField.focusedProperty().addListener( new ChangeListener<Boolean>()
        {
            @Override
            public void changed( ObservableValue<? extends Boolean> arg0,
                    Boolean arg1, Boolean arg2 )
            {
                if ( !arg2 )
                {
                    commitEdit( textField.getText() );
                }
            }
        } );

    }
}

問題的第一部分 :你可以嘗試下面的類(它為我工作):

     public class EditingDoubleCell extends TableCell<Trade, Double> {

        private TextField textField;

        public EditingDoubleCell() {
            textField = new TextField();
            textField.setOnAction(e -> commitEdit(Double.valueOf(textField.getText())));
        }

        @Override
        public void startEdit() {
            if (!isEmpty()) {
                super.startEdit();
                setText(null);
                setGraphic(textField);
                textField.requestFocus();

            }
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setText(getString());
            setGraphic(null);
        }

        @Override
        public void commitEdit(Double newValue) {
            super.commitEdit(newValue);
        }

        @Override
        public void updateItem(Double item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                setText(null);
                setGraphic(null);
            } else {

                Locale locale = new Locale("en", "UK");
                String pattern = "###,###.###";
                DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
                df.applyPattern(pattern);
                String s = df.format(getItem());
                setText(s);
                setGraphic(null);
              // set font of Price cell to a color
            TableRow<Trade> row = getTableRow();
            if (row.getItem().getCaution()) {
                setStyle("-fx-background-color:blue;");
            } else {
                setStyle("-fx-background-color: red;");
                    }
            }
        }

        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }

    }

問題的第二部分 :只需調用setcellfactory(...)以獲得警告列,並且必須重寫updateItem(...)方法:

 cautionCol.setCellFactory(column -> new TableCell<Trade, Boolean>() {

        @Override
        protected void updateItem(Boolean item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setText(null);
            } else {
                setText(String.valueOf(item));
                //TableRow<Trade> row = getTableRow();
                if (item) {
                    setStyle("-fx-background-color:blue;");
                } else {
                    setStyle("-fx-background-color: red;");
                }

            }
        }

    });




javafx