Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.TableColumn;
  8. import javafx.scene.control.TableView;
  9. import javafx.scene.control.TextField;
  10. import javafx.scene.control.cell.PropertyValueFactory;
  11. import javafx.scene.layout.HBox;
  12. import javafx.scene.layout.VBox;
  13. import javafx.stage.Stage;
  14.  
  15.  
  16. public class Main extends Application {
  17.  
  18. TextField playerName;
  19. TextField dexMod;
  20. TextField otherMod;
  21. TableView<Players> playersTable;
  22. Button addPlayerBtn;
  23. Button rollInitBtn;
  24. Button delPlayerBtn;
  25.  
  26.  
  27. public static void main(String[] args){
  28.  
  29. launch(args);
  30.  
  31.  
  32. }
  33.  
  34.  
  35. @Override
  36. public void start(Stage primaryStage) throws Exception {
  37.  
  38. primaryStage.setTitle("Initiative");
  39.  
  40. // TextField so the user can enter a players Name
  41. playerName = new TextField();
  42. playerName.setPrefSize(200, 20);
  43. playerName.setPromptText("Player Name");
  44. //playerName.setFocusTraversable(false);
  45.  
  46. // TextField so the user can enter the players dex mod
  47. dexMod = new TextField();
  48. dexMod.setPrefSize(50, 20);
  49. dexMod.setPromptText("Dex");
  50. //dexMod.setFocusTraversable(false);
  51.  
  52. // TextField so the user can enter the players other mods
  53. otherMod = new TextField();
  54. otherMod.setPrefSize(50, 20);
  55. otherMod.setPromptText("Other");
  56. //otherMod.setFocusTraversable(false);
  57.  
  58.  
  59. addPlayerBtn = new Button("Add Player");
  60. addPlayerBtn.setOnAction(e -> addPlayerBtnClicked());
  61.  
  62. delPlayerBtn = new Button("Delete Player");
  63. delPlayerBtn.setOnAction(e -> delPlayerBtnClicked());
  64.  
  65. rollInitBtn = new Button("Roll Initiative");
  66. rollInitBtn.setOnAction(e -> rollInitBtnClicked());
  67.  
  68.  
  69.  
  70. //TableView Name Columns
  71. TableColumn<Players, String> nameColumn = new TableColumn<>("Name");
  72. nameColumn.setMinWidth(200);
  73. nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
  74.  
  75. //TableView dexMod Columns
  76. TableColumn<Players, String> dexColumn = new TableColumn<>("Dex Mod");
  77. dexColumn.setMinWidth(50);
  78. dexColumn.setStyle( "-fx-alignment: CENTER;");
  79. dexColumn.setCellValueFactory(new PropertyValueFactory<>("dexMod"));
  80.  
  81. //TableView otherMod Columns
  82. TableColumn<Players, String> otherColumn = new TableColumn<>("Other Mods");
  83. otherColumn.setMinWidth(50);
  84. otherColumn.setStyle( "-fx-alignment: CENTER;");
  85. otherColumn.setCellValueFactory(new PropertyValueFactory<>("otherMod"));
  86.  
  87. //TableView roll Columns
  88. TableColumn<Players, String> rollColumn = new TableColumn<>("Die Roll");
  89. rollColumn.setMinWidth(50);
  90. rollColumn.setStyle( "-fx-alignment: CENTER;");
  91. rollColumn.setCellValueFactory(new PropertyValueFactory<>("roll"));
  92.  
  93. //TableView total Columns
  94. TableColumn<Players, String> totalColumn = new TableColumn<>("Total");
  95. totalColumn.setMinWidth(50);
  96. totalColumn.setStyle( "-fx-alignment: CENTER;");
  97. totalColumn.setCellValueFactory(new PropertyValueFactory<>("total"));
  98.  
  99.  
  100.  
  101. playersTable = new TableView<>();
  102. playersTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
  103. playersTable.setItems(getPlayers());
  104. playersTable.getColumns().addAll(nameColumn, dexColumn, otherColumn, rollColumn, totalColumn);
  105.  
  106.  
  107.  
  108. HBox hLayout = new HBox();
  109. hLayout.setPadding(new Insets(10, 10, 10, 10)); // sets the padding on the top right left and bottom of the layout
  110. hLayout.setSpacing(10); // sets the spacing between the TextFields and buttons
  111. hLayout.getChildren().addAll(playerName, dexMod, otherMod, addPlayerBtn, rollInitBtn, delPlayerBtn);
  112.  
  113.  
  114. VBox vLayout = new VBox();
  115. vLayout.getChildren().addAll(playersTable, hLayout);
  116.  
  117.  
  118. Scene mainScene = new Scene(vLayout, 800, 400);
  119. primaryStage.setScene(mainScene);
  120. primaryStage.show();
  121.  
  122. }
  123.  
  124.  
  125. // What happens when the user clicks the Add Player button
  126. public void addPlayerBtnClicked() {
  127.  
  128. Players players = new Players();
  129. players.setName(playerName.getText());
  130. players.setDexMod(Integer.parseInt(dexMod.getText()));
  131. players.setOtherMod(Integer.parseInt(otherMod.getText()));
  132. playersTable.getItems().add(players);
  133. playerName.clear();
  134. dexMod.clear();
  135. otherMod.clear();
  136. }
  137.  
  138. // What happens when the user clicks the Delete Player button
  139. public void delPlayerBtnClicked() {
  140.  
  141. ObservableList<Players> playerSelected, allPlayers;
  142. allPlayers = playersTable.getItems();
  143. playerSelected = playersTable.getSelectionModel().getSelectedItems();
  144.  
  145. playerSelected.forEach(allPlayers::remove);
  146.  
  147.  
  148.  
  149. }
  150.  
  151. public void rollInitBtnClicked() {
  152.  
  153. Players players = new Players();
  154. ObservableList<Players> playerSelected, allPlayers;
  155. allPlayers = playersTable.getItems();
  156. playerSelected = playersTable.getSelectionModel().getSelectedItems();
  157.  
  158. players.setRoll((int)Math.ceil(Math.random() * 20));
  159.  
  160. }
  161.  
  162.  
  163.  
  164. public ObservableList<Players> getPlayers(){
  165. ObservableList<Players> players = FXCollections.observableArrayList();
  166. return players;
  167. }
  168.  
  169. }
  170.  
  171. public class Players {
  172.  
  173. private String name;
  174. private int dexMod;
  175. private int otherMod;
  176. private int roll;
  177. private int total;
  178.  
  179.  
  180. // default method to enter default data
  181. public Players(){
  182.  
  183. }
  184.  
  185. // overloaded method to enter data that is handed to it
  186. public Players(String name, int dexMod, int otherMod, int roll, int total){
  187.  
  188. this.name = name;
  189. this.dexMod = dexMod;
  190. this.otherMod = otherMod;
  191. this.roll = roll;
  192. this.total = total;
  193.  
  194. }
  195.  
  196. public String getName() {
  197. return name;
  198. }
  199.  
  200. public void setName(String name) {
  201. this.name = name;
  202. }
  203.  
  204. public int getDexMod() {
  205. return dexMod;
  206. }
  207.  
  208. public void setDexMod(int dexMod) {
  209. this.dexMod = dexMod;
  210. }
  211.  
  212. public int getOtherMod() {
  213. return otherMod;
  214. }
  215.  
  216. public void setOtherMod(int otherMod) {
  217. this.otherMod = otherMod;
  218. }
  219.  
  220. public int getRoll() {
  221. return roll;
  222. }
  223.  
  224. public void setRoll(int roll) {
  225. this.roll = roll;
  226. }
  227.  
  228. public int getTotal() {
  229. return total;
  230. }
  231.  
  232. public void setTotal(int total) {
  233. this.total = total;
  234. }
  235.  
  236. }
  237.  
  238. private final Random random = new Random();
  239.  
  240. public void rollInitBtnClicked() {
  241. List<Players> targets = playersTable.getItems();
  242.  
  243. for (Players p : targets) {
  244. p.setRoll(random.nextInt(20));
  245. // set total ect...
  246. }
  247.  
  248. // update table
  249. playersTable.refresh();
  250. }
  251.  
  252. private final IntegerProperty roll = new SimpleIntegerProperty();
  253.  
  254. public int getRoll() {
  255. return roll.get();
  256. }
  257.  
  258. public void setRoll(int roll) {
  259. this.roll.set(roll);
  260. }
  261.  
  262. public IntegerProperty rollProperty() {
  263. return this.roll;
  264. }
  265.  
  266. // same modifications for other int properties
  267. ...
  268.  
  269. private final IntegerBinding total = Bindings.createIntegerBinding(() -> getRoll() + getOtherMod() + getDexMod(), roll, otherMod, dexMod);
  270.  
  271. public int getTotal() {
  272. return this.total.get();
  273. }
  274.  
  275. public IntegerBinding totalProperty() {
  276. return this.total;
  277. }
  278.  
  279. TableColumn<Players, String> nameColumn
  280. TableColumn<Players, Number> dexColumn
  281. TableColumn<Players, Number> otherColumn
  282. TableColumn<Players, Number> rollColumn
  283. TableColumn<Players, Number> totalColumn
  284.  
  285. playersTable.setEditable(true);
  286.  
  287. nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
  288.  
  289. otherColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
  290. rollColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
  291. totalColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
  292.  
  293. TableColumn<Players, Integer> totalColumn = new TableColumn<>("Total");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement