Java中的JTable TableModel问题

发布于 2024-07-25 13:59:01 字数 3399 浏览 5 评论 0原文

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

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;

}

表模型

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

I can show my data in a JTable without a problem, but when I want to filter while my app is running, the JTable is not showing me data changes. I searched for it and found a class named TableModel but I can't write my AbstractTableModel. Can anyone show me how I can do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野心澎湃 2024-08-01 13:59:01

我建议阅读这篇如何使用表格( 基本上,

TableModel 必须通过触发适当的事件来通知表更改的数据。 请参阅此处

I suggest reading this How to Use Tables (from the Java Tutorials Using Swing Components)

Basically the TableModel has to notify the Table of changed data by firing the appropriate Events. See here

苏璃陌 2024-08-01 13:59:01

有一个非常好的库,称为 GlazedLists,它使使用列表和表格变得更加简单,包括列排序和行过滤。

它绝对值得一看。

http://publicobject.com/glazedlists/

HTH

There is a very good library called GlazedLists that makes it a lot simpler to work with lists and tables, including column sorting and row filtering.

Its definitely worth taking a look.

http://publicobject.com/glazedlists/

HTH

人海汹涌 2024-08-01 13:59:01

您应该利用 JTable 实现的 TableModelListener 接口。 将表添加到 TableModel 后,调用 AbstractTableModel 实现的相应 fireTableChanged() 类型事件。 这应该会强制您的 JTable 进行更新。

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

You should utilize the TableModelListener interface, which your JTable implements. Once you add your table to your TableModel, call the appropriate fireTableChanged()-type event that AbstractTableModel implements. This should force your JTable to update.

You will still need to implement a method to reset your data in your model when your filter operation returns. it should be in this method that you call your fireTableChanged() event. you also should ensure that you are in the AWT thread when you fire the table changed event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文