java - JCombobox with custom DefaultListCellRenderer as a DefaultCellEditor: returns string value instead of custom object -
i have jtable
model implements abstracttablemodel
. model holds custom requisitionitem
objects have section
object in 1 of field. when inserting new record table, add new row new requisitionitem
initialized non-null empty values. section
column, have custom renderers table , combobox follows
for table;
requestitemstable.getcolumnmodel().getcolumn(3).setcellrenderer(new defaulttablecellrenderer(){ public void setvalue(object value) { if (value==null) { settext(""); } else { section section = (section) value; settext(section.getname()); } }});
for combobox;
sectioncombobox.setrenderer(new basiccomboboxrenderer() { @override public component getlistcellrenderercomponent(jlist list, object value, int index, boolean isselected, boolean cellhasfocus) { super.getlistcellrenderercomponent(list, value, index, isselected, cellhasfocus); if (value != null) { settext(((section) value).getname()); } if (index == -1) { settext(""); } return this; } });
for editing, have following;
sectioncombobox = new jcombobox<>(); sectioncombobox.setmodel(new javax.swing.defaultcomboboxmodel<>(sectionjpacontroller.getdepartmentsections(department.getnumber()).toarray(new section[0]))); requestitemstable.getcolumnmodel().getcolumn(3).setcelleditor(new defaultcelleditor(sectioncombobox));
but after clicking section
cell, selecting 1 of section
items in combobox , hitting enter, java.lang.classcastexception: java.lang.string cannot cast ***.model.domain.section
. sow why return defaultcelleditor
not section object string?
why have use customized logic rendering , selecting section objects in combobox?
if understand correctly, trying render name section objects on jcombobox able retrieve entire section object @ jtable.
to achieve this, need things:
initialize jcombobox using vector of section objects selection.
return name of section in tostring method because jcombobox evaluates tostring of items rendering.
set combox used in celleditor jtable column section (which have done)
a running example be:
public class jtablewithcomboboxeditor extends jframe { class section { string name; int value2; public section(string name, int value2) { this.name = name; this.value2 = value2; } @override public string tostring() { return name; } } private jtable table = new jtable(new defaulttablemodel(new string[]{"section"}, 2)); public jtablewithcomboboxeditor() { this.add(new jscrollpane(table)); section section1 = new section("a", 1); section section2 = new section("b", 2); vector sectionlist = new vector(); sectionlist.add(section1); sectionlist.add(section2); jcombobox combobox = new jcombobox(sectionlist); table.getcolumnmodel().getcolumn(0).setcelleditor(new defaultcelleditor(combobox)); table.getmodel().addtablemodellistener(new tablemodellistener() { @override public void tablechanged(tablemodelevent e) { object value = table.getvalueat(0, 0); if (value != null) { section section = (section) value; system.out.println(section.name + " " + section.value2); } } }); } }
Comments
Post a Comment