【问题标题】:What is the BigInteger property in JavaFX?JavaFX 中的 BigInteger 属性是什么?
【发布时间】:2017-02-07 08:27:31
【问题描述】:

如何将BigInteger 用作JavaFX 中的属性,就像String as 属性用作SimpleStringProperty

【问题讨论】:

    标签: java javafx properties javafx-8 biginteger


    【解决方案1】:

    JavaFX 中没有BigIntegerProperty(或任何BigInteger 属性实现),但您可以将ObjectProperty<T> 用作ObjectProperty<BigInteger>

    ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
    

    该属性存储一个BigInteger实例,可以监听和绑定。

    示例

    使用此类型的Application,绑定到TextArea的文本:

    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            BorderPane root = new BorderPane();
    
            ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
            bigIntProp.addListener((obs, oldval, newval) -> System.out.println(newval));
    
            TextArea ta = new TextArea();
    
            bigIntProp.bind(Bindings.createObjectBinding(() ->
                            (!ta.getText().isEmpty()) ? new BigInteger(ta.getText()) : BigInteger.ZERO
                    , ta.textProperty()));
            root.setCenter(ta);
    
            primaryStage.setScene(new Scene(root, 300, 275));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      【解决方案2】:

      java中没有像SimpleStringProperty类这样的内置BigInteger Property类。

      所以我为你创建了一个SimpleBigIntegerProperty,它可以和那些内置的属性类一样使用。

      import java.math.BigInteger;
      import javafx.beans.property.SimpleObjectProperty;
      /**
       * 
       * This class provides a full implementation of a {@link Property} wrapping an
       * arbitrary {@code BigInteger}.
       */
      public class SimpleBigIntegerProperty extends SimpleObjectProperty<BigInteger>{
      
          private static final Object DEFAULT_BEAN = null;
          private static final String DEFAULT_NAME = "";
      
          private final Object bean;
          private final String name;
          /**
           * {@inheritDoc}
           */
          @Override
          public Object getBean() {
              return bean;
          }
      
          /**
           * {@inheritDoc}
           */
          @Override
          public String getName() {
              return name;
          }
      
          /**
           * The constructor of {@code BigIntegerProperty}
           */
          public SimpleBigIntegerProperty() {
              this(DEFAULT_BEAN, DEFAULT_NAME);
          }
          /**
           * The constructor of {@code BigIntegerProperty}
           * 
           * @param initialValue
           *            the initial value of the wrapped value
           */
          public SimpleBigIntegerProperty(BigInteger initialValue) {
              this(DEFAULT_BEAN, DEFAULT_NAME, initialValue);
          }
      
          /**
           * The constructor of {@code BigIntegerProperty}
           * 
           * @param bean
           *            the bean of this {@code BigIntegerProperty}
           * @param name
           *            the name of this {@code BigIntegerProperty}
           */
          public SimpleBigIntegerProperty(Object bean, String name) {
              this.bean = bean;
              this.name = (name == null) ? DEFAULT_NAME : name;
          }
      
          /**
           * The constructor of {@code BigIntegerProperty}
           * 
           * @param bean
           *            the bean of this {@code BigIntegerProperty}
           * @param name
           *            the name of this {@code BigIntegerProperty}
           * @param initialValue
           *            the initial value of the wrapped value
           */
          public SimpleBigIntegerProperty(Object bean, String name, BigInteger initialValue) {
              super(initialValue);
              this.bean = bean;
              this.name = (name == null) ? DEFAULT_NAME : name;
          }
      
      }
      

      示例 1:

      一个简单的例子,

      SimpleBigIntegerProperty bigInteger = new SimpleBigIntegerProperty(BigInteger.valueOf(123456789));
      System.out.println(bigInteger.getValue());
      

      示例 2:ObservableList 为例,

      private final ObservableList<Person> data = FXCollections.observableArrayList(
              new Person("Jon Skeet", BigInteger.valueOf(123456789)),
              new Person("Michael Brown", BigInteger.valueOf(987654321))
      );
      

      Person 类(具有人名和 age-in-seconds 属性)在哪里,

      public class Person {
      
          protected SimpleStringProperty personName;
          protected SimpleBigIntegerProperty ageInSeconds;
      
          public Person() {
              this.personName = null;
              this.ageInSeconds = null;
          }
      
          public Person(String person_name, BigInteger age_in_seconds) {
              this.personName = new SimpleStringProperty(person_name);
              this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
          }
      
          public void setPersonName(String person_name) {
              this.personName = new SimpleStringProperty(person_name);
          }
      
          public void setAgeInSeconds(BigInteger age_in_seconds) {
              this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
          }
      
          public String getPersonName() {
              return this.personName.getValue();
          }
      
          public BigInteger getAgeInSeconds() {
              return this.ageInSeconds.getValue();
          }
      }
      

      【讨论】:

      • 为什么不直接使用 ObjectProperty 和 SimpleObjectProperty?
      • 对于原始属性来说这是有意义的,这就是 JavaFX 开箱即用地提供它们的原因。 String 还有其他方法,因此 StringProperty 也很有意义。但除非您想为属性提供特殊方法,否则通常不需要自定义属性类型。
      猜你喜欢
      • 2013-03-17
      • 2010-09-06
      • 2015-04-05
      • 1970-01-01
      • 2015-02-25
      • 2014-09-02
      • 2015-02-10
      • 2020-10-10
      相关资源
      最近更新 更多