JavaFX table lost sorting feature -


after following tutorial http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/ filtering observablelist tableview via textfield. table columns lost it's default sorting feature. heres sscce

controller/model

package application;  import java.math.bigdecimal; import java.text.decimalformat;  import javafx.application.application; import javafx.beans.property.objectproperty; import javafx.beans.property.simpleobjectproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.collections.transformation.filteredlist; import javafx.collections.transformation.sortedlist; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.textfield; import javafx.stage.stage;  public class test extends application{      @fxml tableview<dummy> tbldummies;     @fxml tablecolumn<dummy, string> tbcname;     @fxml tablecolumn<dummy, string> tbcprice1;     @fxml tablecolumn<dummy, string> tbcprice2;     @fxml textfield txtfinder;      observablelist<dummy> mainlist = fxcollections.observablearraylist();      @override     public void start(stage primarystage) throws exception {         fxmlloader f = new fxmlloader(getclass().getresource("test.fxml"));         f.setcontroller(this);         primarystage.setscene(new scene(f.load()));         setup();         primarystage.show();     }      private void setup() {          //random values added main list          mainlist.add(new dummy("abc", new bigdecimal(1.9), new bigdecimal(2.9)));         mainlist.add(new dummy("bcd", new bigdecimal(2.9), new bigdecimal(3.9)));         mainlist.add(new dummy("cde", new bigdecimal(3.9), new bigdecimal(4.9)));         mainlist.add(new dummy("def", new bigdecimal(4.9), new bigdecimal(5.9)));         mainlist.add(new dummy("efg", new bigdecimal(5.9), new bigdecimal(6.9)));         mainlist.add(new dummy("fgh", new bigdecimal(6.9), new bigdecimal(7.9)));         mainlist.add(new dummy("ghi", new bigdecimal(7.9), new bigdecimal(8.9)));          //linking property table columns          tbcname.setcellvaluefactory( data -> data.getvalue().nameproperty());         tbcprice1.setcellvaluefactory( data -> data.getvalue().price1formattedproperty());         tbcprice2.setcellvaluefactory( data -> data.getvalue().price2formattedproperty());          //config. sorter          filteredlist<dummy> filtereddata = new filteredlist<>(mainlist, p -> true);         sortedlist<dummy> sorteddata = new sortedlist<>(filtereddata);         sorteddata.comparatorproperty().bind(tbldummies.comparatorproperty());         tbldummies.setitems(filtereddata);          //type filter          txtfinder.textproperty().addlistener((obs,old,niu)->{             filtereddata.setpredicate(producto -> {                 if (niu == null || niu.isempty())                      return true;                                 if (producto.getname().tolowercase().contains(niu.tolowercase()))                     return true;                 return false;             });         });      }      public static void main(string[] args) {         launch(args);     }         class dummy {          private stringproperty name;         private objectproperty<bigdecimal> price1;         private objectproperty<bigdecimal> price2;          public dummy(             string name,             bigdecimal price1,             bigdecimal price2)         {             this.name = new simplestringproperty(name);             this.price1 = new simpleobjectproperty<bigdecimal>(price1);             this.price2 = new simpleobjectproperty<bigdecimal>(price2);          }          public stringproperty nameproperty() { return name; }         public objectproperty<bigdecimal> price1property() { return price1; }         public objectproperty<bigdecimal> price2property() { return price2; }          public string getname() { return this.name.get(); }         public bigdecimal getprice1() { return this.price1.get(); }         public bigdecimal getprice2() { return this.price2.get(); }          public void setname(string name) { this.name.set(name); }         public void setprice1(bigdecimal price1) { this.price1.set(price1); }         public void setprice2(bigdecimal price2) { this.price2.set(price2); }          //------formatting money values          private stringproperty price1formattedproperty = new simplestringproperty();         private stringproperty price2formattedproperty = new simplestringproperty();          public stringproperty price1formattedproperty() {             price1formattedproperty.set("$ "+f2d(getprice1().doublevalue()));             return price1formattedproperty;         }             public stringproperty price2formattedproperty() {             price2formattedproperty.set("$ "+f2d(getprice2().doublevalue()));             return price2formattedproperty;         }          private string f2d(double value) {             decimalformat df = new decimalformat("0.00##");             return df.format(value);         }      }  } 

fxml view

    <?xml version="1.0" encoding="utf-8"?>  <?import javafx.geometry.insets?> <?import javafx.scene.control.label?> <?import javafx.scene.control.tablecolumn?> <?import javafx.scene.control.tableview?> <?import javafx.scene.control.textfield?> <?import javafx.scene.layout.borderpane?> <?import javafx.scene.layout.hbox?>   <borderpane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">    <center>       <tableview fx:id="tbldummies" prefheight="200.0" prefwidth="200.0" borderpane.alignment="center">         <columns>           <tablecolumn fx:id="tbcname" prefwidth="75.0" text="name" />           <tablecolumn fx:id="tbcprice1" prefwidth="75.0" text="price 1" />             <tablecolumn fx:id="tbcprice2" prefwidth="75.0" text="price 2" />         </columns>          <columnresizepolicy>             <tableview fx:constant="constrained_resize_policy" />          </columnresizepolicy>       </tableview>    </center>    <top>       <hbox alignment="center_left" spacing="10.0" borderpane.alignment="center">          <children>             <label text="type filter table" />             <textfield fx:id="txtfinder" />          </children>          <padding>             <insets bottom="10.0" left="10.0" right="10.0" top="10.0" />          </padding>       </hbox>    </top> </borderpane> 

my question how sorting feature back.

heres quick switch. replace section , sorting filtering gone.

         //config. sorter          /*filteredlist<dummy> filtereddata = new filteredlist<>(mainlist, p -> true);         sortedlist<dummy> sorteddata = new sortedlist<>(filtereddata);         sorteddata.comparatorproperty().bind(tbldummies.comparatorproperty());         tbldummies.setitems(filtereddata);*/         tbldummies.setitems(mainlist);          //type filter          txtfinder.textproperty().addlistener((obs,old,niu)->{             /*filtereddata.setpredicate(producto -> {                 if (niu == null || niu.isempty())                      return true;                                 if (producto.getname().tolowercase().contains(niu.tolowercase()))                     return true;                 return false;             });*/         }); 


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -