java - 为什么调用 fireTableDataChanged() 方法时我的 ListSelectionListener 总是被触发

标签 java swing jtable listener listselectionlistener

我有一个扩展 AbstractTableModel 的 jtable。我注册了两个 ListSelectionListener,它们获取所选单元格的行和列。但是,当我调用模型中定义的 updateData() 方法(该方法又调用 fireTableDataChanged() 方法)时,两个 ListSelectionListener 被触发,并且它们获取行和列值 -1,这是我使用它们进行索引时的异常。有没有办法阻止 fireTableDataChanged() 方法触发两个监听器?或者是否有其他方法可以更新表数据而不触发注册到模型的所有监听器?

这是代码:

 public class MonthTableModel extends AbstractTableModel {

/**
 * 
 */
private static final long serialVersionUID = 1L;
//private int currentYear, currentMonth;
private String[] columnName = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return 7;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return 6;
}

    public void updateData(int y, int m)
{
    /**
            do something to change the data of the table
            */
    fireTableDataChanged();
}
public String getColumnName(int column)
{

    return columnName[column];
}
@Override

public Object getValueAt(int row, int column) {
    // TODO Auto-generated method stub
    if ((row >= 0 && row < 6) && (column < 7 && column >= 0))
        return "CalendarDate";
    return "";
}


public boolean isCellEditable(int row, int col) 
   {
      return false;
   }

TabelMonthModel jTableMonthTable = new TableMonthModel();

SelectionListener listener = new SelectionListener(jTableMonthTable);

jTableMonthTable.getSelectionModel().addListSelectionListener(listener);
      jTableMonthTable.getColumnModel().getSelectionModel().addListSelectionListener(listener);


class SelectionListener implements ListSelectionListener  {
        JTable jtable;
        public RowSelectionListener(JTable j)
        {
            jtable = j;
        }
        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
                   if (!e.getValueIsAdjusting())
                   { 
            int rowIndex = jtable.getSelectedRow();
            int colIndex = jtable.getSelectedColumn();
            System.out.println("row: " + rowIndex + "\n");
                        System.out.println("col: " + colIndex + "\n");
                   }
        }


    }

感谢您的帮助

最佳答案

the two ListSelectionListeners get triggered and they get row and column value -1, which is an exception when I use them to index

我猜想该事件会生成,因为您删除了所有数据,因此需要将选择设置为 -1 表示不再选择任何单元格。

您的代码应该检查 -1 并且不进行处理。

另一个选项是在调用 updateData() 方法之前删除监听器,然后在模型更新后添加监听器。

关于java - 为什么调用 fireTableDataChanged() 方法时我的 ListSelectionListener 总是被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19532342/

相关文章:

java - 不使用过滤器设置响应头 - RESTeasy

java - Web 服务响应包含无效的 XML 字符

java - 生产期间的 Logcat

java - 正确显示带线程的 Sprite

java - 如何为 JTable 中的单元格着色?

java - 由于空指针异常,无法从 JTable 获取数据

JavaMail 附件和正文问题

使用 GridBagLayout 调整 Java Swing 组件的大小

c# - Java Swing 与 C#.net 桌面数据库应用程序

java - 连接到 JDBC 时使用 getColumnClass() 对 JTable 进行排序