2016-07-24 55 views
0

我正在製作購物清單應用程序,並且它具有將項目添加到列表(tableview)以及項目數量和價格。事情是這樣的,它可以工作(因爲tableview不會說「表中沒有內容」)但是表格是空白的。有什麼建議麼? 代碼:當試圖通過ObservableList將項目添加到JavaFX 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); 
} 

}

回答

相關問題