java - How to use check box listener with a dynamic JTable? -
i new using swing, why asking lot; actually, searching lot this, not found answer. have dynamic table; has filled name of sheets xml file, , there column check box clicked if sheet has validated.
when create table empty. after other action filled data.
i need know how use check box listener in case.
this main code, button open performs filling.
public class createscenarioui extends jframe { /** * */ private static final long serialversionuid = 1l; private jpanel contentpane; private jtextfield textfield; string filepath = null; string[] sheetnames = null; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { createscenarioui frame = new createscenarioui(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ public createscenarioui() { settitle("scenario creation"); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 450); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(null); final maintable table = new maintable(contentpane); /** button open */ final jbutton btnopen = new jbutton("open"); btnopen.setfont(new font("tahoma", font.plain, 11)); btnopen.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { performbtnopen(); } private void performbtnopen() { jfilechooser choose = new jfilechooser(); choose.addchoosablefilefilter(new excelfilter()); int returnval = choose.showopendialog(null); if (returnval == jfilechooser.approve_option) { textfield.settext(choose.getselectedfile().getname()); } filereader filereader = new filereader(); system.out.println(choose.getselectedfile()); filepath = choose.getselectedfile().tostring(); sheetnames = filereader.getsheetnames(choose.getselectedfile()); table.getmodel().addcolumn("sheet name", sheetnames); boolean[] selectorcolumn = new boolean[sheetnames.length]; (int = 0; < sheetnames.length; i++) { selectorcolumn[i] = new boolean(false); } table.getmodel().addcolumn("selector", selectorcolumn); } }); btnopen.setbounds(320, 50, 100, 20); contentpane.add(btnopen);
}}
and here how create maintable
:
public class maintable extends jframe implements tablemodellistener, itemlistener { defaulttablemodel model = new defaulttablemodel(); @override public void tablechanged(tablemodelevent arg0) { // todo auto-generated method stub } public maintable(jpanel contentpane){ string[] setcolumnidentifiers = {}; jtable table = new jtable(new object[][] {}, new object[] { "sheet name", "create" }); jscrollpane scrollpane = new jscrollpane(table); /** main table */ model = new defaulttablemodel(setcolumnidentifiers, 0) { /** * */ private static final long serialversionuid = 1l; @override public class getcolumnclass(int columnindex) { if (columnindex == 1) { return boolean.class; } else { return super.getcolumnclass(columnindex); } } @override public boolean iscelleditable(int row, int column) { boolean editable = true; if (column == 0) { editable = false; /**object value = getvalueat(row, column); if (value instanceof integer) { editable = ((int)value) != 0; }*/ } return editable; } }; table.setmodel(model); table.getmodel().addtablemodellistener(this); table.setfillsviewportheight(true); table.setcolumnselectionallowed(true); table.setborder(new lineborder(new color(0, 0, 0))); table.setcellselectionenabled(true); table.setbounds(40, 100, 380, 190); scrollpane.setbounds(40, 100, 380, 190); scrollpane.setborder(new lineborder(new color(0, 0, 0))); contentpane.add(scrollpane); } public defaulttablemodel getmodel(){ return model; } @override public void itemstatechanged(itemevent event) { //object source = event.getitemselectable(); // todo auto-generated method stub }
}
any useful, thank everyone!
the default cell editor editable column of type boolean.class
jcheckbox
. only editor needs listen checkbox, , only while checkbox being edited. after editor concludes, examine tablemodel
changes. if component needs know such changes, should register tablemodellistener
, illustrated here. related examples may found here , here.
Comments
Post a Comment