Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set hidden field values in a java swing Jtable

Tags:

java

swing

jtable

i've created a swing jtable with some data from a database. in web apps, usually i display the data to the user and set it's unique database id in as an html tag attribute, so that when the user clicks on say edit, i pick the element's hidden db unique id from the html tag attribute using javascript. That way, i know which data user wants to edit and i can update it in the database using it's unique primary key. Now how do i do this in a desktop app writen in java using swing. Put it more clearly, am looking for an equivalent of;

<table>
<tr id=1 ><td>david</td></tr>
<tr id=2 ><td>peter</td></tr>
<tr id=3 ><td>Timothy</td></tr>
</table>

Hope am clear. Thanks

like image 898
David Okwii Avatar asked Feb 03 '26 13:02

David Okwii


1 Answers

Your TableModel which backs up the JTable can contain anything you like. It can for example contain objects like

class User{
  public final int ID;
  public String name;
  public int age;
}

and you can then choose to only include certain values in your JTable

class MyTableModel implements TableModel{
  private List<User> users;
  @Override
  public Object getValueAt(int row, int column){
    if ( column == 0 ){
      return users.get( row ).name;
    }
  }
}

But since your TableModel still contains the full User objects, you have all the required information.

Note: the above code will not compile due to missing methods, ... . It is just here to illustrate what I mean

like image 120
Robin Avatar answered Feb 06 '26 02:02

Robin