英文:
How to add a JCheckBox column to my JTable without using models?
问题
我想在不使用模型的情况下,向我的JTable
添加一个JCheckBox
列。我搜索并找到了一些解决方案,比如使用模型,但它们不是我想要的,因为我的项目已经处于最新阶段,可能会引发一些问题,而且我也不知道如何使用模型。如果有人可以帮助我找到解决方案,我将非常感谢。
for (int cnt = 0; myStudent.currentCourses[cnt] != 0; cnt++) {
for (course c : Courses) {
if (myStudent.currentCourses[cnt] == c.getCourseNum() && myStudent.groupNum[cnt] == c.getGroupsContained()) {
data[cnt][0] = String.valueOf(c.getCourseNum());
data[cnt][1] = String.valueOf(myStudent.groupNum[cnt]);
data[cnt][2] = String.valueOf(c.getUnits());
data[cnt][3] = c.getCourseName();
data[cnt][4] = String.valueOf(c.getSignedNum());
data[cnt][5] = c.getProfessorName();
data[cnt][6] = c.getExamDate();
data[cnt][7] = c.getClassSchedule();
data[cnt][8] = c.getPrerequisites();
data[cnt][9] = c.getExtraInfo();
}
}
}
courseTable = new JTable(data, columnNames);
只是为了澄清可能让你困惑的一些事情(整个代码大约有800行)。myStudent
是来自 student
类的特定对象,而 Courses
则是一个 course
类的 ArrayList
(course
类具有一些属性,我使用 getter 设置了特定课程信息的数据)。我想要添加 JCheckBox
列到我的表格中,原因是:如果用户通过复选框选择了某些课程(假设有4门课程),然后按下表格下方的按钮,我希望动态地从表格中移除该课程(行)。
英文:
I wanted to add a JCheckBox
column to my JTable
without using models. I searched and found some solutions like using models but they are not what I wanted because my project is at its latest stage and might provide some problems and I don't know how to use Models as well. If anyone can help me find a solution I'll be grateful.
for (int cnt = 0; myStudent.currentCourses[cnt] != 0; cnt++){
for (course c : Courses) {
if (myStudent.currentCourses[cnt] == c.getCourseNum() && myStudent.groupNum[cnt] == c.getGroupsContained()) {
data[cnt][0] = String.valueOf(c.getCourseNum());
data[cnt][1] = String.valueOf(myStudent.groupNum[cnt]);
data[cnt][2] = String.valueOf(c.getUnits());
data[cnt][3] = c.getCourseName();
data[cnt][4] = String.valueOf(c.getSignedNum());
data[cnt][5] = c.getProfessorName();
data[cnt][6] = c.getExamDate();
data[cnt][7] = c.getClassSchedule();
data[cnt][8] = c.getPrerequisites();
data[cnt][9] = c.getExtraInfo();
}
}
}
courseTable = new JTable(data, columnNames);
Just to clarify some things that might confuse you (the whole code is about 800 lines). myStudent
is an specific object from a student
class and Courses
is an ArrayList
of a course
class (the course
class has some properties and I set my data with that specific course information using getters). I want to add JCheckBox
column to my table for this reason: if the user choose some courses by checkbox (consider we have 4 courses) and then presses a button under the table, I want to remove that course (row) from the table dynamically.
专注分享java语言的经验与见解,让所有开发者获益!
评论