问题
UPDATE:
I found this answer by @MadProgrammer and it worked!
Updating JTable on button click
Basically, you add a refresh() method to your TableModel that takes in the updated Object. Then, every time the button is pressed, this method of the tablemodel is called.
I assume that you can also extend JTable and put this method inside the extended class. There might be more efficient solutions, but I will stick for this one for now. Thank you!
/****************************************************************************/
I am making tables in java to display values of Objects from class "DinnerTable". I built a custom DinnerTableModel class than extends AbstractTableModel and implements TableModel. However, I seems that when my modify my original "DinnerTable" objects, the corresponding objects in the tableModel don't change at all. The solution to this issue that I googled was to create a TableModelListener. However, I also have another JTable that doesn't have this listener in it, but it works perfectly fine. The key distinctions between these tables is that the JButton that causes these objects' value to change was one the same Tab as the working table, but on a different tab from the non-working DinnerTable in the JTabbedPane. What is an efficient way of updating those DinnerTableModels? (I will need 38 of them). Thank you!
package DinnerList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class DinnerTableModel extends AbstractTableModel implements TableModel{
private final String[] columnNames;
private DinnerTable dt;
public DinnerTableModel(DinnerTable popopo)
{
dt=popopo;
columnNames= new String[1];
columnNames[0]=popopo.getTableNumber()+". "+popopo.getTeacherName();
}
public String getColumnName(int col)
{
return columnNames[0];
}
public Object getValueAt(int rowIndex, int colIndex)
{
if(rowIndex<7)
{
System.out.println(dt.getMembers().size());
if(dt.getMembers().size()>rowIndex)
{
System.out.println("haha");
return dt.getMembers().get(rowIndex);
}
else
{
return null;
}
}
else
{
System.out.println("Error: attempting to getValue at row>=7 in DTM");
return null;
}
}
public Class getColumnClass(int c)
{
return Student.class;
}
public boolean isCellEditable(int row, int col)
{
return true;
}
public void setValueAt(Object b,int row, int col)
{
if(b instanceof Student)
{
dt.getMembers().set(row, (Student)b);
}
else
{
System.out.println("Error: Attemping to put nonstudent into student in DTM");
}
fireTableCellUpdated(row, col);
}
}
///////////////////////////////////
package DinnerList;
import java.util.ArrayList;
import java.util.List;
public class DinnerTable
{
private List<Student> members= new ArrayList<Student>(7);
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 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 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");
}
}
}
///////////
回答1:
The TableModel should store the data. Then when the data is changed you invoke the appropriate fireXXX(...) method of the AbstractTableModel to tell the table to repaint itself.
Then changes to the data are made through the TableModel and the TableModel will update your custom object.
This basically means that the DinnerTable class data structures should be part of the DinnerTableModel, not a separate class.
See Row Table Model for a step by step guide on creating a custom TableModel for a custom Object.
来源:https://stackoverflow.com/questions/46375129/java-tablemodel-not-updating