Java TableModel无法更新? [英] Java TableModel not updating?

查看:304
本文介绍了Java TableModel无法更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

我通过@MadProgrammer找到了这个答案,并且有效!

I found this answer by @MadProgrammer and it worked!

在单击按钮时更新JTable

基本上,您可以在TableModel中添加一个refresh()方法,以接收更新的Object.然后,每次按下按钮,都会调用tablemodel的此方法.

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.

我假设您还可以扩展JTable并将此方法放入扩展类中. 也许会有更有效的解决方案,但我现在会坚持使用这一解决方案. 谢谢!

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!

/************************************************** **********************************/

/****************************************************************************/

我正在用Java制作表格以显示"DinnerTable"类中的Objects值.我构建了一个自定义的DinnerTableModel类,然后扩展了AbstractTableModel并实现了TableModel.但是,我似乎在修改原始的"DinnerTable"对象时,tableModel中的相应对象完全没有改变.我用谷歌搜索解决此问题的方法是创建一个TableModelListener.但是,我还有另一个JTable,其中没有此侦听器,但是它工作得很好.这些表之间的主要区别在于,导致这些对象的值发生变化的JButton与工作表是同一Tab,但与JTabbedPane中非工作的DinnerTable处于不同的选项卡.更新那些DinnerTableModels的有效方法是什么? (我将需要38个).谢谢!

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");
    }
}
}

//////////

推荐答案

TableModel应该存储数据.然后,当数据更改时,您将调用AbstractTableModel的适当fireXXX(...)方法来告诉表重新绘制自身.

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.

然后通过TableModel进行数据更改,TableModel将更新您的自定义对象.

Then changes to the data are made through the TableModel and the TableModel will update your custom object.

这基本上意味着DinnerTable类的数据结构应该是DinnerTableModel的一部分,而不是单独的类.

This basically means that the DinnerTable class data structures should be part of the DinnerTableModel, not a separate class.

有关步骤,请参见行表模型为自定义对象创建自定义TableModel的步骤指南.

See Row Table Model for a step by step guide on creating a custom TableModel for a custom Object.

这篇关于Java TableModel无法更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆