我想在场景构建器中创建 2 个表视图,第一个表将从 mysql 表中检索数据,然后使用从第一个表中选择一个行来查看该特定行的更多数据。我该怎么做?
编辑:我从谷歌得到的最接近的例子是http://edu.makery.ch/blog/2012/11/16/javafx-tutorial-addressapp-1/ 但数据不是从数据库中检索的。我在下面添加了我的代码以及我从 Eclipse 获得的错误消息。
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Food> foodData = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Canteen Management System");
try {
// Load the root layout from the fxml file
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
showPersonOverview();
}
public void showPersonOverview() {
try {
// Load the fxml file and set into the center of the main layout
// FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("cams/order/view/OrderMenu.fxml"));
// AnchorPane overviewPage = (AnchorPane) loader.load();
// rootLayout.setCenter(overviewPage);
// FoodController controller = loader.getController();
FoodController controller = (FoodController) replaceSceneContent("cams/order/view/OrderMenu.fxml");
controller.setMainApp(this);
} catch (Exception ex) {//IOException e
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
// Exception gets thrown if the fxml file could not be loaded
//e.printStackTrace();
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = MainApp.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(MainApp.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page, 1280, 800);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
return (Initializable) loader.getController();
}
public ObservableList<Food> getPersonData() {
return foodData;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
public class FoodController implements Initializable{
@FXML
private TableView<Food> tblViewer = new TableView<Food>();
@FXML
private TableColumn<Food, String> foodPicture;
@FXML
private TableColumn<Food, String> foodName;
@FXML
private TableColumn<Food, Integer> foodPrice;
@FXML
private TableColumn<Food, Integer> foodID;
private MainApp mainApp;
@Override
public void initialize(URL url, ResourceBundle rb) {
// foodID.setCellValueFactory(new PropertyValueFactory<Food, Integer> ("id"));
// foodPrice.setCellValueFactory(new PropertyValueFactory<Food, Integer>("price"));
foodName.setCellValueFactory(new PropertyValueFactory<Food, String>("name"));
foodPicture.setCellValueFactory(new PropertyValueFactory<Food, String>("picture"));
tblViewer.getItems().setAll(getAllFoodInfo());
}
public List<Food> getAllFoodInfo(){
Connection conn;
List ll = new LinkedList();
Statement st;
ResultSet rs;
String url = "jdbc:mysql://localhost/cams";
String user = "root";
String pass = "admin";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pass);
st = conn.createStatement();
String recordQuery = ("Select * from food");
rs = st.executeQuery(recordQuery);
while(rs.next()){
Integer id = rs.getInt("id");
double price = rs.getDouble("price");
String name = rs.getString("name");
String picture = rs.getString("picture");
ll.add(new Food(id, price, name, picture));
System.out.println(id +","+ price +","+ name +","+ picture +" "+"added.");
}
}catch(ClassNotFoundException | SQLException ex){
Logger.getLogger(FoodController.class.getName()).log(Level.SEVERE, null, ex);
}
return ll;
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
tblViewer.setItems(mainApp.getPersonData());
}
}
应用程序启动方法中的异常线程“main”java.lang.RuntimeException 中的异常:应用程序启动中的异常> com.sun.javafx 处>com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 的方法。 application.LauncherImpl.access$000(LauncherImpl.java:47) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) at java.lang.Thread.run(Unknown Source) 原因:java。 lang.IllegalStateException:未设置位置。在 javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021) 在 cams.order.main.MainApp.start(MainApp.java:37) 在 com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java: 319) 在 com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215) 在 com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179) 在 com.