调用setValueAt不会发送TableModel事件

时间:2015-10-15 19:25:34

标签: java swing jtable tablemodel

我写了一个从db获取数据的个人TableModel。 问题是当我修改表中的数据时,我可以看到效果 在桌子上,但没有桌面活动成功。这似乎是方法 setValueAt没有发生事件,但我可以看到他更新了表格的单元格。 很明显,我在主程序中添加了这些代码。

我添加了这些以表明它不起作用

Stabili.java(来自db的数据来源)

package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(catalog = "", schema = "ROOT")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Stabili.findAll", query = "SELECT s FROM Stabili s"),})
public class Stabili implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(nullable = false)
private Long id;
@Basic(optional = false)
@Column(nullable = false, length = 50)
private String indirizzo;

public Stabili() {
}

public Stabili(Long id) {
    this.id = id;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getIndirizzo() {
    return indirizzo;
}

public void setIndirizzo(String indirizzo) {
    this.indirizzo = indirizzo;
}
@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    if (!(object instanceof Stabili)) {
        return false;
    }
    Stabili other = (Stabili) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.Stabili[ id=" + id + " ]";
}
}

// tablemodel StabiliTableModel.java

package prova;

import entity.Stabili;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.swing.event.EventListenerList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class StabiliTableModel implements TableModel, TableModelListener{

protected EventListenerList listenerList = new EventListenerList();
private final EntityManager em;
private final TypedQuery<Stabili> qry;
private final List<Stabili> results;

public StabiliTableModel(EntityManager em){
    this.em = em;
    qry = em.createNamedQuery("Stabili.findAll",Stabili.class);
    results = qry.getResultList();
}

@Override
public int getRowCount() {
    return results.size();
}

@Override
public int getColumnCount() {
    return 2;
}

@Override
public String getColumnName(int columnIndex) {
    switch (columnIndex){
        case 0:
            return "Id";
        case 1:
            return "Indirizzo";
    }
}

@Override
public Class<?> getColumnClass(int columnIndex) {
    return getValueAt(0, columnIndex).getClass();
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    switch (columnIndex){
                case 0:
                    return results.get(rowIndex).getId();
                case 1:
                    return results.get(rowIndex).getIndirizzo();
    }
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    switch (columnIndex){
        case 1:
            results.get(rowIndex).setIndirizzo(aValue.toString());
    }
    fireTableCellUpdated(rowIndex,columnIndex);
}

@Override
public void addTableModelListener(TableModelListener l) {
}

@Override
public void removeTableModelListener(TableModelListener l) {
}

@Override
public void tableChanged(TableModelEvent e) {
    System.out.println(e.getColumn());
}

public void fireTableDataChanged(){
  fireTableChanged(new TableModelEvent(this, 0, Integer.MAX_VALUE));
}
public void fireTableStructureChanged(){
   fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
}

public void fireTableRowsInserted(int firstRow, int lastRow){
      fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
                                           TableModelEvent.ALL_COLUMNS,
                                           TableModelEvent.INSERT));
}

public void fireTableRowsUpdated(int firstRow, int lastRow){
    fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
                                       TableModelEvent.ALL_COLUMNS,
                                       TableModelEvent.UPDATE));
}


public void fireTableRowsDeleted(int firstRow, int lastRow)
{
  fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
                                       TableModelEvent.ALL_COLUMNS,
                                       TableModelEvent.DELETE));
}


public void fireTableCellUpdated(int row, int column)
{
  fireTableChanged(new TableModelEvent(this, row, row, column));
}

public void fireTableChanged(TableModelEvent event){
    int    index;
    TableModelListener listener;
    Object[] list = listenerList.getListenerList();
    for (index = 0; index < list.length; index += 2){
      listener = (TableModelListener) list [index + 1];
      listener.tableChanged(event);
    }
}

}

//最后是主

package prova;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
 import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;


public class Prova extends JFrame{

private JTable mainTable,fixedTable;
private JScrollPane scrollPane;
private JTableHeader mainTableHeader;
private TableColumnModel originalColumnModel,mainColumnModel,fixedColumnModel;
private TableColumn[] columns;
private int ncols,counter;
private EntityManager em;

public Prova(){

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("provaPU");
    em = emf.createEntityManager();

    StabiliTableModel mainTableModel = new StabiliTableModel(em);

    scrollPane = new JScrollPane();

    mainTable = new JTable(mainTableModel);

    mainTableModel.addTableModelListener(mainTableModel);
    scrollPane.setViewportView(mainTable);
    add(scrollPane, BorderLayout.CENTER);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
@Override
        public void run() {
    new Prova().setVisible(true);
        }
});
}
}

1 个答案:

答案 0 :(得分:1)

根据建议herepreferred approach会在TableModelEvent的实施中扩展AbstractTableModelfire合适的setValueAt()DefaultTableModel就是一个例子。

  

希望扩展AbtractTableModel

一些替代方案:

  • 委派AbtractTableModelStabiliTableModel的子类。

  • 添加EventListenerList的实例作为StabiliTableModel的成员,并使用EventListenerList API中列出的方案触发您自己的活动。

  

没有什么可以解雇这个事件,但我不知道是什么。

完全;您需要使用现有的事件机制或创建自己的事件。