Package tableview; import javafx. application. application; import javafx. beans. property. simplestringproperty; import javafx. beans. property. stringproperty; import javafx. collections. fxcollections; import javafx. collections. observablelist; import javafx. scene. group; import javafx. scene. scene; import javafx. scene. control. tablecolumn; import javafx. scene. control. tableview; import javafx. scene. control. cell. propertyvaluefactory; import javafx. scene. layout. hbox; import javafx. stage. stage; import javafx. util. callback; public class tableviewcellvalue extends application {private tableview <person> table = new tableview <person> (); 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"); Final hbox HB = new hbox (); public static void main (string [] ARGs) {launch (ARGs );} @ override public void start (stage) {scene = new scene (new group (); stage. settitle ("Table view sample"); stage. setwidth (450); stage. setheight (550); table. seteditable (true); tablecolumn firstnamecol = new tablecolumn ("first name"); firstnamecol. setcellvaluefactory (New propertyvaluefactory <person, string> ("firstname"); tablecolumn lastnamecol = new tablecolumn ("last name"); lastnamecol. setcellvaluefactory (New propertyvaluefactory <person, string> ("lastname"); tablecolumn emailcol = new tablecolumn ("email"); Final stringproperty sp = new simplestringproperty ("true "); emailcol. setcellvaluefactory (New callback () {@ overridepublic object call (Object arg0) {// sp. set ("true"); Return SP ;}}); table. setitems (data); table. getcolumns (). addall (firstnamecol, lastnamecol, emailcol); // auto-stretch columns. All columns occupy the entire table. setcolumnresizepolicy (tableview. constrained_resize_policy); (Group) scene. getroot ()). getchildren (). addall (table); stage. setscene (scene); stage. show (); SP. setvalue ("false");} public static class person {private final simplestringproperty firstname; private final simplestringproperty lastname; private final simplestringproperty email; private person (string fname, string lname, string email) {This. firstname = new simplestringproperty (fname); this. lastname = new simplestringproperty (lname); this. email = new simplestringproperty (email);} Public String getfirstname () {return firstname. get ();} public void setfirstname (string fname) {firstname. set (fname);} Public String getlastname () {return lastname. get ();} public void setlastname (string fname) {lastname. set (fname);} Public String getemail () {return email. get ();} public void setemail (string fname) {email. set (fname );}}}
Javafx. Scene. Control. tableview constrained_resize_policy: automatically stretch the column. All columns occupy the entire table.
11