1 Reply Latest reply: Nov 4, 2012 8:43 PM by 972422 RSS

    TableView

    969853
      Hi everyone ,
      I doing a program in javaFx . .I have two tables student and standard ,where student table references the standard table... In TableView, student details should be displayed . While doing like that a problem occurs in TableView i want to display only a particular field from standard but i getting whole object

      I have attach the source code

      Domain : Standard.java
      --------------------------------------------------------------------------------------------------------------------------------------------
      package com.sample.tableview;

      import java.util.Date;


      /**
      * An entity for Standard. This class does not contain any business logic.
      *
      * @author 966850
      * @since Sep 3, 2012
      * @version : 1.0
      *
      */

      public class Standard {

           private long id;
           private String standardName;
           private Date createdDate;
           private boolean isDelete = false;

           public Standard() {
                super();
                // TODO Auto-generated constructor stub
           }

           public Standard(long id, String standard, Date createdDate, boolean isDelete) {
                super();
                this.id = id;
                standardName = standard;
                this.createdDate = createdDate;
                this.isDelete = isDelete;
           }

           public long getId() {
                return id;
           }

           public void setId(long id) {
                this.id = id;
           }

           public String getStandardName() {
                return standardName;
           }

           public void setStandardName(String standard) {
                standardName = standard;
           }

           public Date getCreatedDate() {
                return createdDate;
           }

           public void setCreatedDate(Date createdDate) {
                this.createdDate = createdDate;
           }

           public boolean getIsDelete() {
                return isDelete;
           }

           public void setIsDelete(boolean isDelete) {
                this.isDelete = isDelete;
           }

           @Override
           public String toString() {
                return "Standard [id=" + id + ", Standard=" + standardName
                          + ", createdDate=" + createdDate + ", isDelete=" + isDelete
                          + "]";
           }

      }
      -----------------------------------------------------------------------------------------------------------------------------------------------------

      Domain : Student.java
      -----------------------------------------------------------------------------------------------------------------------------------------------------
      package com.sample.tableview;

      import java.util.Date;


      /**
      * An entity for Student.
      * This class does not contain any business logic.
      *
      * @author 966850
      * @since Sep 1, 2012
      * @version : 1.0
      *
      */

      public class Student {
           private long id;
           private String name;
           private String fatherName;
           private String motherName;
           private String admissionNo;
           private Standard standard;
           private String division;
           private Date createdDate;
           private boolean isDelete = false;
           //private String standardName;
           
           public Student() {
                super();
                // TODO Auto-generated constructor stub
           }
           
           
           
           public Student(long id, String name, String fatherName, String motherName,
                     String admissionNo, Standard standard, String division,
                     Date createdDate, boolean isDelete) {
                super();
                this.id = id;
                this.name = name;
                this.fatherName = fatherName;
                this.motherName = motherName;
                this.admissionNo = admissionNo;
                this.standard = standard;
                this.division = division;
                this.createdDate = createdDate;
                this.isDelete = isDelete;
           }

           
           public Student(long id, String name, String fatherName, String motherName,
                     String admissionNo, String division,
                     Date createdDate, boolean isDelete) {
                super();
                this.id = id;
                this.name = name;
                this.fatherName = fatherName;
                this.motherName = motherName;
                this.admissionNo = admissionNo;
                this.division = division;
                this.createdDate = createdDate;
                this.isDelete = isDelete;
           }




           public long getId() {
                return id;
           }
           
           public void setId(long id) {
                this.id = id;
           }
           
           public String getName() {
                return name;
           }
           public void setName(String name) {
                this.name = name;
           }
           
           public String getFatherName() {
                return fatherName;
           }
           public void setFatherName(String fatherName) {
                this.fatherName = fatherName;
           }
           
           public String getMotherName() {
                return motherName;
           }
           public void setMotherName(String motherName) {
                this.motherName = motherName;
           }
           
           public String getAdmissionNo() {
                return admissionNo;
           }
           public void setAdmissionNo(String admissionNo) {
                this.admissionNo = admissionNo;
           }
           
           public Standard getStandard() {
                return standard;
           }
           public void setStandard(Standard standard) {
                this.standard = standard;
           }
           
           public String getDivision() {
                return division;
           }
           public void setDivision(String division) {
                this.division = division;
           }
           
           public Date getCreatedDate() {
                return createdDate;
           }
           public void setCreatedDate(Date createdDate) {
                this.createdDate = createdDate;
           }
           
           public boolean getIsDelete() {
                return isDelete;
           }
           public void setIsDelete(boolean isDelete) {
                this.isDelete = isDelete;
           }
           
           
      }
      -----------------------------------------------------------------------------------------------------------------------------------------------------
      Class: Main.java
      -----------------------------------------------------------------------------------------------------------------------------------------------------
      package com.sample.tableview;

      import java.util.logging.Level;
      import java.util.logging.Logger;

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

      public class Main extends Application{

           public static AnchorPane page;
      @Override
      public void start(Stage primaryStage) {
      try {
      page = (AnchorPane) FXMLLoader.load(Main.class.getResource("SampleTableView.fxml"));
      Scene scene = new Scene(page);
      primaryStage.setScene(scene);
      primaryStage.setTitle("Sample TableView");
      primaryStage.show();
      } catch (Exception ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }
      }


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

      -----------------------------------------------------------------------------------------------------------------------------------------------------
      fxml:SampleTableView.fxml

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

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

      <AnchorPane fx:id="tablePane" prefHeight="482.0" prefWidth="690.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.tableview.SampleTableViewController">
      <!-- TODO Add Nodes -->
      <children>
      <TableView fx:id="tableView" layoutX="43.0" layoutY="126.0" prefHeight="301.0" prefWidth="602.0" />
      </children>
      </AnchorPane>

      -------------------------------------------------------------------------------------------------------------------------------

      Class:SampleTableViewController.java
      --------------------------------------------------------------------------------------------------------------------------------
      package com.sample.tableview;

      import java.net.URL;
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      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 javafx.scene.layout.AnchorPane;

      public class SampleTableViewController implements Initializable {

           @FXML
           public AnchorPane tablePane;
           @FXML
           public TableView<Student> tableView;

           TableColumn<Student,String> colAdmissionNo = new TableColumn<Student,String>("Admission No");
           TableColumn<Student,String> colName = new TableColumn<Student,String>("Name");
           TableColumn<Student,String> colStandard = new TableColumn<Student,String>("Standard");
           //TableColumn<S, T> colStandard1 = new TableColumn("Standard");

           Standard standard1, standard2;
           Student student1, student2, student3, student4;

           public ObservableList<Student> studentObservableList = FXCollections
                     .observableArrayList();

           List<Student> studentsList = new ArrayList<>();

           public SampleTableViewController() {

           }

           @Override
           public void initialize(URL arg0, ResourceBundle arg1) {
                prepareContents();
                // prepareObservableLists();
                showStudentsDetails();
           }

           public void prepareContents() {
                standard1 = new Standard(1, "PKG", new Date(), false);
                standard2 = new Standard(2, "LKG", new Date(), false);

                student3 = new Student(1, "minu", "abbas", "shareefa", "111",
                          standard1, "A", new Date(), false);
                student4 = new Student(2, "keerthy", "rajan", "kumari", "112",
                          standard2, "A", new Date(), false);
                studentsList.add(student3);
                studentsList.add(student4);
                
                for(Student student:studentsList)
                {
                     studentObservableList.add(student);
                }
           }

           @SuppressWarnings("unchecked")
           public void showStudentsDetails() {
                colAdmissionNo.setMinWidth(100);
                colName.setMinWidth(100);
                colStandard.setMinWidth(100);
                colAdmissionNo
                          .setCellValueFactory(new PropertyValueFactory<Student, String>(
                                    "admissionNo"));
                colName.setCellValueFactory(new PropertyValueFactory<Student, String>(
                          "name"));
                colStandard
                          .setCellValueFactory(new PropertyValueFactory<Student, String>(
                                    "standard"));

                tableView.setItems(studentObservableList);
                tableView.getColumns().addAll(colAdmissionNo, colName,colStandard);
           }

      }

      ---------------------------------------------------------------------------------------------------------------------------------------------------------------




      If anyone knows about this problem ,Please let me know .....Very Urgent

      Edited by: 966850 on Oct 23, 2012 9:39 PM

      Edited by: 966850 on Oct 23, 2012 9:56 PM
        • 1. Re: TableView
          972422
          In Class SampleViewController

          this line
          colStandard
          .setCellValueFactory(new PropertyValueFactory<Student, String>(
          "*standard*"));

          You should put the particular field you want to display instead of the whole class "standard". For Example: id, standardName, createdDate,... that you've defined in the class Standard