Projects and artifacts for JavaFx - started 7 Nov 2015
Created
November 17, 2017 23:34
-
-
Save MitchDresdner/73fb83ed01355fd17b30a8bc75906c9f to your computer and use it in GitHub Desktop.
Git cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.idea/ | |
out/ | |
JavaFx.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CompilerConfiguration"> | |
<bytecodeTargetLevel target="1.8" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding"> | |
<file url="PROJECT" charset="UTF-8" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/LearningJFx.iml" filepath="$PROJECT_DIR$/LearningJFx.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="VcsDirectoryMappings"> | |
<mapping directory="" vcs="Git" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.root{ | |
-fx-background-color: #383838; | |
} | |
.label{ | |
-fx-text-fill: #e8e8e8; | |
} | |
.button{ | |
-fx-background-color: #AB4642; | |
-fx-text-fill: #FFFFFF; | |
-fx-background-radius: 4; | |
} | |
.button-blue{ | |
-fx-background-color: #7cafc2; | |
-fx-text-fill: #FFFFFF; | |
-fx-background-radius: 4; | |
} | |
#bold-label{ | |
-fx-font-weight: bold; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.bindings; | |
import javafx.application.Application; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
Stage window; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
window = primaryStage; | |
window.setTitle("JavaFx - Bindings"); | |
//Input and labels | |
TextField userInput = new TextField(); | |
userInput.setMaxWidth(200); | |
Label firstLabel = new Label("Welcome to the site "); | |
Label secondLabel = new Label(); | |
HBox bottomText = new HBox(firstLabel, secondLabel); | |
bottomText.setAlignment(Pos.CENTER); | |
VBox vBox = new VBox(10, userInput, bottomText); | |
vBox.setAlignment(Pos.CENTER); | |
secondLabel.textProperty().bind(userInput.textProperty()); | |
Scene scene = new Scene(vBox, 300, 200); | |
window.setScene(scene); | |
window.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.fxml.Initializable; | |
public class Controller implements Initializable { | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
System.out.println("View is now loaded!"); | |
} | |
protected void loginCB () { | |
System.out.println("Login callback"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.css; | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.GridPane; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
Stage window; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
window = primaryStage; | |
window.setTitle("JavaFX - CSS Sample"); | |
//GridPane with 10px padding around edge | |
GridPane grid = new GridPane(); | |
grid.setPadding(new Insets(10, 10, 10, 10)); | |
grid.setVgap(8); | |
grid.setHgap(10); | |
//Name Label - constrains use (child, column, row) | |
Label nameLabel = new Label("Username:"); | |
nameLabel.setId("bold-label"); | |
GridPane.setConstraints(nameLabel, 0, 0); | |
//Name Input | |
TextField nameInput = new TextField("Foodle"); | |
GridPane.setConstraints(nameInput, 1, 0); | |
//Password Label | |
Label passLabel = new Label("Password:"); | |
GridPane.setConstraints(passLabel, 0, 1); | |
//Password Input | |
TextField passInput = new TextField(); | |
passInput.setPromptText("password"); | |
GridPane.setConstraints(passInput, 1, 1); | |
//Login | |
Button loginButton = new Button("Log In"); | |
GridPane.setConstraints(loginButton, 1, 2); | |
//Sign up | |
Button signUpButton = new Button("Sign Up"); | |
signUpButton.getStyleClass().add("button-blue"); | |
GridPane.setConstraints(signUpButton, 1, 3); | |
//Add everything to grid | |
grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, loginButton, signUpButton); | |
Scene scene = new Scene(grid, 300, 200); | |
scene.getStylesheets().add("resources/css/sample.css"); | |
window.setScene(scene); | |
window.show(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.fxml; | |
import javafx.fxml.Initializable; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
System.out.println("View is now loaded!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.fxml; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("JavaFx - FXml"); | |
primaryStage.setScene(new Scene(root, 300, 275)); | |
primaryStage.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml" | |
fx:controller="sample.Controller"> | |
<Label text="I love bacon"/> | |
<Button text="Submit"/> | |
</VBox> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root, 300, 275)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.geometry.Insets?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.control.Menu?> | |
<?import javafx.scene.control.MenuBar?> | |
<?import javafx.scene.control.MenuItem?> | |
<?import javafx.scene.control.TextArea?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.control.TreeView?> | |
<?import javafx.scene.layout.BorderPane?> | |
<?import javafx.scene.layout.HBox?> | |
<?import javafx.scene.layout.Region?> | |
<?import javafx.scene.layout.VBox?> | |
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<top> | |
<VBox BorderPane.alignment="CENTER"> | |
<children> | |
<MenuBar> | |
<menus> | |
<Menu mnemonicParsing="false" text="File"> | |
<items> | |
<MenuItem mnemonicParsing="false" text="Close" /> | |
</items> | |
</Menu> | |
<Menu mnemonicParsing="false" text="Edit"> | |
<items> | |
<MenuItem mnemonicParsing="false" text="Delete" /> | |
</items> | |
</Menu> | |
<Menu mnemonicParsing="false" text="Help"> | |
<items> | |
<MenuItem mnemonicParsing="false" text="About" /> | |
</items> | |
</Menu> | |
</menus> | |
</MenuBar> | |
<HBox spacing="10.0"> | |
<children> | |
<TextField promptText="username" /> | |
<TextField layoutX="10.0" layoutY="10.0" promptText="password" /> | |
<Region HBox.hgrow="ALWAYS" /> | |
<Button mnemonicParsing="false" onAction="#loginCB" text="Log In" /> | |
<Button layoutX="308.0" layoutY="10.0" mnemonicParsing="false" text="Settings" /> | |
</children> | |
<VBox.margin> | |
<Insets bottom="8.0" left="8.0" right="8.0" top="8.0" /> | |
</VBox.margin> | |
</HBox> | |
</children> | |
</VBox> | |
</top> | |
<left> | |
<TreeView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> | |
</left> | |
<center> | |
<TextArea prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> | |
</center> | |
<bottom> | |
<HBox BorderPane.alignment="CENTER"> | |
<children> | |
<Label text="Label" /> | |
</children> | |
<BorderPane.margin> | |
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" /> | |
</BorderPane.margin> | |
</HBox> | |
</bottom> | |
</BorderPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.tableview; | |
import javafx.application.Application; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.image.Image; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
Stage window; | |
TableView<Product> table; | |
TextField nameInput, priceInput, quantityInput; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
window = primaryStage; | |
window.setTitle("JavaFX - Samples"); | |
//Name column | |
TableColumn<Product, String> nameColumn = new TableColumn<>("Name"); | |
nameColumn.setMinWidth(200); | |
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); | |
//Price column | |
TableColumn<Product, Double> priceColumn = new TableColumn<>("Price"); | |
priceColumn.setMinWidth(100); | |
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price")); | |
//Quantity column | |
TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity"); | |
quantityColumn.setMinWidth(100); | |
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); | |
//Name input | |
nameInput = new TextField(); | |
nameInput.setPromptText("Name"); | |
nameInput.setMinWidth(100); | |
//Price input | |
priceInput = new TextField(); | |
priceInput.setPromptText("Price"); | |
//Quantity input | |
quantityInput = new TextField(); | |
quantityInput.setPromptText("Quantity"); | |
//Button | |
Button addButton = new Button("Add"); | |
addButton.setOnAction(e -> addButtonClicked()); | |
Button deleteButton = new Button("Delete"); | |
deleteButton.setOnAction(e -> deleteButtonClicked()); | |
HBox hBox = new HBox(); | |
hBox.setPadding(new Insets(10,10,10,10)); | |
hBox.setSpacing(10); | |
hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton); | |
table = new TableView<>(); | |
table.setItems(getProduct()); | |
table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); | |
VBox vBox = new VBox(); | |
vBox.getChildren().addAll(table, hBox); | |
Scene scene = new Scene(vBox); | |
// Good: window.getIcons().add(new Image("http://goo.gl/kYEQl")); | |
//window.getIcons().add(new Image("resources/css/Dominion-Logo_64.ico")); | |
window.getIcons().add(new Image("resources/images/Logo400.png")); | |
// Bad: no likey .ico: window.getIcons().add(new Image("resources/images/Dominion-Logo_64.ico")); | |
window.setScene(scene); | |
window.show(); | |
} | |
//Add button clicked | |
public void addButtonClicked(){ | |
Product product = new Product(); | |
product.setName(nameInput.getText()); | |
product.setPrice(Double.parseDouble(priceInput.getText())); | |
product.setQuantity(Integer.parseInt(quantityInput.getText())); | |
table.getItems().add(product); | |
nameInput.clear(); | |
priceInput.clear(); | |
quantityInput.clear(); | |
} | |
//Delete button clicked | |
public void deleteButtonClicked(){ | |
ObservableList<Product> productSelected, allProducts; | |
allProducts = table.getItems(); | |
productSelected = table.getSelectionModel().getSelectedItems(); | |
productSelected.forEach(allProducts::remove); | |
} | |
//Get all of the products | |
public ObservableList<Product> getProduct(){ | |
ObservableList<Product> products = FXCollections.observableArrayList(); | |
products.add(new Product("Laptop", 859.00, 20)); | |
products.add(new Product("Bouncy Ball", 2.49, 198)); | |
products.add(new Product("Toilet", 99.00, 74)); | |
products.add(new Product("The Notebook DVD", 19.99, 12)); | |
products.add(new Product("Corn", 1.49, 856)); | |
return products; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.tableview; | |
public class Product { | |
private String name; | |
private double price; | |
private int quantity; | |
public Product(){ | |
this.name = ""; | |
this.price = 0; | |
this.quantity = 0; | |
} | |
public Product(String name, double price, int quantity){ | |
this.name = name; | |
this.price = price; | |
this.quantity = quantity; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public int getQuantity() { | |
return quantity; | |
} | |
public void setQuantity(int quantity) { | |
this.quantity = quantity; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.tableviewedit; | |
import javafx.application.Application; | |
import javafx.beans.property.SimpleDoubleProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.EventHandler; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.ContentDisplay; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.input.KeyCode; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
public class JavaFXMultiColumnChart extends Application { | |
public class Record { | |
private SimpleStringProperty fieldMonth; | |
private SimpleDoubleProperty fieldValue1; | |
private SimpleDoubleProperty fieldValue2; | |
Record(String fMonth, double fValue1, double fValue2) { | |
this.fieldMonth = new SimpleStringProperty(fMonth); | |
this.fieldValue1 = new SimpleDoubleProperty(fValue1); | |
this.fieldValue2 = new SimpleDoubleProperty(fValue2); | |
} | |
public void setFieldMonth(String fMonth) { | |
fieldMonth.set(fMonth); | |
} | |
void setFieldValue1(Double fValue1) { | |
fieldValue1.set(fValue1); | |
} | |
void setFieldValue2(Double fValue2) { | |
fieldValue2.set(fValue2); | |
} | |
} | |
private TableView<Record> tableView = new TableView<>(); | |
private ObservableList<Record> dataList = | |
FXCollections.observableArrayList( | |
new Record("January", 100, 120), | |
new Record("February", 200, 210), | |
new Record("March", 50, 70), | |
new Record("April", 75, 50), | |
new Record("May", 110, 120), | |
new Record("June", 300, 200), | |
new Record("July", 111, 100), | |
new Record("August", 30, 50), | |
new Record("September", 75, 70), | |
new Record("October", 55, 50), | |
new Record("November", 225, 225), | |
new Record("December", 99, 100)); | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
primaryStage.setTitle("java-buddy.blogspot.com"); | |
Group root = new Group(); | |
tableView.setEditable(true); | |
Callback<TableColumn, TableCell> cellFactory = | |
new Callback<TableColumn, TableCell>() { | |
@Override | |
public TableCell call(TableColumn p) { | |
return new EditingCell(); | |
} | |
}; | |
TableColumn columnMonth = new TableColumn("Month"); | |
columnMonth.setCellValueFactory( | |
new PropertyValueFactory<>("fieldMonth")); | |
columnMonth.setMinWidth(60); | |
TableColumn columnValue1 = new TableColumn("Value 1"); | |
columnValue1.setCellValueFactory( | |
new PropertyValueFactory<Record, Double>("fieldValue1")); | |
columnValue1.setMinWidth(60); | |
TableColumn columnValue2 = new TableColumn("Value 2"); | |
columnValue2.setCellValueFactory( | |
new PropertyValueFactory<Record, Double>("fieldValue2")); | |
columnValue2.setMinWidth(60); | |
//--- Add for Editable Cell of Value field, in Double | |
columnValue1.setCellFactory(cellFactory); | |
columnValue1.setOnEditCommit( | |
new EventHandler<TableColumn.CellEditEvent<Record, Double>>() { | |
@Override | |
public void handle(TableColumn.CellEditEvent<Record, Double> t) { | |
((Record) t.getTableView().getItems().get( | |
t.getTablePosition().getRow())).setFieldValue1(t.getNewValue()); | |
} | |
}); | |
columnValue2.setCellFactory(cellFactory); | |
columnValue2.setOnEditCommit( | |
new EventHandler<TableColumn.CellEditEvent<Record, Double>>() { | |
@Override | |
public void handle(TableColumn.CellEditEvent<Record, Double> t) { | |
((Record) t.getTableView().getItems().get( | |
t.getTablePosition().getRow())).setFieldValue2(t.getNewValue()); | |
} | |
}); | |
//--- | |
tableView.setItems(dataList); | |
tableView.getColumns().addAll(columnMonth, columnValue1, columnValue2); | |
VBox vBox = new VBox(); | |
vBox.setSpacing(10); | |
vBox.getChildren().add(tableView); | |
root.getChildren().add(vBox); | |
primaryStage.setScene(new Scene(root, 300, 350)); | |
primaryStage.show(); | |
} | |
class EditingCell extends TableCell<Record, Double> { | |
private TextField textField; | |
EditingCell() { | |
} | |
@Override | |
public void startEdit() { | |
super.startEdit(); | |
if (textField == null) { | |
createTextField(); | |
} | |
setGraphic(textField); | |
setContentDisplay(ContentDisplay.GRAPHIC_ONLY); | |
textField.selectAll(); | |
} | |
@Override | |
public void cancelEdit() { | |
super.cancelEdit(); | |
setText(String.valueOf(getItem())); | |
setContentDisplay(ContentDisplay.TEXT_ONLY); | |
} | |
@Override | |
public void updateItem(Double item, boolean empty) { | |
super.updateItem(item, empty); | |
if (empty) { | |
setText(null); | |
setGraphic(null); | |
} else { | |
if (isEditing()) { | |
if (textField != null) { | |
textField.setText(getString()); | |
} | |
setGraphic(textField); | |
setContentDisplay(ContentDisplay.GRAPHIC_ONLY); | |
} else { | |
setText(getString()); | |
setContentDisplay(ContentDisplay.TEXT_ONLY); | |
} | |
} | |
} | |
private void createTextField() { | |
textField = new TextField(getString()); | |
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2); | |
textField.setOnKeyPressed(t -> { | |
if (t.getCode() == KeyCode.ENTER) { | |
commitEdit(Double.parseDouble(textField.getText())); | |
} else if (t.getCode() == KeyCode.ESCAPE) { | |
cancelEdit(); | |
} | |
}); | |
} | |
private String getString() { | |
return getItem() == null ? "" : getItem().toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.tblviewedit; | |
import javafx.application.Application; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableColumn.CellEditEvent; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.control.cell.TextFieldTableCell; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
public class TableViewSample extends Application { | |
private TableView<Person> table = new TableView<Person>(); | |
private final ObservableList<Person> data = | |
FXCollections.observableArrayList( | |
new Person("Jacob", "Smith", "[email protected]"), | |
new Person("Isabella", "Johnson", "[email protected]"), | |
new Person("Ethan", "Williams", "[email protected]"), | |
new Person("Emma", "Jones", "[email protected]"), | |
new Person("Michael", "Brown", "[email protected]")); | |
final HBox hb = new HBox(); | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) { | |
Scene scene = new Scene(new Group()); | |
stage.setTitle("Table View Sample"); | |
stage.setWidth(450); | |
stage.setHeight(550); | |
final Label label = new Label("Address Book"); | |
label.setFont(new Font("Arial", 20)); | |
table.setEditable(true); | |
TableColumn firstNameCol = new TableColumn("First Name"); | |
firstNameCol.setMinWidth(100); | |
firstNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("firstName")); | |
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
firstNameCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setFirstName(t.getNewValue()); | |
} | |
} | |
); | |
TableColumn lastNameCol = new TableColumn("Last Name"); | |
lastNameCol.setMinWidth(100); | |
lastNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("lastName")); | |
lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
lastNameCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setLastName(t.getNewValue()); | |
} | |
} | |
); | |
TableColumn emailCol = new TableColumn("Email"); | |
emailCol.setMinWidth(200); | |
emailCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("email")); | |
emailCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
emailCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setEmail(t.getNewValue()); | |
} | |
} | |
); | |
table.setItems(data); | |
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol); | |
final TextField addFirstName = new TextField(); | |
addFirstName.setPromptText("First Name"); | |
addFirstName.setMaxWidth(firstNameCol.getPrefWidth()); | |
final TextField addLastName = new TextField(); | |
addLastName.setMaxWidth(lastNameCol.getPrefWidth()); | |
addLastName.setPromptText("Last Name"); | |
final TextField addEmail = new TextField(); | |
addEmail.setMaxWidth(emailCol.getPrefWidth()); | |
addEmail.setPromptText("Email"); | |
final Button addButton = new Button("Add"); | |
addButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent e) { | |
data.add(new Person( | |
addFirstName.getText(), | |
addLastName.getText(), | |
addEmail.getText())); | |
addFirstName.clear(); | |
addLastName.clear(); | |
addEmail.clear(); | |
} | |
}); | |
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton); | |
hb.setSpacing(3); | |
final VBox vbox = new VBox(); | |
vbox.setSpacing(5); | |
vbox.setPadding(new Insets(10, 0, 0, 10)); | |
vbox.getChildren().addAll(label, table, hb); | |
((Group) scene.getRoot()).getChildren().addAll(vbox); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static class Person { | |
private final SimpleStringProperty firstName; | |
private final SimpleStringProperty lastName; | |
private final SimpleStringProperty email; | |
private Person(String fName, String lName, String email) { | |
this.firstName = new SimpleStringProperty(fName); | |
this.lastName = new SimpleStringProperty(lName); | |
this.email = new SimpleStringProperty(email); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String fName) { | |
firstName.set(fName); | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String fName) { | |
lastName.set(fName); | |
} | |
public String getEmail() { | |
return email.get(); | |
} | |
public void setEmail(String fName) { | |
email.set(fName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment