java - Sorting data based on column in javafx using handler -
how call sort function without clicking on column header?
when clicking on table header, records getting sorted. want sort records without clicking on table header. possible add handler column header, if handler need call.
you can modify the sortorder
of tableview
, use the sorttype
property of tablecolum
.
example
element type
public class element { private final string a; private final string b; public element(string a, string b) { this.a = a; this.b = b; } public string geta() { return a; } public string getb() { return b; } }
constructing tableview
tableview<element> tv = new tableview<>(fxcollections.observablearraylist( new element("a", "a"), new element("a", "b"), new element("b", "a"), new element("b", "b"))); tablecolumn<element, string> column1 = new tablecolumn<>("a"); column1.setcellvaluefactory(new propertyvaluefactory<>("a")); column1.setsorttype(tablecolumn.sorttype.ascending); tablecolumn<element, string> column2 = new tablecolumn<>("b"); column2.setcellvaluefactory(new propertyvaluefactory<>("b")); column1.setsorttype(tablecolumn.sorttype.descending); tv.getcolumns().setall(column1, column2); tv.getsortorder().setall(column1, column2); // button reverses order of column1 on click button btn = new button(); btn.settext("reverse column 1"); btn.setonaction((actionevent event) -> { column1.setsorttype(reverse(column1.getsorttype())); }); ... private static tablecolumn.sorttype reverse(tablecolumn.sorttype st) { return tablecolumn.sorttype.values()[1-st.ordinal()]; }
Comments
Post a Comment