Unable to insert values into tableview - JavaFx

早过忘川 提交于 2019-12-02 08:00:55

问题


I am new to JavaFx and I am trying to create a tableview and fxml is created using scene builder. If I run the program, the table is not getting the values. I found that this question is somehow matching with my requirement (javaFX 2.2 - Not able to populate table from Controller), but my code is matching with that too. But still I am not able to get the values in the table.

I have referenced the following video : http://www.youtube.com/watch?v=HiZ-glk9_LE

My code is as follows,

MainView.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class MainView extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane page = FXMLLoader.load(MainView.class.getResource("MainView.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Sample Window"); 
            primaryStage.setResizable(false); 
            primaryStage.show();
        } catch (Exception e) {
        }
    }
}

MainView.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.MainViewController">
  <children>
    <SplitPane dividerPositions="0.535175879396985" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
          <children>
            <TableView fx:id="tableID" prefHeight="210.0" prefWidth="598.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <columns>
                <TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" />
                <TableColumn prefWidth="100.0" text="Date" fx:id="tDate" />
                <TableColumn prefWidth="200.0" text="Name" fx:id="tName" />
                <TableColumn prefWidth="75.0" text="Price" fx:id="tPrice" />
              </columns>
            </TableView>
          </children>
        </AnchorPane>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
      </items>
    </SplitPane>
  </children>
</AnchorPane>

MainViewController.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import model.Table;

/**
 * FXML Controller class
 *
 */
public class MainViewController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    TableView<Table> tableID;
    @FXML
    TableColumn<Table, Integer> tID;
    @FXML
    TableColumn<Table, String> tName;
    @FXML
    TableColumn<Table, String> tDate;
    @FXML
    TableColumn<Table, String> tPrice;
    int iNumber = 1;
    ObservableList<Table> data =
            FXCollections.observableArrayList(
            new Table(iNumber++, "Dinesh", "12/02/2013", "20"),
            new Table(iNumber++, "Vignesh", "2/02/2013", "40"),
            new Table(iNumber++, "Satheesh", "1/02/2013", "100"),
            new Table(iNumber++, "Dinesh", "12/02/2013", "16"));

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // System.out.println("called");

        tID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rID"));
        tName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
        tDate.setCellValueFactory(new PropertyValueFactory<Table, String>("rDate"));
        tPrice.setCellValueFactory(new PropertyValueFactory<Table, String>("rPrice"));
        tableID.setItems(data);

    }
}

Table.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;


public class Table {

    SimpleIntegerProperty rID;
    SimpleStringProperty rName;
    SimpleStringProperty rDate;
    SimpleStringProperty rPrice;

    public Table(int rID, String rName, String rDate, String rPrice) {
        this.rID = new SimpleIntegerProperty(rID);
        this.rName = new SimpleStringProperty(rName);
        this.rDate = new SimpleStringProperty(rDate);
        this.rPrice = new SimpleStringProperty(rPrice);
        System.out.println(rID);
        System.out.println(rName);
        System.out.println(rDate);
        System.out.println(rPrice);
    } 

    public Integer getrID() {
        return rID.get();
    }

    public void setrID(Integer v) {
        this.rID.set(v);
    }

    public String getrDate() {
        return rDate.get();
    }

    public void setrDate(String d) {
        this.rDate.set(d);
    }

    public String getrName() {
        return rName.get();
    }

    public void setrName(String n) {
         this.rDate.set(n);
    }

    public String getrPrice() {
        return rPrice.get();
    }

    public void setrPrice(String p) {
       this.rPrice.set(p); 
    }
}

Thanks in advance.


回答1:


In a Table.java change all getters and setters names,
change getr* to getR*
change setr* to setR*




回答2:


Thanks to @Uluk Biy. Whatever he mentioned, that needs to be changed and I found that I missed the <cellValueFactory> and <PropertyValueFactory> tags under the <TableColumn> tag for each columns in the FXML file which is as follows,

<TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" >
 <cellValueFactory>
    <PropertyValueFactory property="tID" />
  </cellValueFactory>
</TableColumn>

Similar to this, all columns need to be updated like this.

After this change, the code is working fine. I am able to add the values into the row. :)



来源:https://stackoverflow.com/questions/14868489/unable-to-insert-values-into-tableview-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!