2009-06-16 72 views
1

我可以在沒有問題的情況下在JTable中顯示我的數據,但是當我想在應用程序運行時進行過濾時,JTable未顯示數據更改。我搜索了它,發現了一個名爲TableModel的類,但我無法寫出我的AbstractTableModel。任何人都可以告訴我我該如何做到這一點?Java中的JTable TableModel問題

Personelz.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; 

} 

的TableModel

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

回答

5

我建議您閱讀(使用Java教程Swing組件)這個How to Use Tables

基本上爲TableModel有通知的變化表數據通過發射適當的事件。請參閱here

+0

抖動是正確的。 JTable監聽TableModel。 TableModel在其狀態發生更改時觸發TableModelEvent到其偵聽器。您可以擴展AbstractTableModel或使用DefaultTableModel,它有一些支持來管理偵聽器並觸發事件。 – 2009-06-16 22:44:43

1

您應該使用您的JTable實現的TableModelListener接口。將表添加到TableModel後,請撥打AbstractTableModel實施的適當的fireTableChanged()型事件。這應該強制您的JTable更新。

當您的過濾器操作返回時,仍然需要實施一種方法來重置模型中的數據。應該用這種方法,你可以打電話給你的fireTableChanged()事件。您還應該確保在觸發表更改的事件時處於AWT線程中。

2

有一個名爲GlazedLists的非常好的庫,它使得使用列表和表格(包括列排序和行過濾)變得簡單很多。

它絕對值得一看。

http://publicobject.com/glazedlists/

HTH