swing - Java JTable combobox validation -
there 3 things trying here:
first trying make combo box show "not feed" when u lanuch program, @ moment when launches shows nothing, when click on combo box shows option "feed" , "not feed".
secondly trying validation combobox, when click
jbutton
next, validate if combobox "feed" if go next, else have pop saying "check again"lastly, make cells on first 4 col uneditable , last column editable.
public class dosagetablehelper { private static jtable todotable; private static jscrollpane jpane; private static int counter=1; public static defaulttablemodel getelderlyfromquerydos(string timing,string position) throws sqlexception { sqlobject = new sqlobject(); resultset rs = null; if(timing.equalsignorecase("morning")){ preparedstatement stmt = so.getpreparedstatementwithkey("select morningdosage et_elderly name = ?"); stmt.setstring(1,position); stmt.executequery(); system.out.println(stmt); rs = stmt.getresultset(); } else if(timing.equalsignorecase("afternoon")){ preparedstatement stmt = so.getpreparedstatementwithkey("select afternoondosage et_elderly name = ?"); stmt.setstring(1,position); stmt.executequery(); system.out.println(stmt); rs = stmt.getresultset(); } else if(timing.equalsignorecase("noon")){ preparedstatement stmt = so.getpreparedstatementwithkey("select noondosage et_elderly name = ?"); stmt.setstring(1,position); stmt.executequery(); system.out.println(stmt); rs = stmt.getresultset(); } return (defaulttablemodel) buildtablemodel(rs); } @suppresswarnings("unchecked") public static defaulttablemodel buildtablemodel(resultset rs) throws sqlexception { arraylist<dosageobject> dosagelist=null; system.out.println(rs); try { while(rs.next()){ bytearrayinputstream in = new bytearrayinputstream(rs.getbytes(1)); objectinputstream = new objectinputstream(in); object retrievedosblob =(object) is.readobject(); if(retrievedosblob instanceof arraylist<?>){ dosagelist=((arraylist<dosageobject>) retrievedosblob); } } } catch (classnotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } // storing array list in array list future uses vector<string> columnnames = new vector<string>(); columnnames.add("description"); columnnames.add("prescription"); columnnames.add("medication type"); columnnames.add("dosage"); columnnames.add("checked"); vector<vector<object>> data = new vector<vector<object>>(); for(int k=0;k<dosagelist.size();k++){ vector<object> vector = new vector<object>(); vector.add(dosagelist.get(k).getmeddescrip()); vector.add(dosagelist.get(k).getmedprescrip()); vector.add(dosagelist.get(k).getmedtype()); vector.add(dosagelist.get(k).getmeddosage()); data.add(vector); } defaulttablemodel dtm = new defaulttablemodel(data, columnnames) { private static final long serialversionuid = 4234183862785566645l; @override public boolean iscelleditable(int rowindex, int columnindex) { return !( rowindex == 1 && columnindex == 1 ); } }; return dtm; } // debug-able main method public static void main(string[] args) throws sqlexception { arraylist<string> namelist= new arraylist<string>(); namelist.add("lee ching chong"); namelist.add("lim kuay siak"); namelist.add("lee ching chong"); todotable =new jtable(getelderlyfromquerydos("morning",namelist.get(0))); string[] values = new string[] { "not feed", "feed" }; tablecolumn col = todotable.getcolumnmodel().getcolumn(4); col.setcelleditor(new mycomboboxeditor(values)); col.setcellrenderer(new mycomboboxrenderer(values)); jpane = new jscrollpane(todotable); jpanel panel = new jpanel(); jframe frame = new jframe(); frame.setbounds(0, 0, 700, 543); panel.add(jpane); frame.getcontentpane().add(new jscrollpane(panel)); jbutton btnnext = new jbutton("next"); panel.add(btnnext); frame.setvisible(true); btnnext.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { try { todotable.setmodel(dosagetablehelper.getelderlyfromquerydos("morning",namelist.get(counter))); string[] values = new string[] { "not feed", "feed" }; tablecolumn col = todotable.getcolumnmodel().getcolumn(4); col.setcelleditor(new mycomboboxeditor(values)); col.setcellrenderer(new mycomboboxrenderer(values)); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } counter++; } }); } } @suppresswarnings("rawtypes") class mycomboboxrenderer extends jcombobox implements tablecellrenderer { private static final long serialversionuid = 1319299961084034009l; @suppresswarnings("unchecked") public mycomboboxrenderer(string[] items) { super(items); } public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { if (isselected) { setforeground(table.getselectionforeground()); super.setbackground(table.getselectionbackground()); } else { setforeground(table.getforeground()); setbackground(table.getbackground()); } setselecteditem(value); return this; } } class mycomboboxeditor extends defaultcelleditor { private static final long serialversionuid = -1702063500403826596l; @suppresswarnings({ "rawtypes", "unchecked" }) public mycomboboxeditor(string[] items) { super(new jcombobox(items)); } }
read oracle tutorial - how use tables - part use jcombobox tablecelleditor,
- explanation
jtable
has 2 separate funcionalitiestablecelleditor
editing value stored inxxxtablemodel
,tablecellrenderer
painting value stored inxxxtablemodel
, after editing (event, notifiers implemented in apis) storing selected value
tablecelleditor
(jcombobox
)xxxtablemodel
usingxxxtablemodel.setvalueat
, store"feed"
or"not feed"
, notjcombobox
object
,tablecellrenderer
painting model viewby default works automatically using
defaulttablemodel
, there required override reduced numbers of methods (getcolumnclass
,iscellediteble
, in casesetvalueat
too)
- explanation
there 2 ways (1st. required override
xxxtablemodel.setvalueat
)jbutton
disabled (programatically) untill value changed blank"feed"
or"not feed"
, required loop inside model, note after code linesuper.setvalueat()
executed, value editor stored modelaction
jbutton
loops inside model (primitive, easiest, themost efficient validations, simple , direct code, without bugs insidexxxtablemodel.setvalueat
)
required override
xxxtablemodel.setvalueat
, set, changeiscelleditable(row, column)
correct value, need create separate arrayiscelleditable
, models events, don't change array outside, methodxxxtablemodel.setvalueat
can notifier
use
defaulttablemodel
if possible (to avoids missinterpretations in code required overrideabstracttablemodel
, methods, listeners , models notifiers -firexxxxxx
)code ordering inside model important, firstly to store value model, call changes, validations, additional code, code based on value stored in model
i'm sure every 3 points here few times, including working code examples in sscce / mcve forms
Comments
Post a Comment