i have table view add row manually , want know before add row if record add in table.
james_d saying have add @override public boolean equals(object obj)
method in class. allow use default list contains()
function can in controller code
if(!table.getitems().contains(newobj)) table.getitems().add(newobj);
here example code:
public void start(stage primarystage) throws exception { observablelist<myobject> items = fxcollections.observablearraylist( new myobject("robert"), new myobject("nick"), new myobject("john"), new myobject("kate")); tableview<myobject> table = new tableview<>(); table.setitems(items); tablecolumn<myobject, string> column = new tablecolumn<>("column name"); column.setcellvaluefactory(new propertyvaluefactory<>("name")); table.getcolumns().addall(column); table.setitems(items); textfield textfield = new textfield(); button button = new button("add"); button.setonmouseclicked(event -> { myobject newobj = new myobject(textfield.gettext()); if(!table.getitems().contains(newobj)){ table.getitems().addall(newobj); } }); vbox root = new vbox(); root.getchildren().addall(table, textfield, button); primarystage.setscene(new scene(root, 600, 475)); primarystage.show(); } public static class myobject { private string name; public myobject(string name) { setname(name); } @override public boolean equals(object obj) { return obj instanceof myobject && ((myobject) obj).name.equals(name); } public string getname() { return name; } public void setname(string name) { this.name = name; } }
and propper way go, if can't change objects class can use or similar helper function.
public static boolean contains(tableview<myobject> table, myobject obj){ for(myobject item: table.getitems()) if (item.getname().equals(obj.getname())) return true; return false; }
Comments
Post a Comment