我正在制作一个购物清单应用程序,它具有将项目添加到列表(tableview)以及项目和价格的功能。它是的,它的工作原理(因为tableview没有说“表中没有内容”)但表格是空白的。有什么建议? 代码:
package listproject;
public class Main extends Application{
public Node getNodeByRowColumnIndex(final int row,final int column,GridPane gridPane) {
Node result = null;
ObservableList<Node> childrens = gridPane.getChildren();
for(Node node : childrens) {
if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
result = node;
break;
}
}
return result;
}
ObservableList<Product> data =
FXCollections.observableArrayList(
);
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Grocery List");
primaryStage.setWidth(450);
primaryStage.setHeight(800);
//menu buttons
Button addLinkMain = new Button("Add an Item");
Button addLinkTitle = new Button("Add an Item");
Button mainLinkAdd = new Button("Back");
Button mainLinkTitle = new Button("Back");
Button addItem = new Button("Add");
Button setTitleMenu = new Button("Set Title");
Button setTitleAdd = new Button("Set Title");
Button clear = new Button("Clear");
//items for the item addition screen
TextField title = new TextField("Name");
TextField quantity = new TextField("#");
TextField price = new TextField("$");
HBox addBox = new HBox();
BorderPane addBorder = new BorderPane();
HBox addMenu = new HBox();
//mainScene items
BorderPane pane = new BorderPane();
HBox mainscenemenu=new HBox();
TableView<Product> tableview = new TableView<Product>();
//tableview items
TableColumn itemTitle=new TableColumn("Product");
TableColumn itemQuantity=new TableColumn("Quantity");
TableColumn itemPrice=new TableColumn("Price");
//settitle items
TextField listTitle= new TextField("Rename the List");
Button setTittle = new Button("Set List Title");
BorderPane setTitlePane=new BorderPane();
StackPane setTitleFieldContainer=new StackPane();
HBox setTitleOwnMenu = new HBox();
//Scene declaration
Scene mainScene = new Scene(pane);
primaryStage.setScene(mainScene);
Scene addScene = new Scene(addBorder);
Scene setTitleScene = new Scene(setTitlePane);
//Main Scene
pane.setCenter(tableview);
pane.setBottom(mainscenemenu);
tableview.getColumns().addAll(itemTitle,itemQuantity,itemPrice);
mainscenemenu.getChildren().addAll(setTitleMenu,clear,addLinkMain);
tableview.setItems(data);
//Item Addition Scene
addBox.getChildren().addAll(title,quantity,price);
addMenu.getChildren().addAll(mainLinkAdd,addItem,setTitleAdd);
addBorder.setCenter(addBox);
addBorder.setBottom(addMenu);
//Set Title Scene
setTitleFieldContainer.getChildren().addAll(listTitle);
setTitleOwnMenu.getChildren().addAll(addLinkTitle,setTittle,mainLinkTitle);
setTitlePane.setCenter(setTitleFieldContainer);
setTitlePane.setBottom(setTitleOwnMenu);
//Menu Button Event Handlers
addLinkMain.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(addScene);
}
});
addLinkTitle.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(addScene);
}
});
mainLinkAdd.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(mainScene);
}
});
mainLinkTitle.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(mainScene);
}
});
setTitleMenu.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(setTitleScene);
}
});
setTitleAdd.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.setScene(setTitleScene);
}
});
//Adding an Item to the List *unfinished
addItem.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
data.add(new Product(title.getText(),quantity.getText(),price.getText()));
}
});
//Changing the Title of The List
setTittle.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setTitle(listTitle.getText());
}
});
//Clearing the List
clear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
tableview.getItems().clear();
}
});
//Show the Stage
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
答案 0 :(得分:1)
您永远不会为表格的列设置cellValueFactory
。您需要执行此操作以指定应在列中显示的项目部分。
可以在此处找到匿名类为cellValueFactory
的示例。
使用PropertyValueFactory
可以减少所需的代码量。更多关于这一点。
可在此处找到教程:https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm