java - How to add JCheckBox in JTable? -
first of apologize english neglect explain problem.
first want jcheckbox in jtable have.
i retrieving student id , student name database in column index 0 , 1. want third column should absent/present take whether student present or absent jcheckbox value.
here code jtable values :
attendance.java
/* * change template, choose tools | templates * , open template in editor. */ package shreesai; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.util.vector; /** * * @author admin */ public class attendance{ connection con = connectdatabase.connecrdb(); public vector getemployee()throws exception { vector<vector<string>> employeevector = new vector<vector<string>>(); preparedstatement pre = con.preparestatement("select studentid,name student"); resultset rs = pre.executequery(); while(rs.next()) { vector<string> employee = new vector<string>(); employee.add(rs.getstring(1)); //empid employee.add(rs.getstring(2));//name employeevector.add(employee); } if(con!=null) con.close(); rs.close(); pre.close(); return employeevector; } }
this code taking values database saving vector
attendancegui.java
/* * change template, choose tools | templates * , open template in editor. */ package shreesai; import static java.awt.frame.maximized_both; import java.util.vector; import javax.swing.joptionpane; /** * * @author admin */ public class attendancegui extends javax.swing.jframe { /** * creates new form attendancegui */ private vector<vector<string>> data; private vector<string> header; public attendancegui() throws exception { this.setlocationrelativeto(null); setextendedstate(maximized_both); attendance att = new attendance(); data = att.getemployee(); header = new vector<string>(); header.add("student id"); header.add("student name"); header.add("absent/present"); initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jscrollpane1 = new javax.swing.jscrollpane(); attendancet = new javax.swing.jtable(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); attendancet.setmodel(new javax.swing.table.defaulttablemodel( data,header )); jscrollpane1.setviewportview(attendancet); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap(397, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap(89, short.max_value)) ); pack(); }// </editor-fold> /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ 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(attendancegui.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(attendancegui.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(attendancegui.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(attendancegui.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { try{ new attendancegui().setvisible(true); } catch(exception e){ joptionpane.showmessagedialog(null,e); } } }); } // variables declaration - not modify private javax.swing.jtable attendancet; private javax.swing.jscrollpane jscrollpane1; // end of variables declaration }
my problemis can't add jcheckbox
in front of every student have seen jtabel
model, renderer , don't anything. want this...
i've search stuff couple of weeks did bot suitable this
start how use tables.
your table model needs several things.
- it needs return
boolean.class
getcolumnclass
method appropriate column. need override method. - the method
iscelleditable
need returntrue
table column want make editable (so user can change value of column) - you're table model need capable of holding value column
- make sure pass valid value
boolean
column row, otherwisenull
updated simple example
import java.awt.eventqueue; import java.util.vector; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.defaulttablemodel; public class tabletest { public static void main(string[] args) { new tabletest(); } public tabletest() { startui(); } public void startui() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } mytablemodel model = new mytablemodel(); model.addrow(new object[]{0, "brian", false}); model.addrow(new object[]{1, "ned", false}); model.addrow(new object[]{2, "john", false}); model.addrow(new object[]{3, "drogo", false}); jtable table = new jtable(model); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new jscrollpane(table)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class mytablemodel extends defaulttablemodel { public mytablemodel() { super(new string[]{"id", "name", "present"}, 0); } @override public class<?> getcolumnclass(int columnindex) { class clazz = string.class; switch (columnindex) { case 0: clazz = integer.class; break; case 2: clazz = boolean.class; break; } return clazz; } @override public boolean iscelleditable(int row, int column) { return column == 2; } @override public void setvalueat(object avalue, int row, int column) { if (avalue instanceof boolean && column == 2) { system.out.println(avalue); vector rowdata = (vector)getdatavector().get(row); rowdata.set(2, (boolean)avalue); firetablecellupdated(row, column); } } } }
ps- highly recommend avoid form editors until have better understanding of how swing works - imho
Comments
Post a Comment