java - Is it possible to selectively style the contents of a TableCell in JavaFx? -


i've tableview contents needs styled using different colors, possible achieve same in javafx.

here example program

import javafx.application.application; import javafx.beans.property.simplestringproperty; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.geometry.insets; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.layout.vbox; import javafx.scene.text.font; import javafx.stage.stage;  public class tableviewsample extends application {      private final tableview<person> table = new tableview<>();     private final observablelist<person> data = fxcollections.observablearraylist(new person("jacob smith", "jacob.smith@example.com"), new person(         "isabella johnson", "isabella.johnson@example.com"), new person("ethan williams", "ethan.williams@example.com"), new person("emma jones",         "emma.jones@example.com"), new person("michael brown", "michael.brown@example.com"));      public static void main(final string[] args)     {         launch(args);     }      @override     public void start(final stage stage)     {         final scene scene = new scene(new group());         stage.settitle("table view sample");         stage.setwidth(450);         stage.setheight(500);          final label label = new label("address book");         label.setfont(new font("arial", 20));          table.seteditable(true);          final tablecolumn namecol = new tablecolumn("first name");         namecol.setminwidth(200);         namecol.setcellvaluefactory(new propertyvaluefactory<>("name"));          final tablecolumn emailcol = new tablecolumn("email");         emailcol.setminwidth(200);         emailcol.setcellvaluefactory(new propertyvaluefactory<>("email"));          table.setitems(data);         table.getcolumns().addall(namecol, emailcol);          final vbox vbox = new vbox();         vbox.setspacing(5);         vbox.setpadding(new insets(10, 0, 0, 10));         vbox.getchildren().addall(label, table);          ((group) scene.getroot()).getchildren().addall(vbox);          stage.setscene(scene);         stage.show();     }      public static class person     {          private final simplestringproperty name;         private final simplestringproperty email;          private person(final string fname, final string email)         {             this.name = new simplestringproperty(fname);             this.email = new simplestringproperty(email);         }          public string getname()         {             return name.get();         }          public void setname(final string fname)         {             name.set(fname);         }          public string getemail()         {             return email.get();         }          public void setemail(final string fname)         {             email.set(fname);         }     } } 

the output of above program

enter image description here

is possible style last name different color first name?

you implement custom cell factory this:

namecol.setcellfactory(column -> {             return new tablecell<person, string>() {                 @override                 protected void updateitem(string item, boolean empty) {                     super.updateitem(item, empty);                      if (item == null || empty) {                          settext(null);                         setstyle("");                      } else {                          text text1 = new text( item.substring(0, item.indexof(" ")));                         text1.setfill(color.red);                         text1.setfont(font.font("helvetica", fontposture.italic, 12));                         text text2 = new text( item.substring(item.indexof(" ")));                         text2.setfill(color.blue);                         text2.setfont(font.font("helvetica", fontweight.bold, 12));                         textflow textflow = new textflow(text1, text2);                          setcontentdisplay(contentdisplay.graphic_only);                         setgraphic(textflow);                         setprefheight(20);                      }                 }             };         }); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -