- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[更新!!!]这是成品:
:)
因此,我正在开发一个程序,帮助我组织和打印晚餐座位。我有一个学生类,其中包含名字、姓氏等实例变量。我还有一个 DiningTable 类,其中包含学生列表和字符串教师名。我创建了一个 GUI,以便更轻松地分配不同的座位。我已经在 JTabbedPane 上有一个选项卡,用于组织学生信息(如果可用或不可用)并随机排列他们所在的 table 。现在,为了更好地可视化表格并更轻松地将每个学生分配到特定的座位,我需要制作如下内容: 我想使用 TableModel 中的实际 DiningTable 对象,以便每当我在此框架上编辑某些内容时,所做的更改都会转换为对象。但是,我不太确定该怎么做。我应该:
1.创建嵌套在更大 JTable 的单元格中的表(每个 DiningTable 一个 JTable)?但是我怎样才能执行诸如在晚餐 table 之间滑动学生之类的操作呢?或者2.在GridLayout或GridBagLayout中对齐JTable?但话又说回来,我怎样才能交换学生呢?
谢谢! 打包晚餐列表;
public class Student
{
private String lastName;
private String firstName;
private int grade;
private int table;
private boolean gender;//Male=true, female=false;
private boolean available; //true=available;
public Student()
{
lastName="";
firstName="";
grade=0;
table=0;
gender=true;
available=true;
}
public Student(String l, String f, int i, boolean g, boolean a)
{
lastName=l;
firstName=f;
grade=i;
gender=g;
available=a;
//table is not written back to the txt.
table=0;
}
//Getters
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int getGrade()
{
return grade;
}
public int getTable()
{
return table;
}
public boolean getGender()
{
return gender;
}
public boolean getAvailable()
{
return available;
}
//Setters
public void setLastName(String s)
{
this.lastName=s;
}
public void setFirstName(String s)
{
firstName=s;
}
public void setGrade(int i)
{
grade=i;
}
public void setTable(int hiahia)
{
table=hiahia;
}
public void setGender(boolean b)
{
gender=b;
}
public void setAvailable(boolean b)
{
available=b;
}
//Miscellaneous
public String toString()
{
String a="";
a=lastName+","+firstName+","+Integer.toString(grade)+","+Boolean.toString(gender)+","+Boolean.toString(available);
return a;
}
}
/////////////////////////////////////
package DinnerList;
import java.util.ArrayList;
import java.util.List;
public class DinnerTable
{
private List<Student> members= new ArrayList<Student>();
private int tableNumber=0;
private int capacity=0;
private String teacherName="";
private boolean available=true;
public DinnerTable(int a, int b, String c, boolean d)
{
tableNumber=a;
capacity=b;
teacherName=c;
available=d;
}
public void setTableNumber(int a) {tableNumber=a;}
public void setCapacity(int a) {capacity=a;}
public void setTeacherName(String a) {teacherName=a;}
public void setAvailable(boolean b) {available=b;}
public void add(Student s)
{
if(available&&(members.size()<capacity))
{ this.members.add(s); }
else if(!available)
{ System.out.println("Adding Student failed, table "+tableNumber+" not available");}
else
{ System.out.println("Adding Student failed, table "+tableNumber+" is full");}
}
public int getTableNumber()
{return tableNumber;}
public int getCapacity() {return this.capacity;}
public String getTeacherName() {return teacherName;}
public boolean getAvailable() {return available;}
public List<Student> getMembers(){return members;}
public void remove(Student s)
{
if(members.contains(s))
{
members.remove(s);
}
else
{
System.out.println("Failed to remove student from table because it wasn't there");
}
}
}
//////////////////////
package DinnerList;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class DinnerTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Object huahua =table.getModel().getValueAt(row, column);
String ppp="";
if(huahua!=null)
{
if(huahua instanceof Student)
{
ppp=((Student) huahua).getLastName()+", "+((Student)huahua).getFirstName();
}
else if(huahua instanceof String)
{
ppp=(String)huahua;
}
else
{
System.out.println("Error: DinnerTableCellRenderer intakes unknown data type");
}
}
else
{
ppp="";
}
JLabel label = (JLabel)super.getTableCellRendererComponent(table, ppp,isSelected, hasFocus,row, column);
return label;
}
}
//////////
package DinnerList;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class DinnerTableModel extends AbstractTableModel implements TableModel
{
private final String[] columnNames={"","","",""};
private List<DinnerTable> tableCollection= new ArrayList<DinnerTable>();
public DinnerTableModel(List<DinnerTable> huhu)
{
tableCollection.addAll(huhu);
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
if(tableCollection.size()%4==0)
{
return tableCollection.size()/4;
}
else
{
return (int)(tableCollection.size()/4)+1;
}
}
public String getColumnName(int col)
{
return columnNames[col];
}
public DinnerTable getTableAt(int row, int column)
{
if(tableCollection.size()>=(Integer)((row)/8)+column+1)
{
return tableCollection.get((Integer)((row)/8)+column);
}
else
{
return null;
}
}
public Object getValueAt(int rowIndex, int colIndex)
{
DinnerTable dd= this.getTableAt(rowIndex, colIndex);
String ss= "";
if(dd==null)
{
return "";
}
else if(rowIndex%8==0)
{
return (dd.getTableNumber()+". "+dd.getTeacherName());
}
else if(dd.getMembers().size()>=rowIndex%8)
{
return dd.getMembers().get(rowIndex%8);
}
else
{
return "";
}
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col)
{
if(this.getValueAt(row, col) instanceof Student)
{
return true;
}
else
{
return false;
}
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
{
if(aValue instanceof Student)
{
if(rowIndex%8!=0)
{
if(null!=this.getTableAt(rowIndex,columnIndex))
{
this.getTableAt(rowIndex,columnIndex).getMembers().set(rowIndex%8-1, (Student)aValue);
}
else
{
System.out.println("error: Attempting to put student in nonexistent table in table list gui");
}
}
else
{
System.out.println("error: Attempting to put student in a string in table list gui");
}
}
else if(aValue instanceof String)
{
System.out.println("error: Attempting to change teacher name in tablelist gui");
}
else
{
System.out.println("error: Attempting to set unknown object type in tablelist gui");
}
}
}
////****************////
JTable tableTable= new JTable(new DinnerTableModel(tables));
for(int hihihi=0;hihihi<tableTable.getColumnCount();hihihi++)
{
tableTable.getColumnModel().getColumn(hihihi).setCellRenderer(new DinnerTableCellRenderer());
}
JScrollPane scrollpaneB1= new JScrollPane();
scrollpaneB1.add(tableTable);
panelB.add(scrollpaneB1);
最佳答案
I am new to programming and have a lot of questions. It is very nice of you to be so patient! : ) However, I have another JTable that intakes another TableModel class. I ran tests and it seems that the model does change whenever the original objects change, even though I didn't use any propertychangelisteners. This is very confusing to me...
JTable
是 API 中较复杂的组件之一(JTree
和文本组件是下一个),如果您能熟悉它,您就会要熟悉整个 API,还有很长的路要走。
有多种方法可以通知 TableModel
对象已更改,但最好的方法之一是以解耦的方式执行此操作,这样您就不必维护在 Student
对象可能发生变化的每个位置,对 TableModel
的引用。
这表明 Student
对象本身应该生成某种更改事件,感兴趣的各方(如 TabelModel
)可以监听这些事件并在事件发生时采取行动。
通过这种方式,您可以在代码中的任何位置更改 Student
对象,而无需关心谁可能需要知道该对象已更改。
现在,这基本上可以归结为观察者模式,Swing 在其监听器中实现了这种功能,ChangeListener
可能是最直接的想法,但 PropertyChangeListener
是一个更强大的选项,因为它根据已更改的属性生成事件。
在某些情况下,您可能不会关心哪些属性已更改,但对于 TableModel
来说,它非常有用。
这是使用两个表对该概念的简单演示。您可以编辑任一表中的任何值,提交该值时,对应的表将被更新。现在,您还可以在代码中修改 Student
的实例,并且会得到相同的结果,但这是一个创建的简单示例。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Student student = new Student("Skywalker", "Luke", 0, true, true);
StudentTabelModel leftModel = new StudentTabelModel(student);
StudentTabelModel rightModel = new StudentTabelModel(student);
JTable leftTable = new JTable(leftModel);
leftTable.setGridColor(Color.GRAY);
JTable rightTable = new JTable(rightModel);
rightTable.setGridColor(Color.GRAY);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 2));
frame.add(new JScrollPane(leftTable));
frame.add(new JScrollPane(rightTable));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class StudentTabelModel extends AbstractTableModel {
private List<Student> students = new ArrayList<>(25);
private PropertyChangeListener propertyChangeListener;
public StudentTabelModel(Student... students) {
propertyChangeListener = new StudentPropertyChangeListener();
add(students);
}
public void add(Student... students) {
if (students != null && students.length > 0) {
int startRow = this.students.size();
for (Student student : students) {
student.addPropertyChangeListener(propertyChangeListener);
this.students.add(student);
}
fireTableRowsInserted(startRow, this.students.size() - 1);
}
}
public void remove(Student ...students) {
if (students != null && students.length > 0) {
for (Student student : students) {
int index = this.students.indexOf(student);
if (index != -1) {
student.removePropertyChangeListener(propertyChangeListener);
this.students.remove(student);
fireTableRowsDeleted(index, index);
}
}
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public int getRowCount() {
return students.size();
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
case 1:
return String.class;
case 2:
case 3:
return Integer.class;
case 4:
case 5:
return Boolean.class;
}
return Object.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = students.get(rowIndex);
switch (columnIndex) {
case 0:
return student.getLastName();
case 1:
return student.getFirstName();
case 2:
return student.getGrade();
case 3:
return student.getTable();
case 4:
return student.getGender();
case 5:
return student.getAvailable();
}
return "??";
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Student student = students.get(rowIndex);
switch (columnIndex) {
case 0:
student.setLastName(aValue == null ? null : aValue.toString());
break;
case 1:
student.setFirstName(aValue == null ? null : aValue.toString());
break;
case 2:
if (aValue instanceof Integer) {
student.setGrade((Integer) aValue);
}
break;
case 3:
if (aValue instanceof Integer) {
student.setTable((Integer) aValue);
}
break;
case 4:
if (aValue instanceof Boolean) {
student.setGender((Boolean) aValue);
}
break;
case 5:
if (aValue instanceof Boolean) {
student.setAvailable((Boolean) aValue);
}
break;
}
}
protected class StudentPropertyChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() instanceof Student) {
Student student = (Student) evt.getSource();
int row = students.indexOf(student);
if (row != -1) {
switch (evt.getPropertyName()) {
case "lastName":
fireTableCellUpdated(row, 0);
break;
case "firstName":
fireTableCellUpdated(row, 1);
break;
case "grade":
fireTableCellUpdated(row, 2);
break;
case "table":
fireTableCellUpdated(row, 3);
break;
case "gender":
fireTableCellUpdated(row, 4);
break;
case "avaliable":
fireTableCellUpdated(row, 5);
break;
}
}
}
}
}
}
public class Student {
private String lastName;
private String firstName;
private int grade;
private int table;
private boolean gender;//Male=true, female=false;
private boolean available; //true=available;
private PropertyChangeSupport propertyChangeSupport;
public Student() {
this("", "", 0, false, true);
}
public Student(String l, String f, int i, boolean g, boolean a) {
lastName = l;
firstName = f;
grade = i;
gender = g;
available = a;
//table is not written back to the txt.
table = 0;
propertyChangeSupport = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
//Getters
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public int getGrade() {
return grade;
}
public int getTable() {
return table;
}
public boolean getGender() {
return gender;
}
public boolean getAvailable() {
return available;
}
//Setters
public void setLastName(String s) {
String old = lastName;
this.lastName = s;
propertyChangeSupport.firePropertyChange("lastName", old, lastName);
}
public void setFirstName(String s) {
String old = firstName;
firstName = s;
propertyChangeSupport.firePropertyChange("firstName", old, firstName);
}
public void setGrade(int i) {
int old = grade;
grade = i;
propertyChangeSupport.firePropertyChange("grade", old, grade);
}
public void setTable(int hiahia) {
int old = table;
table = hiahia;
propertyChangeSupport.firePropertyChange("table", old, table);
}
public void setGender(boolean b) {
boolean old = gender;
gender = b;
propertyChangeSupport.firePropertyChange("gender", old, gender);
}
public void setAvailable(boolean b) {
boolean old = available;
available = b;
propertyChangeSupport.firePropertyChange("available", old, available);
}
//Miscellaneous
@Override
public String toString() {
String a = "";
a = lastName + "," + firstName + "," + Integer.toString(grade) + "," + Boolean.toString(gender) + "," + Boolean.toString(available);
return a;
}
}
}
请记住,如果您创建 Student
对象的两个不同实例并期望一个实例中的更改得到反射(reflect),则这依赖于共享的 Student
实例在另一个,那么你会失望的
关于Java GUI 创建 JTables 表的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46333565/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!