我在Java中收到以下错误消息:“无法从StringProperty
转换为ObservableValue<SimpleStringProperty>
”
我正在使用TableView,并且想在TableColumn中添加数据。我有三个图表,每个图表都应包含类产品的一个属性(名称,数量,价格)。
我在这行中得到了错误:
sname.setCellValueFactory(cellData -> cellData.getValue().
相关代码:
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableColumn.CellEditEvent;
public class ViewShop extends BorderPane {
private Button add;
private Button remove;
private TextField name;
private TextField price;
private TextField count;
private VBox vbox;
private HBox hbox;
private TableView tv;
private TableColumn<Product, SimpleDoubleProperty> sprice;
private TableColumn<Product, SimpleStringProperty> sname;
private TableColumn <Product, SimpleIntegerProperty>scount;
private Label namelabel;
private Label pricelabel;
private Label countlabel;
private ListView<fpt.com.Product> list;
public ViewShop()
{
remove=new Button("Remove");
add=new Button("Add");
name=new TextField();
price=new TextField();
count=new TextField();
tv=new TableView<fpt.com.Product>();
sprice=new TableColumn<>("Preis");
sname= new TableColumn<>("Name");
scount=new TableColumn<>("Anzahl");
namelabel=new Label("Name");
pricelabel=new Label("Preis");
countlabel=new Label("Anzahl");
tv.getColumns().addAll(sname,sprice,scount);
hbox=new HBox();
vbox=new VBox();
list = new ListView<fpt.com.Product>();
//
// sname.setCellValueFactory(new Callback<CellDataFeatures<Product, SimpleStringProperty>, ObservableValue<SimpleStringProperty>>() {
// public ObservableValue<SimpleStringProperty> call(CellDataFeatures<Product, SimpleStringProperty> p) {
// // p.getValue() returns the Person instance for a particular TableView row
// return p.getValue().nameProperty();
//
// }
// });
sname.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
hbox.getChildren().addAll(remove, add);
vbox.getChildren().addAll(namelabel,name,pricelabel ,price, countlabel , count, hbox);
this.setRight(vbox);
this.setLeft(tv);
}
public void addEventHandleradd(EventHandler<ActionEvent> eventHandler) {
this.add.addEventHandler(ActionEvent.ACTION, eventHandler);
}
public void addEventHandlerremove(EventHandler<ActionEvent> eventHandler) {
this.remove.addEventHandler(ActionEvent.ACTION, eventHandler);
}
public String giveName()
{
return name.getText();
}
public String givePrice()
{
return price.getText();
}
public String giveCount()
{
return count.getText();
}
public ListView<fpt.com.Product> getList()
{
return list;
}
}
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.InvalidationListener;
import javafx.beans.property.DoubleProperty;
public class Product implements fpt.com.Product {
Long id;
SimpleStringProperty name;
SimpleIntegerProperty quantity;
SimpleDoubleProperty price;
public Product(String name, int quantity, double price)
{
this.name=new SimpleStringProperty(name);
this.quantity=new SimpleIntegerProperty(quantity);
this.price=new SimpleDoubleProperty(price);
}
@Override
public long getId() {
return id;
}
@Override
public void setId(long id) {
this.id=id;
}
@Override
public double getPrice() {
return this.price.get();
}
@Override
public void setPrice(double price) {
this.price.set(price);
}
@Override
public int getQuantity() {
return this.quantity.get();
}
@Override
public void setQuantity(int quantity) {
this.quantity.set(quantity);
}
@Override
public String getName() {
return this.name.get();
}
@Override
public void setName(String name) {
this.name.set(name);
}
@Override
public StringProperty nameProperty() {
return name;
}
@Override
public DoubleProperty priceProperty() {
return price;
}
@Override
public IntegerProperty quantityProperty() {
return quantity;
}
}
最佳答案
表列的类型错误。他们应该是
private TableColumn<Product, Number> sprice;
private TableColumn<Product, String> sname;
private TableColumn <Product, Number> scount;