the first is to create a table
JTable demotable= new JTable (); DefaultTableModel Dtmdemo = (DefaultTableModel) Demotable.getmodel (); String[] Tableheads = {"Ordinal", "id", "jcheckbox"};d tmdemo.setcolumnidentifiers (tableheads);
//2 refers to the first column, counting from 0
Demotable.getcolumnmodel (). GetColumn (2). Setcelleditor (New Checkboxcelleditor ());d Emotable.getcolumnmodel (). GetColumn (2). Setcellrenderer (New Cwcheckboxrenderer ());
data in the//jtable
for (int i=0;i<10;i++) {object[] Objdata = {i,2,new Boolean (false)/** here is the Jcheckbox location */}; Dtmdemo.addrow (Objdata);}
copy and paste the following class volume below the class you are trying to add the component to
~ Inner Classes----------------------------------------------------------------------------------------------- -----Class Checkboxcelleditor extends Abstractcelleditor implements Tablecelleditor {//~ Static fields/initializers--- ----------------------------------------------------------------------------------private static final long Serialversionuid = 1L; ~ Instance Fields--------------------------------------------------------------------------------------------- ---protected jcheckbox checkBox; ~ Constructors------------------------------------------------------------------------------------------------ ---public checkboxcelleditor () {CheckBox = new Jcheckbox (); Checkbox.sethorizontalalignment (Swingconstants.center); Checkbox.setbackground (Color.White); }//~ Methods---------------------------------------------------------------------------------------------------- ----@Override public Object Getcelleditorvalue () {return boolean.valueof (checkbOx.isselected ()); } //~ ----------------------------------------------------------------------------------------------------------- -----@Override Public Component gettablecelleditorcomponent (JTable table, Object value, Boolean isSelected, int row, int column) {checkbox.setselected ((Boolean) value). Booleanvalue ()); return checkBox; }}//End Class Checkboxcelleditorclass Cwcheckboxrenderer extends Jcheckbox implements Tablecellrenderer {//~ Static fi Elds/initializers-------------------------------------------------------------------------------------Private Static final Long serialversionuid = 1L; ~ Instance Fields--------------------------------------------------------------------------------------------- ---Border Border = new Emptyborder (1, 2, 1, 2); ~ Constructors------------------------------------------------------------------------------------------------ ---public cwcheckboxrenderer () {super (); Setopaque (TRUE); SetHOrizontalalignment (Swingconstants.center); }//~ Methods---------------------------------------------------------------------------------------------------- ----@Override public Component gettablecellrenderercomponent (JTable table, Object value, Boolean isSelected, Boolean hasfocus, int row, int column) {if (value instanceof Boolean) {setselected ((Boolean) Value). Booleanvalue ()); SetEnabled (table.iscelleditable (row, column)); Setforeground (Table.getforeground ()); SetBackground (Table.getbackground ()); } return this; }}//End Class Cwcheckboxrenderer
Final effect:
Java Swing Development JTable in Add component (Jcheckbox)