获取 JTable 中的单元格

发布于 2024-09-03 02:12:01 字数 1104 浏览 2 评论 0原文

单击该行时,如何在 JTextField 中显示 jtable 的一行

(我需要它来编辑 JTable 中的数据库)

我的表模型

 static class TableDataModel extends AbstractTableModel
{
private List nomColonnes;
private List tableau;

public TableDataModel(List nomColonnes, List tableau){
   this.nomColonnes = nomColonnes;
   majDonnees(tableau);
}
public void majDonnees(List nouvellesDonnees){
  this.tableau = nouvellesDonnees;

  fireTableDataChanged();
}

public int getRowCount(){
   return tableau.size();
}

public int getColumnCount(){
    return nomColonnes.size();
  }


  public Object getValueAt(int row, int col){
   return ((ArrayList)( tableau.get(row))).get(col);
 }

 public String getColumnName(int col){
   return nomColonnes.get(col).toString();
 }

 public Class getColumnClass(int c)
 {
   return getValueAt(0,c).getClass();
 }

 public boolean isCellEditable(int row, int col){
  return true;

 }

 public void setValueAt(Object value, int row, int col)
 {
 ((List)tableau.get(row)).set(col,value);
 fireTableCellUpdated(row, col);


  //i suppose i should update the database here
   }


  }

how to display a row of a jtable in a from of JTextField when click on the row,

( I need this to edit the data base from the JTable )

My table model

 static class TableDataModel extends AbstractTableModel
{
private List nomColonnes;
private List tableau;

public TableDataModel(List nomColonnes, List tableau){
   this.nomColonnes = nomColonnes;
   majDonnees(tableau);
}
public void majDonnees(List nouvellesDonnees){
  this.tableau = nouvellesDonnees;

  fireTableDataChanged();
}

public int getRowCount(){
   return tableau.size();
}

public int getColumnCount(){
    return nomColonnes.size();
  }


  public Object getValueAt(int row, int col){
   return ((ArrayList)( tableau.get(row))).get(col);
 }

 public String getColumnName(int col){
   return nomColonnes.get(col).toString();
 }

 public Class getColumnClass(int c)
 {
   return getValueAt(0,c).getClass();
 }

 public boolean isCellEditable(int row, int col){
  return true;

 }

 public void setValueAt(Object value, int row, int col)
 {
 ((List)tableau.get(row)).set(col,value);
 fireTableCellUpdated(row, col);


  //i suppose i should update the database here
   }


  }

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

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

发布评论

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

评论(1

耶耶耶 2024-09-10 02:12:01

使用 ListSelectionListener。每当选择一行时,您都可以使用 table.getValueAt(...) 从模型中获取给定行的数据,然后在表单的文本字段中显示数据。

Use a ListSelectionListener. Whenever a row is selected you get the data from the model for the given row using table.getValueAt(...) and then you display the data in the text field of your form.

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