java - select items in ListView<items> and return null in JavaFX -
shapefx class contains arraylist shapes
public class shapefx { private arraylist<shapes> shapes; public shapefx () { this.shapes= new arraylist<>(); } public shapefx (arraylist<shapes> shapes) { this.shapes= shapes; } public arraylist<shapes> getshapes() { return this.shapes; } public void setshapes(arraylist<shapes> shapes) { this.shapes= shapes; } @override public int size() { return this.shapes.size(); } @override public void add(shapes element) { this.shapes.add(element); } @override public void remove(shapes element) { if (element != null) { this.shapes.remove(element); } } @override public string tostring() { stringbuilder str = new stringbuilder(); iterator itr = this.formageometricas.iterator(); while (itr.hasnext()) { str.append(itr.tostring()); } return str.tostring(); } }
here have small example of 2 events in javafx buttons create shapes , remove shapes, method add(shapes element) works, method remove(shapes element) not work because after shapes created, when try remove shapes, receive message doesn´t exist shapes
//in javafx shapesfx shapesfx = new shapesfx(); observablearraylist<string> options = fxcollections.observablearraylist( "circle", "triangle"); combobox<string>combobox = new combobox<>(options); button btncreate = new button("create shapes"); btncreate.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent t) { //here function object items = combobox.getselectionmodel().getselecteditem(); if (items.equals("circle")) { point2d p1 = new point2d(0, 0); //public class circle extends shapes shapesfx.add(new circle(p1, color.black, float.valueof(radiustextfield.gettext()))); } else if (items.equals("triangle")) { point2d p2 = new point2d(0, 0); //public class triangle extends shapes shapesfx.add(new triangle(p2, color.black, float.valueof(sizetextfield.gettext()))); } });
the error begin in part, when try select item in listview (shapes shape = list.getselectionmodel().getselecteditem();) or error in observablelist listshapes = fxcollections.observablearraylist(shapesfx.getshapes());
//or error here when made (shapesfx.getshapes()) final observablelist<shapes> listshapes = fxcollections.observablearraylist(shapesfx.getshapes()); final listview<shapes> list = new listview<>(listshapes); button btnremove = new button("remove shapes"); btnremove.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent t) { // or error here shapes shape = list.getselectionmodel().getselecteditem(); if (shape != null) { shapesfx.remove(shape); } else { system.out.println("doesn´t exist shapes"); } } })
after inserting data , click on btncreate create shape, in part shape created successfully.
but when click on btnremove delete created shape, , when shapes shape = list.getselectionmodel().getselecteditem(); browse , select shape clean, , instead of shape return different null (forms != null), shape return null (and displays message doesn´t exist shapes) if no shape not created
Comments
Post a Comment