Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.fxml.FXMLLoader;
  3. import javafx.stage.Stage;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6.  
  7. public class Main extends Application {
  8.  
  9.  
  10. public static void main(String[] args) {
  11. launch(args);
  12. }
  13.  
  14. @Override
  15. public void start(Stage primaryStage) {
  16. try {
  17. Parent root = FXMLLoader.load(getClass().getResource("/application/MainFxml.fxml")); //this is the file that
  18. Scene scene = new Scene(root,800,800); ////100,100 is width and height of window
  19. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  20. primaryStage.setScene(scene);
  21. primaryStage.show();
  22. } catch(Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27.  
  28. import java.net.URL;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.EnumSet;
  32. import java.util.ResourceBundle;
  33. import javafx.beans.property.SimpleObjectProperty;
  34. import javafx.beans.property.SimpleStringProperty;
  35. import javafx.beans.property.StringProperty;
  36. import javafx.collections.FXCollections;
  37. import javafx.collections.ObservableList;
  38. import javafx.event.ActionEvent;
  39. import javafx.event.EventHandler;
  40. import javafx.fxml.FXML;
  41. import javafx.fxml.Initializable;
  42. import javafx.geometry.Pos;
  43. import javafx.scene.control.Button;
  44. import javafx.scene.control.ListView;
  45. import javafx.scene.control.TableCell;
  46. import javafx.scene.control.TableColumn;
  47. import javafx.scene.control.TableView;
  48. import javafx.scene.control.TextField;
  49. import javafx.scene.control.TableColumn.CellEditEvent;
  50. import javafx.scene.control.cell.PropertyValueFactory;
  51. import javafx.scene.control.cell.TextFieldTableCell;
  52. import javafx.scene.layout.HBox;
  53.  
  54. public class MainFxmlController implements Initializable {
  55.  
  56. public static int count=-1;
  57.  
  58. @FXML public TableView<tableClass> table = new TableView<tableClass>();
  59. @FXML private TableColumn<tableClass, String>col1;
  60. @SuppressWarnings({ "unchecked", "rawtypes" })
  61. @FXML public TableColumn<tableClass, Row> col2 = new TableColumn("Row");
  62.  
  63. @FXML public TextField txt;
  64. @FXML public Button btn, btn2;
  65. @FXML public ListView<String> listView = new ListView<String>();
  66. public static ArrayList<String> input = new ArrayList<String>();
  67.  
  68. final HBox hb = new HBox();
  69. public ObservableList<tableClass> obList = FXCollections.observableArrayList(); // each column contains an observable list object
  70. public ObservableList<tableClass> loadTable(){
  71. return obList; //return data object
  72. }////END loadData
  73.  
  74.  
  75. @Override
  76. public void initialize(URL url, ResourceBundle rb) {
  77. table.setEditable(true);
  78. col1.setCellValueFactory(cellData -> cellData.getValue().getCol1());
  79.  
  80.  
  81. col2.setCellFactory((param) -> new TextFieldCell<tableClass, Row>(EnumSet.allOf(Row.class)));
  82. col2.setCellValueFactory(new PropertyValueFactory<tableClass, Row>("Row"));
  83. col2.setOnEditCommit(
  84. new EventHandler<CellEditEvent<tableClass, Row>>() {
  85. @Override
  86. public void handle(CellEditEvent<tableClass, Row> t) {
  87. ((tableClass) t.getTableView().getItems().get(
  88. t.getTablePosition().getRow())
  89. ).setCol2(t.getNewValue());
  90. }
  91. }
  92. );
  93.  
  94. tableClass Entry = new tableClass(" ", Row.Row1); //create the table using getters/setters from Table Class
  95. table.getItems().addAll(Entry);
  96. table.setItems(loadTable());
  97.  
  98. col1.setCellFactory(TextFieldTableCell.<tableClass>forTableColumn()); //Makes the columns themselves editable
  99. col1.setOnEditCommit(
  100. new EventHandler<CellEditEvent<tableClass, String>>() {
  101. @Override
  102. public void handle(CellEditEvent<tableClass, String> t) {
  103. ((tableClass) t.getTableView().getItems().get( t.getTablePosition().getRow())).setCol1(t.getNewValue());
  104. }
  105. }
  106. );
  107. col1.setStyle( "-fx-alignment: BOTTOM-RIGHT;"); //to alight text next to textArea
  108.  
  109. txt.setText("fsad,0,0,gfds,43,4,4,fdsg,rtewrtwe,0,67,3,4,4,,4,44,,4"); //TO BE ROMOVED UPON COMPLETION
  110.  
  111. }//end initialize
  112.  
  113. public void buttonAction(ActionEvent e){
  114. if(txt.getText() != ""){
  115. System.out.println(txt.getText());
  116. ArrayList<String> myList = new ArrayList<String>(Arrays.asList(txt.getText().split(",")));
  117. input = myList;
  118. }
  119.  
  120. for(int i =0; i< input.size(); i++){
  121. Row.Row1.equals(input.get(i).toString());
  122. obList.add(new tableClass(input.get(i).toString(), Row.Row1));
  123. }//end for
  124.  
  125. }//end buttonAction
  126.  
  127.  
  128. public void captureText(ActionEvent e){
  129.  
  130. /*
  131. * HERE I NEED TO CAPTURE VALUES FROM THE TEXT FIELDS
  132. * IN COL2
  133. */
  134.  
  135. }
  136.  
  137.  
  138.  
  139. public static enum Row { //enum for the dxTable radio button if you want any other options, add another value and another radio buttton will be populated
  140. Row1;
  141. }
  142.  
  143.  
  144. public static class TextFieldCell<S,T extends Enum<T>> extends TableCell<S,T>{
  145.  
  146. private EnumSet<T> enumeration;
  147.  
  148. public TextFieldCell(EnumSet<T> enumeration) {
  149. this.enumeration = enumeration;
  150. }
  151.  
  152. @Override
  153. protected void updateItem(T item, boolean empty)
  154. {
  155. super.updateItem(item, empty);
  156. if (!empty)
  157. {
  158. // gui setup
  159. HBox hb = new HBox(7);
  160. hb.setAlignment(Pos.CENTER);
  161.  
  162.  
  163. // create a radio button for each 'element' of the enumeration
  164. for (Enum<T> enumElement : enumeration) {
  165. try{
  166. TextField textField = new TextField(input.get(count));
  167. textField.setUserData(enumElement);
  168. hb.getChildren().add(textField);
  169. }catch(IndexOutOfBoundsException e){}
  170. }
  171.  
  172. // issue events on change of the selected radio button
  173.  
  174. setGraphic(hb);
  175. count++;
  176. } //end if
  177. else
  178. setGraphic(null);
  179. }//updateItem
  180. }//END TextFieldCell class
  181.  
  182.  
  183. public static class tableClass{ ///table object with getters and setters.
  184. public final SimpleStringProperty col1;
  185. public final SimpleObjectProperty<Row> col2 = new SimpleObjectProperty<Row>();
  186.  
  187. public tableClass(String col1, Row t) { //uses an enum for the second type
  188. this.col1 = new SimpleStringProperty(col1);
  189. this.col2.setValue(t);
  190. }
  191.  
  192. public StringProperty getCol1() {
  193. return col1;
  194. }
  195.  
  196. public void setCol1(String i) {
  197. col1.set(i);
  198. }
  199.  
  200. public void setCol2(Row t) {
  201. col2.set(t);
  202. }
  203.  
  204. public Row getCol2() {
  205. return col2.get();
  206. }
  207. public String getCol2(int index) {
  208.  
  209. return "";
  210. }
  211.  
  212. }//end table class
  213. }//end controller
  214.  
  215. <?xml version="1.0" encoding="UTF-8"?>
  216.  
  217. <?import javafx.scene.control.Button?>
  218. <?import javafx.scene.control.TableColumn?>
  219. <?import javafx.scene.control.TableView?>
  220. <?import javafx.scene.control.TextField?>
  221. <?import javafx.scene.layout.AnchorPane?>
  222.  
  223. <AnchorPane prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainFxmlController">
  224. <children>
  225. <Button fx:id="btn" layoutX="723.0" layoutY="26.0" mnemonicParsing="false" onAction="#buttonAction" text="btn" />
  226. <TextField fx:id="txt" layoutX="41.0" layoutY="26.0" prefHeight="25.0" prefWidth="647.0" />
  227. <TableView fx:id="table" editable="true" layoutX="41.0" layoutY="106.0" prefHeight="588.0" prefWidth="451.0">
  228. <columns>
  229. <TableColumn fx:id="col1" prefWidth="75.0" text="C1" />
  230. <TableColumn fx:id="col2" prefWidth="114.0" text="C2" />
  231. </columns>
  232. </TableView>
  233. <Button fx:id="btn2" layoutX="507.0" layoutY="669.0" mnemonicParsing="false" onAction="#captureText" text="Button" />
  234. </children>
  235. </AnchorPane>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement