Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.stage.Stage;
  3. import javafx.scene.*;
  4. import javafx.scene.control.*;
  5. import javafx.scene.layout.*;
  6. import javafx.event.*;
  7. import javafx.collections.*;
  8. import java.util.*;
  9. public class ExListView extends Application
  10. {
  11. private ListView lvRect;
  12. private ArrayList<Rectangle> ayRect;
  13. private ObservableList<Rectangle> obRect;
  14. private TextField tfLength, tfWidth;
  15. public void init()
  16. {
  17. ayRect = new ArrayList();
  18. ayRect.add(new Rectangle(10, 1));
  19. ayRect.add(new Rectangle(20, 1));
  20. ayRect.add(new Rectangle(30, 1));
  21. obRect = FXCollections.observableList(ayRect);
  22. }
  23. public void start(Stage topView)
  24. {
  25. FlowPane fPane = new FlowPane();
  26. topView.setScene(new Scene(fPane));
  27. // Adding ListView
  28. lvRect = new ListView(obRect);
  29. lvRect.setTooltip(new Tooltip("Rectangle"));
  30. fPane.getChildren().add(lvRect);
  31. // ListView selection
  32. lvRect.setOnMouseClicked(evt -> showSelected(evt));
  33.  
  34. // TextField and Button for adding Rectangle instance
  35. tfLength = new TextField();
  36. tfWidth = new TextField();
  37. Button addRect = new Button("Add");
  38. addRect.setOnAction(evt -> doAddRect(evt));
  39.  
  40. VBox inputBox = new VBox(10);
  41. inputBox.getChildren().addAll(tfLength, tfWidth, addRect);
  42. fPane.getChildren().add(inputBox);
  43.  
  44. topView.show();
  45. }
  46. private void doAddRect(ActionEvent evt)
  47. {
  48. double length = Double.parseDouble(tfLength.getText());
  49. double width = Double.parseDouble(tfWidth.getText());
  50. Rectange rect = new Rectangle(length, width);
  51.  
  52. obRect.add(rect);
  53.  
  54.  
  55. // Checking
  56. System.out.println("ArrayList");
  57. for (Rectangle r : ayRect)
  58. System.out.println(r);
  59.  
  60. System.out.println("ObservableList");
  61. for (Rectangle r : obRect)
  62. System.out.println(r);
  63.  
  64. }
  65. private void showSelected(Event evt)
  66. {
  67. ObservableList<Rectangle> itemsSelected =
  68. lvRect.getSelectionModel().getSelectedItems();
  69. for (Rectangle r : itemsSelected)
  70. System.out.println(r);
  71. }
  72. public static void main(String[] argv)
  73. {
  74. launch(argv);
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement