java - How to add checkboxes to JTABLE swing -
1) jtable knows jcheckbox built-in boolean tablecellrenderers , tablecelleditor default, there contraproductive declare that,
2) abstracttablemodel should useful, in jtable
required reduce/restrict/change nested , inherits methods default implemented in defaulttablemodel
,
3) consider using defaulttablemodel
, (if not sure how works) instead of abstracttablemodel
,
could generated simple code:
import javax.swing.*; import javax.swing.table.*; public class tablecheckbox extends jframe { private static final long serialversionuid = 1l; private jtable table; public tablecheckbox() { object[] columnnames = {"type", "company", "shares", "price", "boolean"}; object[][] data = { {"buy", "ibm", new integer(1000), new double(80.50), false}, {"sell", "microsoft", new integer(2000), new double(6.25), true}, {"sell", "apple", new integer(3000), new double(7.35), true}, {"buy", "nortel", new integer(4000), new double(20.00), false} }; defaulttablemodel model = new defaulttablemodel(data, columnnames); table = new jtable(model) { private static final long serialversionuid = 1l; /*@override public class getcolumnclass(int column) { return getvalueat(0, column).getclass(); }*/ @override public class getcolumnclass(int column) { switch (column) { case 0: return string.class; case 1: return string.class; case 2: return integer.class; case 3: return double.class; default: return boolean.class; } } }; table.setpreferredscrollableviewportsize(table.getpreferredsize()); jscrollpane scrollpane = new jscrollpane(table); getcontentpane().add(scrollpane); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { tablecheckbox frame = new tablecheckbox(); frame.setdefaultcloseoperation(exit_on_close); frame.pack(); frame.setlocation(150, 150); frame.setvisible(true); } }); } }
Comments
Post a Comment