java - jtable selectionlistener doesn't work -


i have write little program table in it, i've tried , read many documentation , anwers don't understand doing wrong, because 4th column still editable , no event triggered when select rows...

main

public class main extends jframe{      /**      *       */     private static final long serialversionuid = 1l;       private jlabel banner;      private string[] tecnici;     private string[] columnnames;     private object[][] data;     private jtable table;     map<string,list<cliente>> clientilist;     private container pane = getcontentpane();     grouplayout gl = new grouplayout(pane);      main(){         init();     }      private void init(){         imageicon webicon = new imageicon(constants.work_gui_logo);         seticonimage(webicon.getimage());          settitle(constants.work_gui_title);         setsize(300, 200);         setlocationrelativeto(null);         setdefaultcloseoperation(exit_on_close);          banner=new jlabel("", webicon, jlabel.center);          menubar menu=new menubar();          setjmenubar(menu.getmenubar());          system.out.println("leggo tecnici dal db");         tecnici=new string[] {"tarzan","cita","jane"};          clientilist=new hashmap();          for(string tec:tecnici){             list<cliente> l=new arraylist<>();             cliente c=new cliente();             c.setconsultivoincassi(1000);             c.setnome("clnl ross");             c.setpreventivoincassi(1000);             l.add(c);             c=new cliente();             c.setconsultivoincassi(2000);             c.setnome("clnl ross");             c.setpreventivoincassi(2000);             l.add(c);             clientilist.put(tec, l);         }         data=createtablefrommap(clientilist);         columnnames=new string[]{"tecs","customer","count","credit"};         table=new jtable(data, columnnames){             public boolean iscelleditable(int row, int col) {                 //where want make editable 4th col                 return col==4;             }          };         table.getselectionmodel().addlistselectionlistener(new tableselectionlistener());          createwindowlayout(                 banner,                 new jlabel("tecnici"),                 new jcheckbox(tecnici[0]),                 new jcheckbox(tecnici[1]),                 new jcheckbox(tecnici[2]),                 new jtable(data, columnnames));     }      public void createwindowlayout(jcomponent... arg) {          pane = getcontentpane();         gl = new grouplayout(pane);         pane.setlayout(gl);                  gl.setautocreatecontainergaps(true);         gl.setautocreategaps(true);          gl.sethorizontalgroup(gl.createparallelgroup()             .addcomponent(arg[0])             .addgroup(gl.createsequentialgroup()                     .addcomponent(arg[1])                     .addgroup(gl.createparallelgroup(alignment.leading)                         .addcomponent(arg[2])                         .addcomponent(arg[3])                         .addcomponent(arg[4])                                                            )                     )                .addcomponent(arg[5])         );          gl.setverticalgroup(gl.createsequentialgroup()             .addcomponent(arg[0])             .addgroup(gl.createparallelgroup()                 .addcomponent(arg[1])                 .addcomponent(arg[2]))             .addcomponent(arg[3])             .addcomponent(arg[4])             .addcomponent(arg[5])         );          pack();     }      public static void main(string[] args) {         eventqueue.invokelater(() -> {                 main ex = new main();                 ex.setvisible(true);         });     }      private object[][] createtablefrommap(map<string,list<cliente>> clienti ){         iterator it= clienti.entryset().iterator();         object[][] tabella=new object[clienti.size()][4];         int counter=0;         while(it.hasnext()){             map.entry entry=(map.entry) it.next();             for(cliente c:(list<cliente>)entry.getvalue()){                 tabella[counter]=new object[]{entry.getkey(),                         c.getnome(),                         c.getpreventivoincassi(),                         c.getconsultivoincassi()};             }             counter++;         }         return tabella;     }      class tableselectionlistener implements listselectionlistener{          @override         public void valuechanged(listselectionevent arg0) {             string selecteddata = null;              int[] selectedrow = table.getselectedrows();             int[] selectedcolumns = table.getselectedcolumns();              (int = 0; < selectedrow.length; i++) {                 (int j = 0; j < selectedcolumns.length; j++) {                     selecteddata = (string) table.getvalueat(selectedrow[i], selectedcolumns[j]);                   }             }             system.out.println("selected: " + selecteddata);         }      } } 

return col==4; 

did ever basic debugging see value of "col" variable is? first thing should when have problem used system.out.println(...) display value can determine if if condition correct.

if did, have noticed 2 things:

  1. you don't output because code never executed.
  2. even did execute not work because table has 4 columns , java uses 0 based offsets should should using "3" 4th column.

the reason code never executed because have 2 table. first 1 create overridden iscelleditable() method. never add table gui dead code. create second table add gui:

new jcheckbox(tecnici[2]), new jtable(data, columnnames)); 

so rid of above statement , use:

new jcheckbox(tecnici[2]), //new jtable(data, columnnames)); table); 

since table variable table custom method.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -