gpt4 book ai didi

java - 调用 setValueAt 不会发送 TableModel 事件

转载 作者:行者123 更新时间:2023-12-01 11:03:29 25 4
gpt4 key购买 nike

我编写了一个个人TableModel,它从数据库获取数据。问题是当我修改表中的数据时,我可以看到效果在 table 上,但没有 table 事件成功。好像是这个方法setValueAt 没有发生事件,但我可以看到他更新了表格的单元格。显然我在主程序中添加了这些代码。

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

Stabili.java(数据库数据源)

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 + " ]";
}
}

//表模型 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);
}
});
}
}

最佳答案

按照建议herepreferred approach是延长AbstractTableModelfire setValueAt() 实现中合适的 TableModelEventDefaultTableModel 是一个示例。

I don't want to extend AbtractTableModel.

一些替代方案:

  • 委托(delegate)给 StabiliTableModelAbtractTableModel 的子类。

  • 添加 EventListenerList 的实例作为 StabiliTableModel 的成员,并使用 EventListenerList API 中概述的方案触发您自己的事件。

There is not something that fire the event, but I don't know what.

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

关于java - 调用 setValueAt 不会发送 TableModel 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com