标签:java properties javafx biginteger javafx-8
如何使用BigInteger作为JavaFX中的属性,如String作为属性用作SimpleStringProperty?
解决方法:
在java中没有像SimpleStringProperty类那样的内置BigInteger属性类.
所以我为你创建了一个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))
);
Where类(具有人名和年龄属性)是,
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();
}
}
标签:java,properties,javafx,biginteger,javafx-8 来源: https://codeday.me/bug/20190627/1308336.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。