java - "Checkbox editor" for JTable -
what best way add "checkbox editor" jtable? far have jtable use custom abstracttablemodel , use 2 collections data: 1 hashmap add each row checkbox "false" value. second collection framework simple arraylist integers. abstracttablemodel has custom method delete selected rows arraylist , abstracttablemodel. problem if "check" chceckbox in table, checked value stays in same row. think problem in overriding setvalueat. piece of code:
package checkboxeditor; import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.abstracttablemodel; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.hashmap; import javax.swing.jbutton; public class checkboxeditor extends jpanel { public checkboxeditor() { super(new borderlayout()); mytablemodel mytablemodel = new mytablemodel(); jtable table = new jtable(mytablemodel); table.setfillsviewportheight(true); jbutton deletebtn = new jbutton("delete selected rows"); deletebtn.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent ae) { int rowcount = table.getrowcount(); (int = rowcount - 1; >= 0; i--) { boolean checked = (boolean) table.getvalueat(i, 0); if (checked) { mytablemodel.removerow((int) table.getvalueat(i, 1)); } } } }); add(deletebtn, borderlayout.page_start); //create scroll pane , add table it. jscrollpane scrollpane = new jscrollpane(table); //add scroll pane panel. add(scrollpane, borderlayout.center); } class mytablemodel extends abstracttablemodel { // column names private string[] columnnames = {"#", "number"}; // check boxes hashmap<integer, boolean> checkboxes = new hashmap(); // data arraylist<integer> data = new arraylist(); public mytablemodel() { (integer = 1; < 6; i++) { data.add(i); } } public void removerow(integer numbertodelete) { int index = data.indexof(numbertodelete); data.remove(numbertodelete); firetablerowsdeleted(index, index); } public int getcolumncount() { return columnnames.length; } public int getrowcount() { return data.size(); } public string getcolumnname(int col) { return columnnames[col]; } public object getvalueat(int row, int col) { switch (col) { case 0: object value = checkboxes.get(row); return (value == null) ? false : value; case 1: return data.get(row); default: return ""; } } /* * jtable uses method determine default renderer/ * editor each cell. if didn't implement method, * last column contain text ("true"/"false"), * rather check box. */ public class getcolumnclass(int col) { switch (col) { case 0: return boolean.class; default: return integer.class; } } /* * don't need implement method unless table's * editable. */ public boolean iscelleditable(int row, int col) { //note data/cell address constant, //no matter cell appears onscreen. return (col == 0); } /* * don't need implement method unless table's * data can change. */ public void setvalueat(object value, int row, int col) { if (col == 0) { checkboxes.put(row, (boolean) value); } } } /** * create gui , show it. thread safety, method should * invoked event-dispatching thread. */ private static void createandshowgui() { //create , set window. jframe frame = new jframe("tabledemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); //create , set content pane. checkboxeditor newcontentpane = new checkboxeditor(); newcontentpane.setopaque(true); //content panes must opaque frame.setcontentpane(newcontentpane); //display window. frame.pack(); frame.setvisible(true); } public static void main(string[] args) { //schedule job event-dispatching thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
one hashmap add each row checkbox "false" value. second collection framework simple arraylist integers
there no need 2 data structure or create custom tablemodel.
you can use defaulttablemodel
. defaulttablemodel
allows store type of object in each row. need override getcolumnclass(...)
method of model return proper class , table choose appropriate renderer/editor column. like:
string[] columnnames = {"#", "number"}; defaulttablemodel model = new defaulttablemodel(columnnames, 0) { // returning class of each column allow different // renderers , editors used based on class public class getcolumnclass(int column) { return column == 0 ? boolean.class : integer.class; } }; jtable table = new jtable(model);
then can add data table doing like:
for (integer = 1; < 6; i++) { object[] row = {boolean.false, i}; model.addrow( row); }
the defaulttablemodel
supports methods removing rows model.
Comments
Post a Comment