0

I wants to create 2 table view in scene builder and first table will retrieve data from a mysql table and after which the use choose one of the rows from the first table to view more data of that particular row. How should I do it?

Edit: the closest example i got from google is http://edu.makery.ch/blog/2012/11/16/javafx-tutorial-addressapp-1/ But the data is not retrieve from database. I had added my codes below and the error message that i gotten from 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());
}
}

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start >method at >com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) at com.sun.javafx.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) Caused by: java.lang.IllegalStateException: Location is not set. at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021) at cams.order.main.MainApp.start(MainApp.java:37) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215) at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179) at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73) ... 1 more

user1744424
  • 101
  • 2
  • 7

0 Answers0