我正在尝试了解TableView
在JavaFX中的工作方式。我编写了一个简单的类,将电影列表添加到表中,但是运行它时,出现此错误:
javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'Price' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@3aebd1d5 with provided class type: class application.MovieClass
java.lang.IllegalStateException: Cannot read from unreadable property Price
电影类中有3个变量,所有这些变量我都得到相同的错误。
这是我写的2个课程
主要:
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
public class Main extends Application {
TableView<MovieClass> table;
@Override
public void start(Stage primaryStage) {
Label lblHeading = new Label("Movie Inventory");
TableView<MovieClass> table = new TableView<MovieClass>();
table.setItems(loadData());
TableColumn<MovieClass, String> colTitle = new TableColumn<MovieClass, String>("Title");
colTitle.setMinWidth(300);
colTitle.setCellValueFactory(
new PropertyValueFactory<MovieClass, String>("title"));
TableColumn<MovieClass, Integer> colYear = new TableColumn<MovieClass, Integer>("year");
colYear.setMinWidth(100);
colYear.setCellValueFactory(
new PropertyValueFactory<MovieClass, Integer>("Year"));
TableColumn<MovieClass, Double> colPrice = new TableColumn<MovieClass, Double>("price");
colPrice.setMinWidth(100);
colPrice.setCellValueFactory(
new PropertyValueFactory<MovieClass, Double>("Price"));
table.getColumns().addAll(colTitle, colYear, colPrice);
print(loadData());
VBox paneMain = new VBox();
paneMain.setSpacing(10);
paneMain.setPadding(new Insets(10, 10, 10, 10));
paneMain.getChildren().addAll(lblHeading, table);
Scene scene = new Scene(paneMain);
primaryStage.setScene(scene);
primaryStage.setTitle("Movie Inventory");
primaryStage.show();
}
public ObservableList<MovieClass> loadData()
{
ObservableList<MovieClass> data = FXCollections.observableArrayList();
data.add(new MovieClass("It's a Wonderful Life",1946, 14.95));
data.add(new MovieClass("Young Frankenstein",1974, 16.95));
data.add(new MovieClass("Star Wars Episode 4",1976, 17.95));
data.add(new MovieClass("The Princess Bride",1987, 16.95));
data.add(new MovieClass("Glory",1989, 14.95));
data.add(new MovieClass("The Game",1997, 14.95));
data.add(new MovieClass("Shakespeare in Love",1998, 19.95));
data.add(new MovieClass("The Invention of Lying",2009, 18.95));
data.add(new MovieClass("The King's Speech",2010, 19.95));
return data; }
public void print(ObservableList<MovieClass> list) {
for(int i = 0; i > list.size(); i++) {
System.out.println("The Title = " + list.get(i).getMyTitle()
+ " " + "The Year = " + list.get(i).getMyYear()
+ " " + "The Price = " + list.get(i).getMyPrice());
}
}
public static void main(String[] args) {
launch(args);
}
}
电影课:
package application;
public class MovieClass {
private String title;
private int year;
private double price;
public MovieClass()
{
this.title = "";
this.year = 0;
this.price = 0.0;
}
public MovieClass(String title, int year, double price)
{
this.title = title;
this.year = year;
this.price = price;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return this.year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
}