java - JTable get all rows that are being edited -
i have jtable has 2 columns
- column 0 username
- column 1 password.
for password column encrypted sha256.
basically want achieve update rows in password column sha256 have edited after button pressed.
so..
i have rowdata class, store text being edited , position of text being edited(rows,columns).
public class rowdata { int rows = 0, columns = 0; string text = " "; public rowdata(string text,int rows, int columns) { seteditedrows(rows); seteditedcolumns(columns); seteditedtext(text); } public int geteditedrows() { return rows; } public int geteditedcolumns() { return columns; } public string geteditedtext() { return text; } public void seteditedrows(int rows) { this.rows = rows; } public void seteditedcolumns(int columns) { this.columns = columns; } public void seteditedtext(string text) { this.text = text; } }
i wrote tablemodellistener.. have list store text , rows , columns after table has changed
table.getmodel().addtablemodellistener(new tablemodellistener() { @override public void tablechanged(tablemodelevent e) { int row = e.getfirstrow(); int column = e.getcolumn(); tablemodel model = (tablemodel) e.getsource(); //system.out.println(model.getvalueat(row, column)); if(column == 1) { string data = (string) model.getvalueat(row, column); system.out.println(data); datalist.add(new rowdata(data,row,column)); } } });
in button loop through list , retrieve rows, , columns , text , set password sha256 jtable.
updatebtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { if (table.getcelleditor() != null) { table.getcelleditor().stopcellediting(); for(int = 0; < datalist.size(); i++) { string text = datalist.get(i).geteditedtext(); int rows = datalist.get(i).geteditedrows(); int columns = datalist.get(i).geteditedcolumns(); //system.out.println(datalist.get(i).geteditedtext() + " " + datalist.get(i).geteditedrows() + datalist.get(i).geteditedcolumns()); table.setvalueat(convertpassword.converttosha256(text), rows ,columns); } } } });
the result keep printing password endlessly in console. think logic wrong , needed corrected.
table.setvalueat(convertpassword.converttosha256(text), rows ,columns);
when change tablemodel tablemodellistener invoked again. tablemodellistener invoked whether change data using jtable or updating tablemodel directly.
the solution remove tablemodellistener when click on button, @ start of actionlistener. need add tablemodellistener tablemodel @ end of code in case user make further changes.
another solution have 3 columns in tablemodel, username, password , sha256password. can use jtable display first 2 columns. see removecolumn()
method of jtable. conversion code update tablemodel using:
table.getmodel().setvalueat(value, row, 2);
now code in tablemodel invoked, because check updates column 1, nothing happen when update column 2.
then when save data save data tablemodel.
edit:
i must click cell before can press button edit.
you need stop cell editing. see table stop editing couple of solutions.
Comments
Post a Comment