java - Get selected rows in JTable -
i have jtable
looked image below. when row clicked, want display selected row values.
viewmovie
public class viewmovie extends javax.swing.jframe { public viewmovie() throws exception { initcomponents(); populatejtable(); } // create methode populate data jtable mysql database , displaying picture public void populatejtable() throws exception{ myquery mq = new myquery(); arraylist<movie> list = mq.bindtable(); string[] columnname = {"title","date","time","price","hall","description","image"}; object[][] rows = new object[list.size()][8]; for(int = 0; < list.size(); i++){ //rows[i][0] = list.get(i).getid(); rows[i][0] = list.get(i).gettitle(); rows[i][1] = list.get(i).getdate(); rows[i][2] = list.get(i).gettime(); rows[i][3] = list.get(i).getprice(); rows[i][4] = list.get(i).gethall(); rows[i][5] = list.get(i).getdescription(); if(list.get(i).getimage() != null){ imageicon image = new imageicon(new imageicon(list.get(i).getimage()).getimage() .getscaledinstance(150, 120, image.scale_smooth) ); rows[i][6] = image; } else{ rows[i][6] = null; } } themodel model = new themodel(rows, columnname); jtable1.setmodel(model); jtable1.setrowheight(120); jtable1.getcolumnmodel().getcolumn(5).setpreferredwidth(150); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { arraylist<movie> list; jscrollpane1 = new javax.swing.jscrollpane(); jtable1 = new javax.swing.jtable(); jlabel1 = new javax.swing.jlabel(); back= new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jtable1.setmodel(new javax.swing.table.defaulttablemodel( new object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new string [] { "title 1", "title 2", "title 3", "title 4" } )); jtable1.addmouselistener(new java.awt.event.mouseadapter() { // row clicked public void mouseclicked(java.awt.event.mouseevent evt) { int selectedrowindex = jtable1.getselectedrow(); int selectedcolumnindex = jtable1.getselectedcolumn(); object selectedobject = (object) jtable1.getmodel().getvalueat(selectedrowindex, selectedcolumnindex); system.out.println(selectedobject); } }); jscrollpane1.setviewportview(jtable1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1) .addcontainergap()) .addgroup(layout.createsequentialgroup() .addgap(228, 228, 228) .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 291, javax.swing.grouplayout.preferred_size) .addcontainergap(261, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 272, javax.swing.grouplayout.preferred_size) .addgap(18, 18, 18) .addcomponent(jlabel1, javax.swing.grouplayout.default_size, 194, short.max_value) .addcontainergap()) ); pack(); }// </editor-fold> public static void main(string args[]) { try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(viewmovie.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(viewmovie.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(viewmovie.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(viewmovie.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { try { new viewmovie().setvisible(true); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } // variables declaration - not modify private javax.swing.jlabel jlabel1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jtable jtable1; private javax.swing.jbutton back; // end of variables declaration }
when click 1 cell, suppose display row value, display cell value.
one option print data vector of selected row:
defaulttablemodel model = (defaulttablemodel)jtable1.getmodel(); system.out.println(model.getdatavector().get(selectedrowindex));
or can print cell values of row 1 after another:
tablemodel model = jtable1.getmodel(); for(int = 0; < model.getcolumncount(); i++){ if(i > 0){ system.out.println(", "); } system.out.println(model.getvalueat(selectedrowindex, selectedcolumnindex)); } system.out.println();
Comments
Post a Comment