gpt4 book ai didi

java - 如何设置 TableColumnValue JavaFX TableView

转载 作者:行者123 更新时间:2023-12-01 12:24:13 25 4
gpt4 key购买 nike

大家好,我有这个 xml 文件:

<?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,但我认为不需要将其包含在此处。所以我尝试使用以下方法将 xml 中的所有值插入到表中:

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

现在我如何将数据设置为单元值工厂。我正在尝试像这样使用 lambdaExpression:

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

我不知道如何将值插入到 tableColumn 中。对于类文件中的所有注释,我深表歉意。请帮忙。谢谢!

最佳答案

假设您有 TableView<Courses.Course>和一个 TableColumn<Courses.Course, String>我认为你需要

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

关于java - 如何设置 TableColumnValue JavaFX TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com