Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Heatryn

Thread: JTable: row select on the basis of an object

  1. #1
    Heatryn is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default JTable: row select on the basis of an object

    I have a JTable with some objects that represent persons.

    However I have a method that selects a person from my database by matching its fingerprint against those in the database via a fingerprintsensor.

    When I have a match, the method returns the person object. Now I want to select the row in the JTable that corresponds to the person object.

    How can I do this?

    I have a custom TableModel:

    Java Code:
        class LidTableModel extends AbstractTableModel {
    
    	private static final long serialVersionUID = 1L;
    
    	List<Lid> lidList;
    
    	String[] headerList = { "Id", "Voornaam", "Achternaam", "Geslacht", "Postcode", "Gemeente", "Land", "Email", "Gsm" };
    	@SuppressWarnings("rawtypes")
    	Class[] classes = { Long.class, String.class, String.class, Character.class, String.class, String.class, String.class, String.class, String.class };
    
    	public LidTableModel(List<Lid> list) {
    	    lidList = list;
    	}
    
    	@Override
    	public int getColumnCount() {
    	    return headerList.length;
    	}
    
    	@Override
    	public int getRowCount() {
    	    return lidList.size();
    	}
    
    	@Override
    	public Class<?> getColumnClass(int arg0) {
    	    // TODO Auto-generated method stub
    	    return classes[arg0];
    	}
    
    	// this method is called to set the value of each cell
    	@Override
    	public Object getValueAt(int row, int column) {
    	    Lid entity = null;
    	    entity = lidList.get(row);
    	    switch (column) {
    	    case 0:
    		return entity.getId();
    	    case 1:
    		return entity.getPersoon().getVoornaam();
    	    case 2:
    		return entity.getPersoon().getAchternaam();
    	    case 3:
    		return entity.getPersoon().getGeslacht();
    	    case 4:
    		return entity.getPersoon().getPostcode();
    	    case 5:
    		return entity.getPersoon().getGemeente();
    	    case 6:
    		return entity.getPersoon().getLand();
    	    case 7:
    		return entity.getPersoon().getEmail();
    	    case 8:
    		return entity.getPersoon().getGsm();
    	    default:
    		return "";
    	    }
    	}
    
    	// This method will be used to display the name of columns
    	public String getColumnName(int col) {
    	    return headerList[col];
    	}
    
    	@Override
    	public void setValueAt(Object value, int row, int col) {
    	    Lid entity = null;
    	    entity = lidList.get(row);
    	    switch (col) {
    	    case 0:
    		entity.setId((Long) value);
    		break;
    	    case 1:
    		entity.getPersoon().setVoornaam((String) value);
    		break;
    	    case 2:
    		entity.getPersoon().setAchternaam((String) value);
    		break;
    	    case 3:
    		entity.getPersoon().setGeslacht((Character) value);
    		break;
    	    case 4:
    		entity.getPersoon().setPostcode((String) value);
    		break;
    	    case 5:
    		entity.getPersoon().setGemeente((String) value);
    		break;
    	    case 6:
    		entity.getPersoon().setLand((String) value);
    		break;
    	    case 7:
    		entity.getPersoon().setEmail((String) value);
    		break;
    	    case 8:
    		entity.getPersoon().setGsm((String) value);
    		break;
    	    default:
    		break;
    
    	    }
    	    fireTableCellUpdated(row, col);
    	}
    
        }

  2. #2
    Heatryn is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: JTable: row select on the basis of an object

    Found it.

    I do a for loop over all the rows in my jtable and if the rows person object equals the given object, I select that row.
    Fubarable likes this.

Similar Threads

  1. get the select row from a jtable
    By AARON22 in forum Java Applets
    Replies: 1
    Last Post: 05-21-2011, 05:32 PM
  2. Select a row in Jtable
    By pink123 in forum AWT / Swing
    Replies: 5
    Last Post: 03-21-2011, 03:04 PM
  3. Problems with Shift select in Jtable
    By kovski88 in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2010, 04:04 PM
  4. result select sql in jtable
    By herfnai in forum JDBC
    Replies: 0
    Last Post: 08-17-2008, 12:20 PM
  5. result select sql in jtable
    By herfnai in forum JDBC
    Replies: 0
    Last Post: 08-17-2008, 12:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •