Java Swing Development JTable in Add component (Jcheckbox)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.