简体   繁体   English

将 TableModelListener 添加到 AbstractTableModel

[英]Adding TableModelListener to AbstractTableModel

I've faced an issue in adding TableModelListener to AbstractTableModel .我在将TableModelListener添加到AbstractTableModel时遇到了问题。 The program stops working and the JFrame doesn't response for any button-clicking or even closing the JFrame.程序停止工作,JFrame 没有响应任何按钮单击甚至关闭 JFrame。

What I want is to make a JButton enabled iff the number of rows in myTable is equal or more than 2 rows.我想要的是启用JButton ,如果 myTable 中的行数等于或超过 2 行。

Here is my code...这是我的代码...

My custom Table:我的自定义表:

public class MyTable extends JPanel
{
    public Main main;
    public ArrayList<MyData> lstData;
    public JTable table;
    public MyTableModel model;
    // ...

    public MyTable(ArrayList<MyData> lstData, Main main)
    {
        this.lstData = lstData;
        this.main = main;
        model = new MyTableModel(lstData);
        table = new JTable(model);
        // ...
    }

    // ...

    public int getTableSize()
    {
        return model.getRowCount();
    }
    public TableModel getModel()
    {
        return model;
    }

    public class MyTableModel extends AbstractTableModel
    {  
        protected String[] columnNames = new String[ ] {"#","Name", "Phone Number"};
        protected ArrayList<MyData> lstData;
        protected Class[] types = new Class[]{String.class, String.class, String.class};

        public MyTableModel(ArrayList<MyData> lstData)
        {    this.lstData = lstData;    }

        public void SetData(ArrayList<MyData> lstData)
        {    this.lstData = lstData; fireTableDataChanged();    }

        @Override
        public String getColumnName(int columnIndex)
        {    return columnNames[columnIndex];   }

        @Override
        public Class getColumnClass(int columnIndex)
        {    return types[columnIndex];   }

        public Object getValueAt(int row, int column)
        {
            if (row < 0 || row > lstData.size()) return null;
            MyData obj = lstData.get(row);
            switch(column)
            {
                case 0: return obj.getID();
                case 1: return obj.getName();
                case 2: return obj.getPhoneNumber();
                default: return null;
            }
        }

        public int getRowCount() {  return lstData.size();   }
        public int getColumnCount() {  return columnNames.length;   }

    }
}

Main class:主class:

public class Main extends JFrame implements TableModelListener
{
    public static ArrayList<myData> lstData;
    public static MyTable table;
    public static JButton myButton;
    public Main()
    {
        // ...
        table = new MyTable(lstData, this);
        table.getModel().addTableModelListener(this);
        myButton = new JButton();
        myButton.setEnabled(false);
        // ...
    }

    // ...

    public void tableChanged(TableModelEvent e)
    {
       int firstRow = e.getFirstRow();
       int lastRow = e.getLastRow();
       int mColIndex = e.getColumn();

       switch(e.getType())
       {
           case TableModelEvent.INSERT:
           if(table.getTableSize() >= 2) myButton.setEnabled(true);
           else myButton.setEnabled(false);
           break;

           case TableModelEvent.DELETE:
           if(table.getTableSize() >= 2) myButton.setEnabled(true);
           else myButton.setEnabled(false);
           break;
       }
    }
}

Could you help me to solve this issue?你能帮我解决这个问题吗? Thanks in advance.提前致谢。

EDIT:编辑:

The GUI stop responding only if I add or delete elements from the table.仅当我从表中添加或删除元素时,GUI 才会停止响应。

EDIT2:编辑2:

No errors or exceptions are thrown after I add elements to the table, it's just freezing the gui with no response将元素添加到表后没有抛出任何错误或异常,它只是冻结 gui 没有响应

In your MyTableModel class, remove the following line:在您的 MyTableModel class 中,删除以下行:

protected TableModel model = this;

And also remove the following methods:并删除以下方法:

public void setModel(TableModel model){
    this.model = model;
}

public TableModel getModel() {
    return model;
}

You are already implementing a custom table model, there is no need to create that self reference inside of it.您已经在实现自定义表 model,无需在其中创建自引用。 When your class is getting instantiated the this variable is not fully initialized and I suspect that is what is causing problems for you.当你的 class 被实例化时,这个变量没有完全初始化,我怀疑这就是给你带来问题的原因。 But in any case the code is definitely not needed.但无论如何,绝对不需要代码。 Also, in your MyTable class I would recommend changing the getModel() function to defer to your wrapped table variable.此外,在您的 MyTable class 中,我建议更改 getModel() function 以遵循您的包装表变量。 Like so:像这样:

public TableModel getModel() {
    return model.getModel();
}

basic tutorial about TableModelListener here or here or here关于TableModelListener的基本教程这里这里这里

best would be camickr Table Cell Listener that implements deepest funcionalities for Listening in the TableCell最好是 camickr Table Cell Listener ,它实现了 TableCell 中侦听的最深功能

Thank you guys for your help, I solve this issue by modifying the tableChanged method:谢谢大家的帮助,我通过修改tableChanged方法解决了这个问题:

public void tableChanged(TableModelEvent e)
{
   if(table.getTableSize() >= 2) myButton.setEnabled(true);
       else myButton.setEnabled(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM