Guest User

Untitled

a guest
Jul 20th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. package test3;
  2.  
  3. import javafx.application.Application;
  4. import javafx.beans.property.SimpleStringProperty;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.event.ActionEvent;
  8. import javafx.event.EventHandler;
  9. import javafx.geometry.Insets;
  10. import javafx.scene.Group;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.Hyperlink;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.TableColumn;
  16. import javafx.scene.control.TableView;
  17. import javafx.scene.control.cell.PropertyValueFactory;
  18. import javafx.scene.layout.HBox;
  19. import javafx.scene.layout.VBox;
  20. import javafx.scene.text.Font;
  21. import javafx.stage.Stage;
  22.  
  23. public class TableViewSample extends Application {
  24.  
  25. private TableView<Design> table = new TableView<Design>();
  26. private final ObservableList<Design> data = FXCollections
  27. .observableArrayList(new Design("1", "sword", "3",
  28. "very good sword, defeats absolutely any oponent "));
  29. final VBox hb = new VBox();
  30.  
  31. public static void main(String[] args) {
  32. launch(args);
  33. }
  34.  
  35. @Override
  36. public void start(Stage stage) {
  37. Scene scene = new Scene(new Group());
  38. stage.setTitle("Market");
  39. stage.setWidth(700);
  40. stage.setHeight(650);
  41.  
  42. Button button = new Button("Market");
  43. final Hyperlink link = new Hyperlink("https://google.com/");
  44.  
  45. button.setOnAction(new EventHandler<ActionEvent>() {
  46. @Override
  47. public void handle(ActionEvent e) {
  48. getHostServices().showDocument(link.getText());
  49. }
  50. });
  51.  
  52. final Label label = new Label("user name");
  53. label.setFont(Font.font("Arial", 14));
  54.  
  55. final HBox head = new HBox();
  56. head.setSpacing(450);
  57. head.setPadding(new Insets(10, 0, 0, 10));
  58. head.getChildren().addAll(button, label);
  59.  
  60. table.setEditable(true);
  61.  
  62. TableColumn designIdCol = new TableColumn("Design ID");
  63. designIdCol.setPrefWidth(90);
  64. designIdCol
  65. .setCellValueFactory(new PropertyValueFactory<Design, String>(
  66. "Design ID"));
  67.  
  68. TableColumn designNameCol = new TableColumn("Design Name");
  69. designNameCol.setPrefWidth(150);
  70. designNameCol
  71. .setCellValueFactory(new PropertyValueFactory<Design, String>(
  72. "Design Name"));
  73.  
  74. TableColumn printsAvailableCol = new TableColumn("Prints Available");
  75. printsAvailableCol.setPrefWidth(130);
  76. printsAvailableCol
  77. .setCellValueFactory(new PropertyValueFactory<Design, String>(
  78. "Prints Available"));
  79.  
  80. TableColumn partDescriptionCol = new TableColumn("Description");
  81. partDescriptionCol.setPrefWidth(310);
  82. partDescriptionCol
  83. .setCellValueFactory(new PropertyValueFactory<Design, String>(
  84. "Description"));
  85.  
  86. table.setItems(data);
  87. table.getColumns().addAll(designIdCol, designNameCol,
  88. printsAvailableCol, partDescriptionCol);
  89.  
  90.  
  91. final Label id = new Label();
  92. id.setFont(Font.font("Arial", 12));
  93. final Label name = new Label();
  94. name.setFont(Font.font("Arial", 12));
  95. final Label availability = new Label();
  96. availability.setFont(Font.font("Arial", 12));
  97. final Label description = new Label();
  98. description.setFont(Font.font("Arial", 12));
  99.  
  100. table.getSelectionModel()
  101. .selectedItemProperty()
  102. .addListener((observableValue, oldValue, newValue) -> {
  103. // Check whether item is selected and set value of selected
  104. // item to Label
  105. if (table.getSelectionModel().getSelectedItem() != null) {
  106. id.setText("ID: " + newValue.getID());
  107. }
  108. });
  109.  
  110. table.getSelectionModel()
  111. .selectedItemProperty()
  112. .addListener((observableValue, oldValue, newValue) -> {
  113. // Check whether item is selected and set value of selected
  114. // item to Label
  115. if (table.getSelectionModel().getSelectedItem() != null) {
  116. name.setText("Design Name: " + newValue.getName());
  117. }
  118. });
  119.  
  120. table.getSelectionModel()
  121. .selectedItemProperty()
  122. .addListener((observableValue, oldValue, newValue) -> {
  123. // Check whether item is selected and set value of selected
  124. // item to Label
  125. if (table.getSelectionModel().getSelectedItem() != null) {
  126. availability.setText("Availability: " + newValue.getAvailability());
  127. }
  128. });
  129.  
  130. table.getSelectionModel()
  131. .selectedItemProperty()
  132. .addListener((observableValue, oldValue, newValue) -> {
  133. // Check whether item is selected and set value of selected
  134. // item to Label
  135. if (table.getSelectionModel().getSelectedItem() != null) {
  136. description.setText("Description: " + newValue.getDescription());
  137. }
  138. });
  139.  
  140. final Label selected = new Label("Selected: ");
  141.  
  142. final Button printButton = new Button("Print!");
  143. printButton.setOnAction(new EventHandler<ActionEvent>() {
  144. @Override
  145. public void handle(ActionEvent e) {
  146. System.out.print("print");
  147. }
  148. });
  149. // bottom box
  150. hb.getChildren().addAll(selected, id, name, availability, description, printButton);
  151. hb.setSpacing(3);
  152.  
  153. final VBox vbox = new VBox();
  154. vbox.setSpacing(10);
  155. vbox.setPadding(new Insets(10, 0, 0, 10));
  156. vbox.getChildren().addAll(head, table, hb);
  157.  
  158. ((Group) scene.getRoot()).getChildren().addAll(vbox);
  159.  
  160. stage.setScene(scene);
  161. stage.show();
  162. }
  163.  
  164. public static class Design {
  165.  
  166. private final SimpleStringProperty id;
  167. private final SimpleStringProperty name;
  168. private final SimpleStringProperty availability;
  169. private final SimpleStringProperty description;
  170.  
  171. private Design(String ID, String NAME, String AVAILABILITY,
  172. String DESCRIPTION) {
  173. this.id = new SimpleStringProperty(ID);
  174. this.name = new SimpleStringProperty(NAME);
  175. this.availability = new SimpleStringProperty(AVAILABILITY);
  176. this.description = new SimpleStringProperty(DESCRIPTION);
  177. }
  178.  
  179. public String getID() {
  180. return id.get();
  181. }
  182.  
  183. public void setID(String ID) {
  184. id.set(ID);
  185. }
  186.  
  187. public String getName() {
  188. return name.get();
  189. }
  190.  
  191. public void setName(String NAME) {
  192. name.set(NAME);
  193. }
  194.  
  195. public String getAvailability() {
  196. return availability.get();
  197. }
  198.  
  199. public void setAvailability(String AVAILABILITY) {
  200. availability.set(AVAILABILITY);
  201. }
  202.  
  203. public String getDescription() {
  204. return description.get();
  205. }
  206.  
  207. public void setDescription(String DESCRIPTION) {
  208. description.set(DESCRIPTION);
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment