Advertisement
Guest User

Untitled

a guest
Sep 5th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. public class CellFactory extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception {
  4. AnchorPane root = FXMLLoader.load(CellFactory.class.getResource("CellFactory_Layout.fxml"));
  5. Scene scene = new Scene(root);
  6. primaryStage.setScene(scene);
  7. primaryStage.setTitle("CellFactory EXAMPLE");
  8. primaryStage.show();
  9. }
  10.  
  11. public static void main(String[] args) {
  12. launch(args);
  13. }
  14. }
  15.  
  16. public class Fruit {
  17. private final SimpleStringProperty name;
  18. private final SimpleIntegerProperty weight;
  19.  
  20. public Fruit(String name, int weight){
  21. this.name = new SimpleStringProperty(name);
  22. this.weight = new SimpleIntegerProperty(weight);
  23. }
  24.  
  25. public String getName() {return this.name.get();}
  26. public void setName(String v) {this.name.set(v);}
  27. public SimpleStringProperty nameProperty() {return this.name;}
  28.  
  29. public int getWeight() {return this.weight.get();}
  30. public void setWeight(int v) {this.weight.set(v);}
  31. public SimpleIntegerProperty weightProperty() {return this.weight;}
  32. }
  33.  
  34. public class CellFactory_Controller implements Initializable {
  35. @FXML private TableView<Fruit> fruit_tbl;
  36. @FXML private TableColumn<Fruit, String> name_cln;
  37. @FXML private TableColumn<Fruit, Integer> weight_cln;
  38.  
  39. // array with table data
  40. final ObservableList<Fruit> data = FXCollections.observableArrayList();
  41.  
  42. public CellFactory_Controller() {
  43. // some data
  44. this.data.add(new Fruit("banana", 120));
  45. this.data.add(new Fruit("apple", 150));
  46. this.data.add(new Fruit("coconut", 500));
  47. this.data.add(new Fruit("orange", 200));
  48. }
  49.  
  50. @Override
  51. public void initialize(URL location, ResourceBundle resources) {
  52. this.name_cln.setCellValueFactory(new PropertyValueFactory<>("name"));
  53. this.weight_cln.setCellValueFactory(new PropertyValueFactory<>("weight"));
  54. this.weight_cln.setCellValueFactory(cellData -> cellData.getValue().weightProperty().asObject());
  55.  
  56. // comment or uncomment to see what happen
  57. ///////////////////////////////////////////////////////////////////////
  58. this.weight_cln.setCellFactory(column -> new TableCell<Fruit, Integer>() {
  59. @Override
  60. protected void updateItem(Integer item, boolean empty) {
  61. super.updateItem(item, empty);
  62.  
  63. if (item == null || empty) {
  64. setText(null);
  65. setStyle("");
  66. } else {
  67. if (item < 10) {
  68. setTextFill(Color.CHOCOLATE);
  69. } else {
  70. setTextFill(Color.BLACK);
  71. setStyle("");
  72. }
  73. }
  74. }
  75. });
  76. ///////////////////////////////////////////////////////////////////////
  77.  
  78. this.fruit_tbl.setItems(this.data);
  79. }
  80. }
  81.  
  82. <?xml version="1.0" encoding="UTF-8"?>
  83.  
  84. <?import javafx.scene.control.*?>
  85. <?import java.lang.*?>
  86. <?import javafx.scene.layout.*?>
  87.  
  88. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CellFactory.CellFactory_Controller">
  89. <children>
  90. <TableView fx:id="fruit_tbl" layoutX="189.0" layoutY="93.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
  91. <columns>
  92. <TableColumn fx:id="name_cln" prefWidth="471.0" text="FRUIT" />
  93. <TableColumn fx:id="weight_cln" prefWidth="75.0" text="WEIGHT" />
  94. </columns>
  95. <columnResizePolicy>
  96. <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
  97. </columnResizePolicy>
  98. </TableView>
  99. </children>
  100. </AnchorPane>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement