Created
September 3, 2021 07:44
-
-
Save Da9el00/3af0ebbacee5edbc70b67ab2c4782866 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Adding and Deleting TableView Rows
This file contains hidden or 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.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
//Table | |
@FXML | |
private TableView<Customer> tableView; | |
//Columns | |
@FXML | |
private TableColumn<Customer, String> nameColumn; | |
@FXML | |
private TableColumn<Customer, Integer> ageColumn; | |
@FXML | |
private TableColumn<Customer, Integer> numberColumn; | |
//Text input | |
@FXML | |
private TextField nameInput; | |
@FXML | |
private TextField ageInput; | |
@FXML | |
private TextField numberInput; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
nameColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("name")); | |
ageColumn.setCellValueFactory(new PropertyValueFactory<Customer, Integer>("age")); | |
numberColumn.setCellValueFactory(new PropertyValueFactory<Customer, Integer>("number")); | |
} | |
//Submit button | |
@FXML | |
void submit(ActionEvent event) { | |
Customer customer = new Customer(nameInput.getText(), | |
Integer.parseInt(ageInput.getText()), | |
Integer.parseInt(numberInput.getText())); | |
ObservableList<Customer> customers = tableView.getItems(); | |
customers.add(customer); | |
tableView.setItems(customers); | |
} | |
@FXML | |
void removeCustomer(ActionEvent event) { | |
int selectedID = tableView.getSelectionModel().getSelectedIndex(); | |
tableView.getItems().remove(selectedID); | |
} | |
} |
This file contains hidden or 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; | |
public class Customer { | |
private String name; | |
private int age; | |
private int number; | |
public Customer(String name, int age, int number) { | |
this.name = name; | |
this.age = age; | |
this.number = number; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public int getNumber() { | |
return number; | |
} | |
public void setNumber(int number) { | |
this.number = number; | |
} | |
} |
This file contains hidden or 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)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or 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.Button?> | |
<?import javafx.scene.control.TableColumn?> | |
<?import javafx.scene.control.TableView?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.VBox?> | |
<?import javafx.scene.text.Text?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<TableView fx:id="tableView" layoutX="337.0" layoutY="73.0" prefHeight="255.0" prefWidth="225.0"> | |
<columns> | |
<TableColumn fx:id="nameColumn" prefWidth="75.0" text="Name" /> | |
<TableColumn fx:id="ageColumn" prefWidth="75.0" text="Age" /> | |
<TableColumn fx:id="numberColumn" prefWidth="75.0" text="Number" /> | |
</columns> | |
</TableView> | |
<VBox layoutX="68.0" layoutY="101.0" prefHeight="200.0" prefWidth="200.0" spacing="5.0"> | |
<children> | |
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Name" /> | |
<TextField fx:id="nameInput" /> | |
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Age" /> | |
<TextField fx:id="ageInput" /> | |
<Text layoutX="10.0" layoutY="75.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Number" /> | |
<TextField fx:id="numberInput" /> | |
<Button mnemonicParsing="false" onAction="#submit" prefHeight="25.0" prefWidth="200.0" text="Submit" /> | |
</children> | |
</VBox> | |
<Button layoutX="420.0" layoutY="349.0" mnemonicParsing="false" onAction="#removeCustomer" text="Remove" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment