java - ComboBox cannot be cast to TextField? -
i have arraylist of javafx.scene.control.control (arraylist) 2 different underclasses of control(combobox, textfield).
so want update list new input(example) in both of them.
but when try it, eclipse makes error this:
exception in thread "javafx application thread" java.lang.classcastexception: javafx.scene.control.combobox cannot cast javafx.scene.control.textfield @ application.documentbuildup.aktualisiereliste(documentbuildup.java:34) @ application.mainstage$2.handle(mainstage.java:99) @ application.mainstage$2.handle(mainstage.java:1) @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.eventutil.fireeventimpl(unknown source) @ com.sun.javafx.event.eventutil.fireevent(unknown source) @ javafx.event.event.fireevent(unknown source) @ javafx.scene.node.fireevent(unknown source) @ javafx.scene.control.button.fire(unknown source) @ com.sun.javafx.scene.control.behavior.buttonbehavior.mousereleased(unknown source) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(unknown source) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(unknown source) @ com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(unknown source) @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source) @ com.sun.javafx.event.eventutil.fireeventimpl(unknown source) @ com.sun.javafx.event.eventutil.fireevent(unknown source) @ javafx.event.event.fireevent(unknown source) @ javafx.scene.scene$mousehandler.process(unknown source) @ javafx.scene.scene$mousehandler.access$1500(unknown source) @ javafx.scene.scene.impl_processmouseevent(unknown source) @ javafx.scene.scene$scenepeerlistener.mouseevent(unknown source) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(unknown source) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.tk.quantum.glassvieweventhandler.lambda$handlemouseevent$354(unknown source) @ com.sun.javafx.tk.quantum.quantumtoolkit.runwithoutrenderlock(unknown source) @ com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(unknown source) @ com.sun.glass.ui.view.handlemouseevent(unknown source) @ com.sun.glass.ui.view.notifymouse(unknown source) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$148(unknown source) @ java.lang.thread.run(unknown source)
i hope can help.
here code. class dokumentbuilup has method updatelist. named "aktualisiereliste". :))
class mainstage:
public class mainstage extends application { /** * globale variablen */ private int abstandszaehler = 0; private scrollpane rahmen = new scrollpane(); private xmlactions xa = new xmlactions(); private pane partpane = new pane(); private group huelle = new group(); private int id = 0; private list<control> entries = new arraylist<control>(); private documentbuildup dbu = new documentbuildup(entries); /** * main class javafx application extends javafx.application.application class. * start() method main entry point javafx applications. */ public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) { //policy bezüglich scrollingverhalten rahmen.sethbarpolicy(scrollbarpolicy.always); rahmen.setvbarpolicy(scrollbarpolicy.always); //fenster konfigurieren und anzeigen primarystage.settitle("xml-zeilenersteller"); primarystage.setresizable(false); //erzeugt notwendige elemente im gui erzeugeeinzelneelemente(); partpane.setstyle("-fx-background-color: lightsteelblue"); huelle.getchildren().addall(partpane); rahmen.setcontent(huelle); scene mainscene = new scene(rahmen, 900, 590); primarystage.setscene(mainscene); primarystage.show(); } /** * hauptelemente für die root */ public void erzeugeeinzelneelemente(){ //buttons button newxmlline = new button(); newxmlline.settext("einfügen neuer xml-zeile"); newxmlline.setlayoutx(735); newxmlline.setlayouty(80); newxmlline.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { seteverythinginaline(); } }); button newxmldocument = new button(); newxmldocument.settext("erstelle xml-dokument"); newxmldocument.setlayoutx(735); newxmldocument.setlayouty(550); newxmldocument.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { dbu.aktualisiereliste(entries); system.out.println("test2"); } }); //Überschrift text header = new text(105, 50, "xml lines creator"); header.setfont(new font(30)); header.setstyle("-fx-underline: true;"); /** * hinzufügen zur gui */ partpane.getchildren().addall(newxmldocument,newxmlline,header); } /** * methode zur erstellungen der zeilen comboboxen, textfelder */ @suppresswarnings({ "rawtypes", "unchecked" }) public void seteverythinginaline(){ //comboboxen combobox cb = new combobox(xa.getlist()); partpane.getchildren().add(cb); cb.setlayoutx(80); cb.setlayouty(80 + abstandszaehler); entries.add(cb); id++; // cb.setid(integer.tostring(id)); //textfield textfield tfid= new textfield ("id"); partpane.getchildren().add(tfid); tfid.setlayoutx(185); tfid.setlayouty(80 + abstandszaehler); entries.add(tfid); textfield tfname= new textfield ("name"); partpane.getchildren().add(tfname); tfname.setlayoutx(340); tfname.setlayouty(80 + abstandszaehler); entries.add(tfname); textfield tfwert= new textfield ("wert"); partpane.getchildren().add(tfwert); tfwert.setlayoutx(495); tfwert.setlayouty(80 + abstandszaehler); entries.add(tfwert); system.out.println(integer.tostring(id)); //zaehler für den abstand abstandszaehler = abstandszaehler + 30; system.out.println(entries); } }
class documentbuilup:
public class documentbuildup{ @suppresswarnings("unused") private list<control> list; public documentbuildup(list<control> list){ this.list = list; } public list<string> aktualisiereliste(list<control> list ){ list<string> stringentries = new arraylist<string>(); for(int i=0; i<=list.size()-1; i++){ //brauche die zahl beim anklicken der zeile listener? if(i%3 == 1){ combobox cb = (combobox) list.get(i); stringentries.add((string) cb.getvalue()); } if(i%3 == 2 || i%3 == 0){ textfield tf = (textfield) list.get(i); stringentries.add(tf.gettext()); } } system.out.println(stringentries); return stringentries; } }
edit: try 2 seperated lists. 1 combobox , 1 textfield.
edit 2: working 2 arraylists!! :))
if compare how content of entries
list added , how use in aktualisiereliste
should able see problem:
public void seteverythinginaline(){ ... entries.add(somecombobox); ... entries.add(sometextfield); ... entries.add(sometextfield2); ... entries.add(sometextfield3); }
this means there subsequence of 3 textfield
s. first element combobox
public list<string> aktualisiereliste(list<control> list ){ ... for(int i=0; i<=list.size()-1; i++){ if(i%3 == 1){ combobox cb = (combobox) list.get(i); ... } if(i%3 == 2 || i%3 == 0){ textfield tf = (textfield) list.get(i); ... } } ... }
here assume first element (index 0) textfield
(0 % 3 = 0
), isn't case. subsequence of 3 textfield
s produce error, since 3 consecutive indices produce 3 different values modulo 3:
1 % 3 = 1 2 % 3 = 2 3 % 3 = 0
which cause classcastexception
.
i'd consider adding elements list , retrieving them way bad practice, since can't change order of elements (well can, strict restrictions) without breaking other aktualisiereliste
. better approach use instanceof
check element type:
for(control c : list) { if(c instanceof combobox){ combobox cb = (combobox) c; ... } else { textfield tf = (textfield) c; ... } }
however keep list date registering change listeners control
s, i'd prefer:
public class listmodifyingchangelistener implements changelistener<string> { private final int index; private final list<string> list; public listmodifyingchangelistener(int index, list<string> list) { this.index = index; this.list = list; } @override public void changed(observablevalue<? extends string> observable, string oldvalue, string newvalue) { list.set(index, newvalue); } }
list<string> liste; // declared field combobox<string> cb = ... ... // create fixed sized list initial values liste = arrays.aslist(cb.getvalue(), tfid.gettext(), tfname.gettext(), tfwert.gettext()); // register listeners modify element of liste on change cb.valueproperty().addlistener(new listmodifyingchangelistener(0, liste)); tfid.textproperty().addlistener(new listmodifyingchangelistener(1, liste)); tfname.textproperty().addlistener(new listmodifyingchangelistener(2, liste)); tfwert.textproperty().addlistener(new listmodifyingchangelistener(3, liste));
this makes sure liste
contains recent values , prevents unnecessary recreation of lists. no need pass list of controls method , distinguish control types...
Comments
Post a Comment