// https://docs.oracle.com/javafx/2/ui_controls/table-view.htm @SuppressWarnings("unchecked") public DataPanel(final String string) { super(string); tableView = new ScrollPane(); tableView.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE); tableView.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE); // set(MsstGuiPckg.getInstance().getGuiPreferredSize().propertyDataPanelWithHeight(), // MsstGuiPckg.getInstance().getGuiPreferredSize().propertyDataPanelWithWidth()); elementName = new TableColumn<>("element-name"); elementValue = new TableColumn<>("element-value"); elementName.setCellValueFactory(new PropertyValueFactory<TableEntry, String>("elementName")); elementValue.setCellValueFactory(new PropertyValueFactory<TableEntry, String>("elementValue")); dataTable = new TableView<TableEntry>(); dataTable.getColumns().addAll(elementName, elementValue); dataTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); dataTable.setPrefHeight( MsstGuiPckg.getInstance().getGuiPreferredSize().propertyDataPanelWithHeight()); dataTable.setPrefWidth( MsstGuiPckg.getInstance().getGuiPreferredSize().propertyDataPanelWithWidth()); dataTable.setEditable(true); tableView.setContent(dataTable); ; tableView.setFitToHeight(true); tableView.setFitToWidth(true); setContent(tableView); }
public void initialize(URL location, ResourceBundle resource) { bundle = resource; double tableViewWidth = 0.98 * JeproLab.APP_WIDTH; double remainingWidth = tableViewWidth - 60; jeproLabFeedTableView.setPrefWidth(tableViewWidth); jeproLabFeedTableView.setLayoutX(0.01 * JeproLab.APP_WIDTH); jeproLabFeedIndexColumn.setText("#"); jeproLabFeedIndexColumn.setPrefWidth(35); jeproLabFeedCheckBoxColumn.setText(bundle.getString("JEPROLAB_LABEL")); jeproLabFeedCheckBoxColumn.setPrefWidth(25); jeproLabFeedTitleColumn.setText(bundle.getString("JEPROLAB_TITLE_LABEL")); jeproLabFeedTitleColumn.setPrefWidth(0.15 * remainingWidth); jeproLabFeedAuthorColumn.setText(bundle.getString("JEPROLAB_AUTHOR_LABEL")); jeproLabFeedAuthorColumn.setPrefWidth(0.15 * remainingWidth); jeproLabFeedDescriptionColumn.setText(bundle.getString("JEPROLAB_DESCRIPTION_LABEL")); jeproLabFeedDescriptionColumn.getStyleClass().add("text-left"); jeproLabFeedDescriptionColumn.setPrefWidth(.6 * remainingWidth); jeproLabFeedActionColumn.setText(bundle.getString("JEPROLAB_ACTIONS_LABEL")); jeproLabFeedActionColumn.setPrefWidth(0.1 * remainingWidth); }
/** Starts the Gui applicaiton. */ public void start(Stage myStage) { System.out.println("Inside the start() method."); table = new TableView<PersonBean>(); table.setEditable(true); // Give the stage a title. myStage.setTitle("Person Spam App"); filteredData = new FilteredList<>(personData, p -> true); sortedData = new SortedList<>(filteredData); sortedData.comparatorProperty().bind(table.comparatorProperty()); table.setItems(sortedData); table.setPrefWidth(500); table.getStylesheets().add("resource/style.css"); table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY); // My edition: TableColumn<PersonBean, String> wholeNameCol = new TableColumn<PersonBean, String>("Full name"); wholeNameCol.setCellValueFactory(new PropertyValueFactory("firstAndLastName")); wholeNameCol.setCellFactory(TextFieldTableCell.<PersonBean>forTableColumn()); wholeNameCol.setOnEditCommit( (CellEditEvent<PersonBean, String> t) -> { // Start on line below ((PersonBean) t.getTableView().getItems().get(t.getTablePosition().getRow())) .setFirstAndLastName(t.getNewValue()); }); // TableColumn<PersonBean, String> firstNameCol = new TableColumn<PersonBean, String>("First // Name"); // firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName")); // firstNameCol.setCellFactory(TextFieldTableCell.<PersonBean> forTableColumn()); // firstNameCol.setOnEditCommit((CellEditEvent<PersonBean, String> t) -> { // // ((PersonBean)t.getTableView().getItems().get(t.getTablePosition().getRow())).setFirstName(t.getNewValue()); // }); // // TableColumn<PersonBean, String> lastNameCol = new TableColumn<PersonBean, String>("Last // Name"); // lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName")); // lastNameCol.setCellFactory(TextFieldTableCell.<PersonBean> forTableColumn()); // lastNameCol.setOnEditCommit((CellEditEvent<PersonBean, String> t) -> { // ((PersonBean) // t.getTableView().getItems().get(t.getTablePosition().getRow())).setLastName(t.getNewValue()); // }); TableColumn<PersonBean, String> emailCol = new TableColumn<PersonBean, String>("Email"); emailCol.setCellValueFactory(new PropertyValueFactory("email")); emailCol.setCellFactory(TextFieldTableCell.<PersonBean>forTableColumn()); emailCol.setOnEditCommit( (CellEditEvent<PersonBean, String> t) -> { ((PersonBean) t.getTableView().getItems().get(t.getTablePosition().getRow())) .setEmail(t.getNewValue()); }); TableColumn<PersonBean, String> genderCol = new TableColumn<PersonBean, String>("Gender"); genderCol.setCellValueFactory(new PropertyValueFactory("gender")); genderCol.setCellFactory(TextFieldTableCell.<PersonBean>forTableColumn()); genderCol.setOnEditCommit( (CellEditEvent<PersonBean, String> t) -> { ((PersonBean) t.getTableView().getItems().get(t.getTablePosition().getRow())) .setGender(t.getNewValue()); }); TableColumn<PersonBean, Integer> ageCol = new TableColumn<PersonBean, Integer>("Age"); ageCol.setCellValueFactory(new PropertyValueFactory("age")); ageCol.setCellFactory( TextFieldTableCell.<PersonBean, Integer>forTableColumn(new IntegerStringConverter())); ageCol.setOnEditCommit( (CellEditEvent<PersonBean, Integer> t) -> { ((PersonBean) t.getTableView().getItems().get(t.getTablePosition().getRow())) .setAge(t.getNewValue()); }); table.getColumns().setAll(wholeNameCol, emailCol, genderCol, ageCol); ageCol.setId("white"); Button allButton = new Button("All"); allButton.setMinSize(100, 10); allButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Show all!"); filteredData.setPredicate(showAll()); } }); Button maleButton = new Button("Male"); maleButton.setMinSize(100, 10); maleButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Show male"); // printIt(); filteredData.setPredicate(isMale()); } }); Button femaleButton = new Button("Female"); femaleButton.setMinSize(100, 10); femaleButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Show female"); filteredData.setPredicate(isFemale()); } }); Button over18Button = new Button("Over 18"); over18Button.setMinSize(100, 10); over18Button.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Above 18"); // filteredData.setPredicate(over18()); filteredData.setPredicate( new Predicate<PersonBean>() { public boolean test(PersonBean p) { return p.getAge() > 17; } }); } }); Button addButton = new Button("Add row"); addButton.setMinSize(100, 10); addButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Add me!"); filteredData.setPredicate(showAll()); addRow(); } }); Button removeButton = new Button("Delete row"); removeButton.setMinSize(100, 10); removeButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("delete me!"); List items = new ArrayList(table.getSelectionModel().getSelectedItems()); personData.removeAll(items); table.getSelectionModel().clearSelection(); } }); // Create a root node. In this case, a flow layout // is used, but several alternatives exist. FlowPane rootNode = new FlowPane(); rootNode.getStylesheets().add("resource/style.css"); rootNode .getChildren() .addAll(table, allButton, maleButton, femaleButton, over18Button, addButton, removeButton); // Create a scene. Scene myScene = new Scene(rootNode, 800, 800); // Set the scene on the stage. myStage.setScene(myScene); // Show the stage and its scene. myStage.show(); }
/** * createTable is static to allow Army to define a table without having any Actor objects present. */ public static TableView<Actor> createTable() { TableView<Actor> table = new TableView<Actor>(); final double PREF_WIDTH_DOUBLE = 80.0; table.setPrefWidth( PREF_WIDTH_DOUBLE * 7.5); // 7.0 because there are 6 individual columns, but one of those is DOUBLE-WIDTH, // and there is some inter-column spacing table.setEditable(true); TableColumn<Actor, String> nameCol = new TableColumn<>("Name"); nameCol.setCellValueFactory(new PropertyValueFactory<Actor, String>("name")); nameCol.setPrefWidth(PREF_WIDTH_DOUBLE * 2.0); TableColumn<Actor, Number> healthCol = new TableColumn<>("Health"); healthCol.setCellValueFactory(cell -> cell.getValue().health); healthCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> strengthCol = new TableColumn<>("Strength"); strengthCol.setCellValueFactory(cell -> cell.getValue().strength); strengthCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> speedCol = new TableColumn<>("Speed"); speedCol.setCellValueFactory(cell -> cell.getValue().speed); speedCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> locationXCol = new TableColumn<>("X"); locationXCol.setCellValueFactory(cell -> cell.getValue().getAvatar().translateXProperty()); locationXCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> locationYCol = new TableColumn<>("Y"); locationYCol.setCellValueFactory(cell -> cell.getValue().getAvatar().translateYProperty()); locationYCol.setPrefWidth(PREF_WIDTH_DOUBLE); ObservableList<TableColumn<Actor, ?>> c = table.getColumns(); c.add(nameCol); c.add(healthCol); c.add(strengthCol); c.add(speedCol); c.add(locationXCol); c.add(locationYCol); // Compare line ABOVE with line BELOW: The BELOW line looks cleaner and does actually work . . . // but the compiler spits out a warning. The ABOVE line accomplishes the same thing, less // elegantly, but without warnings. // table.getColumns().addAll(nameCol, healthCol, strengthCol, speedCol, locationXCol, // locationYCol); // The following code makes each cell in the selected columns editable (Name, Health, Strength, // Speed) // We CANNOT implement edit capabilities on the X/Y columns since they are READ-ONLY. nameCol.setCellFactory(TextFieldTableCell.<Actor>forTableColumn()); nameCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setName(event.getNewValue()); a.resetAvatarAttributes(); }); healthCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); healthCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setHealth((Double) event.getNewValue()); a.resetAvatarAttributes(); }); strengthCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); strengthCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setStrength((Double) event.getNewValue()); a.resetAvatarAttributes(); }); speedCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); speedCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setSpeed((Double) event.getNewValue()); a.resetAvatarAttributes(); }); return table; } // end createTable()