java 8 - javafx-TableView as combobox popup (Tried and able to achieve partially. need help further) -


what need:

  1. need editable combobox filters data on popup upon typing , first matching item should highlighted , should set text in combo upon pressing enter.
  2. the popup should tableview 2 or 3 columns. (screen shot attached.)(in image textfield prefer combo if user not sure values, can click combo button , see entire list , select one.)

i binding data source tableview should act popup combobox. know should go custom control dont know start?

enter image description here

reference url: enter link description here

thanks in advance.

what have tried far:

guys,

with idea guys gave, have tried far , able achieve. (for now, not showing tableview dynamically below text field (eventually dats wat want)) 1. tableview static data loaded , added scene. 2. having text field below tableview. (this named txt) 3. having text field below first text field(this named txt1) (when press tab previous text field cursor should come here)

i have predicate set table , updating predicate user types in txt. (working) able filter table , first matching row highlighted. (working) when user press either "tab" or "enter" highlighted row in tableview should set value in txt.(working)

needed: 1. when press "tab" or "enter", highlighted row set value in text field (txt) , cursor should move next focusable node. in case 2nd text field (txt1). dont want txt1.requestfocus() bcoz in realtime, have many text fields in scene , control user control. cant hardcode anything.

  1. when user types text in text field(not full text. eg: "do" table contains "dom", "don"), if there multiple matches, both records displayed in table first 1 highlighted. user should able select 2nd row if wants pressing down arrow text field itself.

main.java

package application;  import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.layout.vbox; import javafx.stage.stage;   public class main extends application {     @override     public void start(stage primarystage) {         try {             vbox root = fxmlloader.load(this.getclass().getresource("mainview.fxml"));             scene scene = new scene(root,500,300);             scene.getstylesheets().add(getclass().getresource("application.css").toexternalform());             primarystage.setscene(scene);             primarystage.show();         } catch(exception e) {             e.printstacktrace();         }     }      public static void main(string[] args) {         launch(args);     } } 

maincontroller.java

package application;  import java.io.ioexception; import java.net.url; import java.util.resourcebundle;  import javafx.application.platform; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.collections.transformation.filteredlist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.fxml.initializable; import javafx.scene.parent; import javafx.scene.control.button; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.textfield; import javafx.scene.input.keycode; import javafx.scene.input.keyevent; import javafx.stage.popup;  public class maincontroller implements initializable {     private @fxml tableview<person> table;     private @fxml tablecolumn<person, string> firstnamecol;     private @fxml tablecolumn<person, string> lastnamecol;     private @fxml tablecolumn<person, string> emailcol;     private @fxml tablecolumn<person, integer> agecol;     private @fxml textfield txt;     private @fxml textfield txt1;     private @fxml button btn;      @override     public void initialize(url location, resourcebundle resources)     {         platform.runlater(new runnable() {             @override             public void run() {                 txt.requestfocus();             }         });          observablelist<person> obslist =fxcollections.observablearraylist();         obslist.add(new person("sam", "p1lasttname", "p1email@gmail.com", 20));         obslist.add(new person("dom", "p2lasttname", "p2email@gmail.com", 30));         obslist.add(new person("ken", "p3lasttname", "p3email@gmail.com", 40));         obslist.add(new person("don", "p4lasttname", "p4email@gmail.com", 50));         obslist.add(new person("tom", "p5lasttname", "p5email@gmail.com", 60));           filteredlist<person> filteredlist = new filteredlist<>(obslist, p->true);         table.setitems(filteredlist);          txt.textproperty().addlistener((obs, oldvalue, newvalue) ->{              filteredlist.setpredicate(person-> {                 if(newvalue == null || newvalue.isempty())                     return true;                 if(person.getfirstname().trim().tolowercase().contains(newvalue.tolowercase()))                     return true;                 return false;             });              platform.runlater(new runnable() {                 @override                 public void run()                 {                     // don't want repeated selections                     table.getselectionmodel().clearselection();                     //get focus                     table.requestfocus();                      //select first item in tableview model                     table.getselectionmodel().selectfirst();                      //set focus on first element                     table.getfocusmodel().focus(0);                      //render selected item in tableview                     //tableclickhandler(null);                 }             });              platform.runlater(new runnable() {                 @override                 public void run()                 {                     txt.requestfocus();                     txt.end();                 }             });          });          table.setonkeypressed(new eventhandler<keyevent>() {             @override             public void handle(keyevent event)             {                 if(event.getcode() == keycode.enter)                 {                     txt.settext(table.getselectionmodel().getselecteditem().getfirstname());                 }             }         });          txt.setonkeypressed(new eventhandler<keyevent>() {             @override             public void handle(keyevent event)             {                 if(event.getcode() == keycode.enter || event.getcode() == keycode.tab)                 //if(event.getcode() == keycode.enter)                 {                     txt.settext(table.getselectionmodel().getselecteditem().getfirstname());                     /*platform.runlater(new runnable() {                         public void run() {                             txt1.requestfocus();                         }                     });*/                 }             }         });          /*txt.addeventfilter(keyevent.key_pressed, new eventhandler<keyevent>() {             @override             public void handle(keyevent event)             {                 if(event.getcode() == keycode.tab)                 {                     //txt.settext(table.getselectionmodel().getselecteditem().getfirstname());                     if(txt.getskin() instanceof behaviorskinbase)                     {                         //((behaviorskinbase)txt.getskin()).getbehavior().traversenext();                         behaviorbase x = ((behaviorskinbase)txt.getskin()).getbehavior();                         ((textfieldbehavior)x).callaction("traversenext");                     }                     event.consume();                 }             }         });*/          btn.setonaction(new eventhandler<actionevent>() {             @override             public void handle(actionevent event)             {                 /*                 popup popup = new popup();                 popup.getcontent().add(new tableview());                 //popup.show(txt, txt.localtoscreen(0, 0).getx() + txt.getwidth()/2, txt.localtoscreen(0, 0).gety() + txt.getheight());                 popup.show(txt, txt.localtoscreen(0, 0).getx(), txt.localtoscreen(0, 0).gety() + txt.getheight() + 2);                 */                 parent vbox = null;                 try {                     vbox = fxmlloader.load(this.getclass().getresource("tableview.fxml"));                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 popup popup = new popup();                 popup.getcontent().add(vbox);                 //popup.show(txt, txt.localtoscreen(0, 0).getx() + txt.getwidth()/2, txt.localtoscreen(0, 0).gety() + txt.getheight());                 //popup.show(txt, txt.localtoscreen(0, 0).getx(), txt.localtoscreen(0, 0).gety() + txt.getheight() + 2);                 popup.show(txt, txt.localtoscreen(0, 0).getx(), txt.localtoscreen(0, 0).gety() + txt.getheight() + 2);             }         });     } } 

person.java

package application;  public class person {     private string firstname;     private string lastname;     private string email;     private integer age;      public person(){}      public person(string firstname, string lastname, string email, integer age)     {         this.firstname = firstname;         this.lastname = lastname;         this.email = email;         this.age = age;     }      public string getfirstname()     {         return firstname;     }      public void setfirstname(string firstname)     {         this.firstname = firstname;     }      public string getlastname()     {         return lastname;     }      public void setlastname(string lastname)     {         this.lastname = lastname;     }      public string getemail()     {         return email;     }      public void setemail(string email)     {         this.email = email;     }      public integer getage()     {         return age;     }      public void setage(integer age)     {         this.age = age;     } } 

application.css

.table-row-cell:selected {     -fx-background-color: lightgreen;     /* below style remove border lines of selected row */     -fx-table-cell-border-color: transparent; } 

mainview.fxml

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.geometry.insets?> <?import javafx.scene.control.button?> <?import javafx.scene.control.tablecolumn?> <?import javafx.scene.control.tableview?> <?import javafx.scene.control.textfield?> <?import javafx.scene.control.cell.propertyvaluefactory?> <?import javafx.scene.layout.vbox?>  <!-- <?import application.person?> -->  <vbox spacing="10.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.maincontroller">    <children>       <tableview fx:id="table" prefheight="250.0" prefwidth="437.0">         <columns>           <tablecolumn fx:id="firstnamecol" prefwidth="120.0" text="first name">           <cellvaluefactory><propertyvaluefactory property="firstname" /></cellvaluefactory>           </tablecolumn>           <tablecolumn fx:id="lastnamecol" prefwidth="120.0" text="last name">           <cellvaluefactory><propertyvaluefactory property="lastname" /></cellvaluefactory>           </tablecolumn>             <tablecolumn fx:id="emailcol" prefwidth="120.0" text="email">             <cellvaluefactory><propertyvaluefactory property="email" /></cellvaluefactory>             </tablecolumn>             <tablecolumn fx:id="agecol" prefwidth="75.0" text="age">             <cellvaluefactory><propertyvaluefactory property="age" /></cellvaluefactory>             </tablecolumn>         </columns>        </tableview>       <button text="button" fx:id="btn"/>       <textfield fx:id="txt" prompttext="type filter" />       <textfield fx:id="txt1" prompttext="focus should here when tab pressed pervious txt" />    </children>    <padding>       <insets bottom="10.0" left="10.0" right="10.0" top="10.0" />    </padding> </vbox> 

tableview.fxml

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.control.tablecolumn?> <?import javafx.scene.control.tableview?> <?import javafx.scene.control.cell.propertyvaluefactory?> <?import javafx.scene.layout.vbox?> <?import application.person?> <?import javafx.collections.*?>  <!-- <vbox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65"> <children> -->        <!--  <tableview xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:id="table" prefheight="160.0" prefwidth="440.0"> -->       <tableview xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:id="table" prefheight="140.0">         <columns>           <tablecolumn fx:id="firstnamecol" prefwidth="120.0" text="first name">           <cellvaluefactory><propertyvaluefactory property="firstname" /></cellvaluefactory>           </tablecolumn>           <tablecolumn fx:id="lastnamecol" prefwidth="120.0" text="last name">           <cellvaluefactory><propertyvaluefactory property="lastname" /></cellvaluefactory>           </tablecolumn>             <tablecolumn fx:id="emailcol" prefwidth="120.0" text="email">             <cellvaluefactory><propertyvaluefactory property="email" /></cellvaluefactory>             </tablecolumn>             <tablecolumn fx:id="agecol" prefwidth="75.0" text="age">             <cellvaluefactory><propertyvaluefactory property="age" /></cellvaluefactory>             </tablecolumn>         </columns>          <columnresizepolicy><tableview fx:constant="constrained_resize_policy" /></columnresizepolicy>          <items>         <fxcollections fx:factory="observablearraylist">         <person firstname="p1firstname" lastname="p1lasttname" email="p1email@gmail.com" age="20"/>         <person firstname="p2firstname" lastname="p2lasttname" email="p2email@gmail.com" age="30"/>         <person firstname="p3firstname" lastname="p3lasttname" email="p3email@gmail.com" age="40"/>         <person firstname="p4firstname" lastname="p4lasttname" email="p4email@gmail.com" age="50"/>         <person firstname="p5firstname" lastname="p5lasttname" email="p5email@gmail.com" age="60"/>         </fxcollections>         </items>        </tableview> <!-- </children> </vbox> --> 

any appreciated. thanks!


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -