Created
November 23, 2021 18:21
-
-
Save Da9el00/42fc78bf432ce7ea2d2570f74e4311bd to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Edit TableView
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 Animal { | |
private int id; | |
private String type; | |
private String name; | |
public Animal(int id, String type, String name) { | |
this.id = id; | |
this.type = type; | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getType() { | |
return type; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Animal{" + | |
"id=" + id + | |
", type='" + type + '\'' + | |
", name='" + name + '\'' + | |
'}'; | |
} | |
} |
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 javafx.scene.input.MouseEvent; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@FXML | |
private TableView<Animal> animals; | |
@FXML | |
private TableColumn<Animal, Integer> idCol; | |
@FXML | |
private TableColumn<Animal, String> typeCol; | |
@FXML | |
private TableColumn<Animal, String> nameCol; | |
@FXML | |
private TextField inputId; | |
@FXML | |
private TextField inputType; | |
@FXML | |
private TextField inputName; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
idCol.setCellValueFactory(new PropertyValueFactory<Animal, Integer>("id")); | |
typeCol.setCellValueFactory(new PropertyValueFactory<Animal, String>("type")); | |
nameCol.setCellValueFactory(new PropertyValueFactory<Animal, String>("name")); | |
setupTable(); | |
} | |
@FXML | |
void submit(ActionEvent event) { | |
ObservableList<Animal> currentTableData = animals.getItems(); | |
int currentAnimalId = Integer.parseInt(inputId.getText()); | |
for (Animal animal : currentTableData) { | |
if(animal.getId() == currentAnimalId) { | |
animal.setType(inputType.getText()); | |
animal.setName(inputName.getText()); | |
animals.setItems(currentTableData); | |
animals.refresh(); | |
break; | |
} | |
} | |
} | |
@FXML | |
void rowClicked(MouseEvent event) { | |
Animal clickedAnimal = animals.getSelectionModel().getSelectedItem(); | |
inputId.setText(String.valueOf(clickedAnimal.getId())); | |
inputType.setText(String.valueOf(clickedAnimal.getType())); | |
inputName.setText(String.valueOf(clickedAnimal.getName())); | |
} | |
private void setupTable(){ | |
Animal animal0 = new Animal(0,"Dog","Buddy"); | |
Animal animal1 = new Animal(1,"Cat","Bella"); | |
Animal animal2 = new Animal(2,"Bear","Bob"); | |
Animal animal3 = new Animal(3,"Squid","Laila"); | |
animals.getItems().addAll(animal0,animal1,animal2,animal3); | |
} | |
} |
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="animals" layoutX="309.0" layoutY="77.0" onMouseClicked="#rowClicked" prefHeight="246.0" prefWidth="225.0"> | |
<columns> | |
<TableColumn fx:id="idCol" prefWidth="75.0" text="ID" /> | |
<TableColumn fx:id="typeCol" prefWidth="75.0" text="Type" /> | |
<TableColumn fx:id="nameCol" prefWidth="75.0" text="Name" /> | |
</columns> | |
</TableView> | |
<VBox layoutX="67.0" layoutY="100.0" prefHeight="200.0" prefWidth="160.0" spacing="5.0"> | |
<children> | |
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="ID:" /> | |
<TextField fx:id="inputId" disable="true" /> | |
<Text layoutX="10.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Type:" /> | |
<TextField fx:id="inputType" /> | |
<Text layoutX="10.0" layoutY="265.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Name:" /> | |
<TextField fx:id="inputName" /> | |
<Button mnemonicParsing="false" onAction="#submit" prefHeight="25.0" prefWidth="160.0" text="Submit" /> | |
</children> | |
</VBox> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment