1

我可以JTable毫无问题地显示我的数据,但是当我想在我的应用程序运行时进行过滤时,JTable并没有显示我的数据更改。我搜索它并找到了一个名为 TableModel 的类,但我无法编写我的 AbstractTableModel。谁能告诉我如何做到这一点?

个人化Java

package deneme.persistence;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author İbrahim AKGÜN
 */
@Entity
@Table(name = "PERSONELZ", catalog = "tksDB", schema = "dbo")
@NamedQueries({@NamedQuery(name = "Personelz.findAll", query = "SELECT p FROM Personelz p"), @NamedQuery(name = "Personelz.findByPersonelıd", query = "SELECT p FROM Personelz p WHERE p.personelıd = :personelıd"), @NamedQuery(name = "Personelz.findByAd", query = "SELECT p FROM Personelz p WHERE p.ad = :ad"), @NamedQuery(name = "Personelz.findBySoyad", query = "SELECT p FROM Personelz p WHERE p.soyad = :soyad")})
public class Personelz implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Basic(optional = false)
    @Column(name = "PERSONELID", nullable = false )
    private Integer personelıd;
    @Column(name = "AD", length = 50)
    private String ad;
    @Column(name = "SOYAD", length = 50)
    private String soyad;
    @Column(name = "YAS")
    private Integer yas;
    public Personelz() {
    }

    public Personelz(Integer personelıd) {
        this.personelıd = personelıd;
    }

    public Integer getPersonelıd() {
        return personelıd;
    }

    public void setPersonelıd(Integer personelıd) {
        this.personelıd = personelıd;
    }

    public String getAd() {
        return ad;
    }

    public void setAd(String ad) {
        String oldAd = this.ad;
        this.ad = ad;
        changeSupport.firePropertyChange("ad", oldAd, ad);
    }

    public String getSoyad() {
        return soyad;
    }

    public void setSoyad(String soyad) {
        String oldSoyad = this.soyad;
        this.soyad = soyad;
        changeSupport.firePropertyChange("soyad", oldSoyad, soyad);
    }
     public Integer getYas() {
        return yas;
    }
public void setYas(Integer yas){
this.yas = yas;

}

表模型

 public class TableModel extends AbstractTableModel {

String[] headers;
List<Personelz> personel;
int row;
int column;
Object[][] per;
/** Creates a new instance of TableModel */
    @SuppressWarnings("empty-statement")
public TableModel(List<Personelz> p) {

    this.personel = p;
    column=2;
    row=this.personel.size();

    headers=new String[column];
    headers[0]="AD";
    headers[1]="SOYAD";
    per={p.toArray(),p.toArray()};

}

 public int getColumnCount()
 {
     return column;
 }
 public int getRowCount()
 {
     return row;
 }
 public Object getValueAt(int rowIndex, int kolonindex)
 {

     return per[rowIndex][kolonindex];
 }
 public String getColumnName(int i)
 {
     return headers[i];
 }
4

3 回答 3

5

我建议阅读这篇如何使用表格(来自使用 Swing 组件的 Java 教程)

基本上,TableModel 必须通过触发适当的事件来通知表更改的数据。看这里

于 2009-06-16T22:40:58.540 回答
2

有一个非常好的库,叫做 GlazedLists,它可以让处理列表和表格变得更加简单,包括列排序和行过滤。

它绝对值得一看。

http://publicobject.com/glazedlists/

高温高压

于 2009-06-17T09:36:35.637 回答
1

您应该使用您实现的TableModelListener接口。JTable将表添加到 TableModel 后,调用实现的适当的fireTableChanged()-type 事件AbstractTableModel。这应该会迫使您JTable进行更新。

当您的过滤操作返回时,您仍然需要实现一种方法来重置模型中的数据。您应该在此方法中调用您的fireTableChanged()事件。您还应该确保您在触发表更改事件时处于 AWT 线程中。

于 2009-06-16T23:21:06.883 回答