请,我想在点击tableview行(行选择)时加载FXML文件,并从数据库中检索行的值,并在FXML文件的标签上设置值。
到目前为止我见过的例子并没有实现。
如果有人可以帮助FXML文件加载,我将不胜感激。
我目前的代码:
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
public class Main extends Application {
ObservableList<Person> persons = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
Parent root = FXMLLoader.load(getClass().getResource("personsTable.fxml"));
Scene scene = new Scene(root);
newStage.setScene(newScene);
newStage.show();
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn=DriverManager.getConnection("jdbc:derby://localhost:1527/Employee","Conduct","ccb");
String rest="select *from Employee";
pstmt=conn.prepareStatement(rest);
rs=pstmt.executeQuery();
while(rs.next()){
persons.add(new Person(
rs.getString("name"),
));
staff.setCellValueFactory(new PropertyValueFactory<>("name"));
personsTable.setItems(persons);
personsTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
@Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
// Create a new FXML loader
FXMLLoader loader = new FXMLLoader(getClass().getResource("OnSelection.fxml"));
try {
// Load the another FXML file
Parent newParent = loader.load();
OnSelectionController subController = loader.getController();
// Set the String property
// If you want to use data from the current selection: newValue contains the currently selected Person
// TODO: Get value from DB
subController.textToDisplay.set(newValue.getName());
// newParent contains the root of your other FXML file, do anything that you want to do with it (e.g. add to the current node graph)
// Now I just simply open it in a new window
Stage newStage = new Stage();
Scene newScene = new Scene(newParent);
newStage.setScene(newScene);
newStage.show();
} catch (IOException | SQLException ex) {
Logger.getLogger(ViewController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(ViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:0)
您只需为TableView
的{{3}} selectedItemProperty添加一个监听器,您可以使用selectionModelProperty在其中添加另一个FXML文件。您可以在此FXML文件的控制器上公开一些公共方法和/或属性,并且可以使用此方法和/或属性来设置FXMLLoader
FXMLLoader方法返回的控制器中的数据。
您的规范的简约示例:
在示例中TableView
显示Person
个对象,在选择TableView
时,第二个FXML文件被加载并显示在新的Stage
上。
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 700, 400, Color.WHITE);
TableView<Person> personsTable = new TableView<Person>();
// Add only one Column: Name
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
personsTable.getColumns().add(nameCol);
// On selected item
personsTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
@Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
// Create a new FXML loader
FXMLLoader loader = new FXMLLoader(getClass().getResource("OnSelection.fxml"));
try {
// Load the another FXML file
Parent newParent = loader.load();
OnSelectionController subController = loader.getController();
// Set the String property
// If you want to use data from the current selection: newValue contains the currently selected Person
// TODO: Get value from DB
subController.textToDisplay.set("value from DB for Person" + newValue.getName());
// newParent contains the root of your other FXML file, do anything that you want to do with it (e.g. add to the current node graph)
// Now I just simply open it in a new window
Stage newStage = new Stage();
Scene newScene = new Scene(newParent);
newStage.setScene(newScene);
newStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// Create some dummy data for the table
ObservableList<Person> persons = FXCollections.observableArrayList();
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("Name" + i);
person.setAddress("Address" + i);
person.setCountry("Country" + i);
person.setCourse("Course" + i);
persons.add(person);
}
personsTable.setItems(persons);
}
public static void main(String[] args) {
launch(args);
}
}
Person.java
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
private String name;
private String address;
private String country;
private String course;
}
OnSelectionController.java
public class OnSelectionController implements Initializable {
@FXML private Label label1;
public SimpleStringProperty textToDisplay = new SimpleStringProperty("");
@Override
public void initialize(URL location, ResourceBundle resources) {
// The text that the label displays is bound to the string property value
label1.textProperty().bind(textToDisplay);
}
}
OnSelection.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="191.0" prefWidth="210.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.OnSelectionController">
<!-- TODO Add Nodes -->
<children>
<Label fx:id="label1" layoutX="59.0" layoutY="40.0" text="Label" />
</children>
</AnchorPane>
请参阅代码段中的注释。