package javafxtemplate1; //TableView_IntegerColumn1.htm import javafx.application.Application; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; //import javafx.event.ActionEvent; //import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; //import javafx.scene.control.Button; //import javafx.scene.control.Label; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author Manas9 */ public class JavaFXTemplate1 extends Application { public String str1; // private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); final ObservableList data = FXCollections.observableArrayList(); data.add(new obsEMP("Dev", "Prakash", "Manager IT", 101)); data.add(new obsEMP("Pamella", "Tandon", "Manager Tech Sales", 102)); data.add(new obsEMP("Rossita", "Sanders", "Tech Engineer", 103)); data.add(new obsEMP("Manoj", "Saxena", "Sales Engineer", 104)); data.add(new obsEMP("Anthony", "Powell", "Sales Engineer",105)); data.add(new obsEMP("Janes", "Brown", "Sales Clerk", 106)); TableColumn firstNameCol = new TableColumn(); firstNameCol.setText("First"); firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName")); TableColumn lastNameCol = new TableColumn(); lastNameCol.setText("Last"); lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName")); TableColumn jobCol = new TableColumn(); jobCol.setText("job"); jobCol.setMinWidth(50); jobCol.setCellValueFactory(new PropertyValueFactory("job")); TableColumn empIDCol = new TableColumn(); empIDCol.setText("EmpID"); empIDCol.setMinWidth(50); empIDCol.setCellValueFactory(new PropertyValueFactory("empID")); //Formatting integer columns empIDCol.setCellFactory(new Callback() { public TableCell call(TableColumn p) { TableCell cell = new TableCell() { @Override public void updateItem(Integer item, boolean empty) { super.updateItem(item, empty); setText(empty ? null : getString()); setGraphic(null); } private String getString() { String ret = ""; if (getItem() != null) { ret = getItem().toString(); } else { ret = "0"; } return ret; } }; cell.setStyle("-fx-alignment: top-right;"); return cell; } }); //Formatting integer columns end TableView tableView = new TableView(); tableView.setItems(data); tableView.getColumns().addAll(empIDCol,firstNameCol, lastNameCol, jobCol); root.getChildren().add(tableView); } public static class obsEMP { private final StringProperty firstName; private final StringProperty lastName; private final StringProperty job; private final IntegerProperty pID; //private final DoubleProperty Salary; private obsEMP(String fName, String lName, String job, int empID) { this.firstName = new SimpleStringProperty(fName); this.lastName = new SimpleStringProperty(lName); this.job = new SimpleStringProperty(job); this.pID = new SimpleIntegerProperty(empID); } // below, property name must start with param name //like empID should have empIDProperty else integer returns 0 public StringProperty firstNameProperty() { return firstName; } public StringProperty lastNameProperty() { return lastName; } public StringProperty jobProperty() { return job; } public IntegerProperty empIDProperty() { return pID; } } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }