java - Appropriate collection for model of JTable used for selection -
what best collection storing data in jtable's model (extending abstracttablemodel) if table contains check box selection in first column , object's attributes in other columns (so boolean attribute not part of domain object in row)? have now, suppose map or list serve better?
edited: need mutable collection hold on pair of boolean/person, can retrieve persons boolean set true.
public class tablemodelperson extends abstracttablemodel { private string columnname[] = {"yes/no", "first name", "last name"}; private object data[][] = { {true, new person("katy", "brown")}, {false, new person("sam", "brown")}, {true, new person("peter", "brown")} }; @override public int getrowcount() { return data.length; } @override public int getcolumncount() { return columnname.length; } @override public object getvalueat(int rowindex, int columnindex) { person person = (person) data[rowindex][1]; switch (columnindex) { case 0: return data[rowindex][0]; case 1: return person.getfirstname(); case 2: return person.getlastname(); } return "error"; } @override public void setvalueat(object avalue, int rowindex, int columnindex) { data[rowindex][columnindex] = avalue; firetabledatachanged(); } @override public class<?> getcolumnclass(int columnindex) { return getvalueat(0, columnindex).getclass(); } @override public boolean iscelleditable(int rowindex, int columnindex) { if (columnindex == 0) { return true; } return false; } @override public string getcolumnname(int column) { return columnname[column]; } public list<person> getallselectedpersons() { list<person> lista = new arraylist<>(1); (int = 0; < data.length; i++) { if (data[i][0] == true) { lista.add(new person((string) data[i][1], (string) data[i][2])); } } return lista; } public void addperson(person person){ // code adding person } public void removeperson(person person){ //code removing person } }
edited: maybe found solution:
private list<map.entry<boolean, person>> boolperson = new arraylist<>(); public list<person> getpersonsselected(){ list<person> personlist = new arraylist<>(); for(map.entry<boolean, person> entry : boolperson){ if(entry.getkey()){ personlist.add(entry.getvalue()); } } return personlist; }
but, i'm still wondering there better one?
i intended post photo of table can't because first question, please gentle :)
take @ row table model. first part relates question. shows how create custom persontablemodel
.
in example person
stored in list
, depending on column parameter appropriate getter method person
class used data. in case want boolean value separated person, need have separate storage boolean values.
then need customize method take account boolean column. example might customize getcolumnclass() method follows:
@override public class getcolumnclass(int column) { switch (column) { case 0: return boolean.class; // added case 3: return date.class; // changed default: return string.class; } }
so suggest first code working simple person class using code blog understand basics of creating custom tablemodel. further customization add boolean column @ beginning.
Comments
Post a Comment