2014-10-20 60 views
0

大家好我有這樣的XML文件:如何設置TableColumnValue JavaFX的表視圖

<?xml version='1.0' encoding='UTF-8'?> 
<courses> 
    <course> 
     <name>Java</name> 
     <instructor> 
      <name>Bohdana Dumanska</name> 
      <acceptance>yes</acceptance> 
     </instructor> 
     <students> 
      <student> 
       <name>Brenden Walski</name> 
       <acceptance>yes</acceptance> 
      </student> 
      <student> 
       <name>Ty Flint</name> 
       <acceptance>yes</acceptance> 
      </student> 
     </students> 
     <time>Monday 6:00 - 8:00</time> 
     <availability>yes</availability> 
     <studentsEnrolled>2</studentsEnrolled> 
     <maxStudents>30</maxStudents> 
    </course> 
    <course> 
     <name>Physics</name> 
     <instructor> 
      <name>Jack Nelson</name> 
      <acceptance>yes</acceptance> 
     </instructor> 
     <students> 
      <student> 
       <name>Nazariy Dumic</name> 
       <acceptance>yes</acceptance> 
      </student> 
      <student> 
       <name>Bren Awesome</name> 
       <acceptance>yes</acceptance> 
      </student> 
     </students> 
     <time>Tuesday 6:00 - 8:00</time> 
     <availability>yes</availability> 
     <studentsEnrolled>2</studentsEnrolled> 
     <maxStudents>30</maxStudents> 
    </course> 
</courses> 

隨着那的NetBeans從XSD文件中創建這些類:

package schedule.app.courses; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 


/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="course" maxOccurs="unbounded"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*     &lt;element name="instructor"> 
*      &lt;complexType> 
*      &lt;complexContent> 
*       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*       &lt;sequence> 
*        &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*        &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*       &lt;/sequence> 
*       &lt;/restriction> 
*      &lt;/complexContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;element name="students"> 
*      &lt;complexType> 
*      &lt;complexContent> 
*       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*       &lt;sequence> 
*        &lt;element name="student" maxOccurs="unbounded"> 
*        &lt;complexType> 
*         &lt;complexContent> 
*         &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*          &lt;sequence> 
*          &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*          &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*          &lt;/sequence> 
*         &lt;/restriction> 
*         &lt;/complexContent> 
*        &lt;/complexType> 
*        &lt;/element> 
*       &lt;/sequence> 
*       &lt;/restriction> 
*      &lt;/complexContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;element name="time" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*     &lt;element name="availability" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*     &lt;element name="studentsEnrolled" type="{http://www.w3.org/2001/XMLSchema}int"/> 
*     &lt;element name="maxStudents" type="{http://www.w3.org/2001/XMLSchema}int"/> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "course" 
}) 
@XmlRootElement(name = "courses") 
public class Courses { 

    @XmlElement(required = true) 
    protected List<Courses.Course> course; 

    /** 
    * Gets the value of the course property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the course property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getCourse().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link Courses.Course } 
    * 
    * 
    */ 
    public List<Courses.Course> getCourse() { 
     if (course == null) { 
      course = new ArrayList<Courses.Course>(); 
     } 
     return this.course; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;sequence> 
    *   &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *   &lt;element name="instructor"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;sequence> 
    *     &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *     &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *     &lt;/sequence> 
    *    &lt;/restriction> 
    *    &lt;/complexContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *   &lt;element name="students"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;sequence> 
    *     &lt;element name="student" maxOccurs="unbounded"> 
    *      &lt;complexType> 
    *      &lt;complexContent> 
    *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *       &lt;sequence> 
    *        &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *        &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *       &lt;/sequence> 
    *       &lt;/restriction> 
    *      &lt;/complexContent> 
    *      &lt;/complexType> 
    *     &lt;/element> 
    *     &lt;/sequence> 
    *    &lt;/restriction> 
    *    &lt;/complexContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *   &lt;element name="time" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *   &lt;element name="availability" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *   &lt;element name="studentsEnrolled" type="{http://www.w3.org/2001/XMLSchema}int"/> 
    *   &lt;element name="maxStudents" type="{http://www.w3.org/2001/XMLSchema}int"/> 
    *  &lt;/sequence> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "name", 
     "instructor", 
     "students", 
     "time", 
     "availability", 
     "studentsEnrolled", 
     "maxStudents" 
    }) 
    public static class Course { 

     @XmlElement(required = true) 
     protected String name; 
     @XmlElement(required = true) 
     protected Courses.Course.Instructor instructor; 
     @XmlElement(required = true) 
     protected Courses.Course.Students students; 
     @XmlElement(required = true) 
     protected String time; 
     @XmlElement(required = true) 
     protected String availability; 
     protected int studentsEnrolled; 
     protected int maxStudents; 

     /** 
     * Gets the value of the name property. 
     * 
     * @return 
     *  possible object is 
     *  {@link String } 
     *  
     */ 
     public String getName() { 
      return name; 
     } 

     /** 
     * Sets the value of the name property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link String } 
     *  
     */ 
     public void setName(String value) { 
      this.name = value; 
     } 

     /** 
     * Gets the value of the instructor property. 
     * 
     * @return 
     *  possible object is 
     *  {@link Courses.Course.Instructor } 
     *  
     */ 
     public Courses.Course.Instructor getInstructor() { 
      return instructor; 
     } 

     /** 
     * Sets the value of the instructor property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link Courses.Course.Instructor } 
     *  
     */ 
     public void setInstructor(Courses.Course.Instructor value) { 
      this.instructor = value; 
     } 

     /** 
     * Gets the value of the students property. 
     * 
     * @return 
     *  possible object is 
     *  {@link Courses.Course.Students } 
     *  
     */ 
     public Courses.Course.Students getStudents() { 
      return students; 
     } 

     /** 
     * Sets the value of the students property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link Courses.Course.Students } 
     *  
     */ 
     public void setStudents(Courses.Course.Students value) { 
      this.students = value; 
     } 

     /** 
     * Gets the value of the time property. 
     * 
     * @return 
     *  possible object is 
     *  {@link String } 
     *  
     */ 
     public String getTime() { 
      return time; 
     } 

     /** 
     * Sets the value of the time property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link String } 
     *  
     */ 
     public void setTime(String value) { 
      this.time = value; 
     } 

     /** 
     * Gets the value of the availability property. 
     * 
     * @return 
     *  possible object is 
     *  {@link String } 
     *  
     */ 
     public String getAvailability() { 
      return availability; 
     } 

     /** 
     * Sets the value of the availability property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link String } 
     *  
     */ 
     public void setAvailability(String value) { 
      this.availability = value; 
     } 

     /** 
     * Gets the value of the studentsEnrolled property. 
     * 
     */ 
     public int getStudentsEnrolled() { 
      return studentsEnrolled; 
     } 

     /** 
     * Sets the value of the studentsEnrolled property. 
     * 
     */ 
     public void setStudentsEnrolled(int value) { 
      this.studentsEnrolled = value; 
     } 

     /** 
     * Gets the value of the maxStudents property. 
     * 
     */ 
     public int getMaxStudents() { 
      return maxStudents; 
     } 

     /** 
     * Sets the value of the maxStudents property. 
     * 
     */ 
     public void setMaxStudents(int value) { 
      this.maxStudents = value; 
     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;complexContent> 
     *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *  &lt;sequence> 
     *   &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
     *   &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
     *  &lt;/sequence> 
     *  &lt;/restriction> 
     * &lt;/complexContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "name", 
      "acceptance" 
     }) 
     public static class Instructor { 

      @XmlElement(required = true) 
      protected String name; 
      @XmlElement(required = true) 
      protected String acceptance; 

      /** 
      * Gets the value of the name property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getName() { 
       return name; 
      } 

      /** 
      * Sets the value of the name property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setName(String value) { 
       this.name = value; 
      } 

      /** 
      * Gets the value of the acceptance property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getAcceptance() { 
       return acceptance; 
      } 

      /** 
      * Sets the value of the acceptance property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setAcceptance(String value) { 
       this.acceptance = value; 
      } 

     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;complexContent> 
     *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *  &lt;sequence> 
     *   &lt;element name="student" maxOccurs="unbounded"> 
     *   &lt;complexType> 
     *    &lt;complexContent> 
     *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *     &lt;sequence> 
     *     &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
     *     &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
     *     &lt;/sequence> 
     *    &lt;/restriction> 
     *    &lt;/complexContent> 
     *   &lt;/complexType> 
     *   &lt;/element> 
     *  &lt;/sequence> 
     *  &lt;/restriction> 
     * &lt;/complexContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "student" 
     }) 
     public static class Students { 

      @XmlElement(required = true) 
      protected List<Courses.Course.Students.Student> student; 

      /** 
      * Gets the value of the student property. 
      * 
      * <p> 
      * This accessor method returns a reference to the live list, 
      * not a snapshot. Therefore any modification you make to the 
      * returned list will be present inside the JAXB object. 
      * This is why there is not a <CODE>set</CODE> method for the student property. 
      * 
      * <p> 
      * For example, to add a new item, do as follows: 
      * <pre> 
      * getStudent().add(newItem); 
      * </pre> 
      * 
      * 
      * <p> 
      * Objects of the following type(s) are allowed in the list 
      * {@link Courses.Course.Students.Student } 
      * 
      * 
      */ 
      public List<Courses.Course.Students.Student> getStudent() { 
       if (student == null) { 
        student = new ArrayList<Courses.Course.Students.Student>(); 
       } 
       return this.student; 
      } 


      /** 
      * <p>Java class for anonymous complex type. 
      * 
      * <p>The following schema fragment specifies the expected content contained within this class. 
      * 
      * <pre> 
      * &lt;complexType> 
      * &lt;complexContent> 
      *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
      *  &lt;sequence> 
      *   &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
      *   &lt;element name="acceptance" type="{http://www.w3.org/2001/XMLSchema}string"/> 
      *  &lt;/sequence> 
      *  &lt;/restriction> 
      * &lt;/complexContent> 
      * &lt;/complexType> 
      * </pre> 
      * 
      * 
      */ 
      @XmlAccessorType(XmlAccessType.FIELD) 
      @XmlType(name = "", propOrder = { 
       "name", 
       "acceptance" 
      }) 
      public static class Student { 

       @XmlElement(required = true) 
       protected String name; 
       @XmlElement(required = true) 
       protected String acceptance; 

       /** 
       * Gets the value of the name property. 
       * 
       * @return 
       *  possible object is 
       *  {@link String } 
       *  
       */ 
       public String getName() { 
        return name; 
       } 

       /** 
       * Sets the value of the name property. 
       * 
       * @param value 
       *  allowed object is 
       *  {@link String } 
       *  
       */ 
       public void setName(String value) { 
        this.name = value; 
       } 

       /** 
       * Gets the value of the acceptance property. 
       * 
       * @return 
       *  possible object is 
       *  {@link String } 
       *  
       */ 
       public String getAcceptance() { 
        return acceptance; 
       } 

       /** 
       * Sets the value of the acceptance property. 
       * 
       * @param value 
       *  allowed object is 
       *  {@link String } 
       *  
       */ 
       public void setAcceptance(String value) { 
        this.acceptance = value; 
       } 

      } 

     } 

    } 

} 

還有一個ObjectFactory,但我不要以爲我需要把它包含在這裏。 所以我用這個試圖插入E從XML表中的所有值:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class); 
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
      courses = (Courses)unmarshaller.unmarshal(new File("courses.xml")); 
      courseList = FXCollections.observableArrayList(courses.getCourse()); 

現在我會如何將數據設置爲cellvalue factoy。我試圖用lambadaExpression這樣的:

TableColumn name = new TableColumn("Name"); 
name.setCellValueFactory(cellData -> cellData.toString(courseList....??? 

我不知道進tableColumn.I是爲所有在類文件中的註釋抱歉,我將如何插入值。 請幫忙。謝謝!

回答

1

假設你有一個TableView<Courses.Course>TableColumn<Courses.Course, String>我想你需要

TableColumn<Courses.Course, Name> name = new TableColumn<>("Name"); 
name.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getName())); 
+0

謝謝你這麼多。這工作!我可能從來沒有想過這件事! – Nazariy1995 2014-10-20 21:28:46

+0

關於更多的問題,我會怎麼做像maxStudents我將不得不分配一個整數。我嘗試使用ReadOnlyIntegerWrapper,但它給了我一個錯誤,說ReadOnlyIntegerWrapper不能轉換爲ObservableValue Nazariy1995 2014-10-20 21:47:29