This discussion is archived
5 Replies Latest reply: Oct 29, 2011 5:08 AM by Narayan RSS

Populating a TableView

895675 Newbie
Currently Being Moderated
I connected my application with javafx + mysql, but I'm not getting populate o tableview, the rows are empty.

How I do to populate a TableView?

Thanks
  • 1. Re: Populating a TableView
    Narayan Pro
    Currently Being Moderated
    Hello user,

    For more about TableView demo I've blogged here:Dynamic TableView Data from Database

    Thanks.
    Narayan
  • 2. Re: Populating a TableView
    895675 Newbie
    Currently Being Moderated
    My code is returning an error in ObservableList <Observalist> data:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;
    
    
    public class JavaFX extends Application {
         
         
         private ObservableList<ObservableList> data;
         private TableView viewbdr;
         
         public static void main(String args[]){
         
              Application.launch(args);
              
         }     
         
         public void start(Stage view){
                        
              viewbdr = new TableView();
              
              view.setOnShowing(new EventHandler<WindowEvent>(){
                   public void handle(WindowEvent event){
                        
                        ResultSet rs;
                        Statement tabela;
                        String URL = "jdbc:mysql://localhost:3306/sgbdr";
                        String User = "root";
                        String Pass = "";
                        try{
                             Class.forName("com.mysql.jdbc.Driver");
                             Connection Conexao = DriverManager.getConnection(URL,User,Pass);
                             tabela = Conexao.createStatement();
                             String SQL = "SELECT * FROM tbClient";
                             rs = tabela.executeQuery(SQL);
                                                      
                             while(rs.next()){
    
                                  ObservableList<String> row = FXCollections.observableArrayList(rs.getString("Code"),rs.getString("Name"))
                                  data.add(row);     
                        
                             }
                             viewbdr.setItems(row);
                                            rs.close();
                        }
                        catch(ClassNotFoundException ex){
                             System.out.println("Driver Not Found!!!");
                             System.exit(0);
                        }
                        catch(SQLException ex){
                             System.out.println("Error SQL!!!");
                             System.exit(0);
                        }
                   }
              });          
              
              viewbdr.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
              viewbdr.setEditable(false);
              viewbdr.setTableMenuButtonVisible(true);
              
              TableColumn code = new TableColumn("Code");
              TableColumn name = new TableColumn("Name");
              
              viewbdr.getColumns().addAll(code, name);
              
              Group gp = new Group();
              gp.getChildren().add(viewbdr);
              
              Scene sc = new Scene(gp);
              
              view.setScene(sc);
              view.show();
         }
    }
    Edited by: 892672 on 24/10/2011 02:44

    Edited by: 892672 on 27/10/2011 08:18
  • 3. Re: Populating a TableView
    Narayan Pro
    Currently Being Moderated
    Hello user,

    Please format your code properly for making forum helper to help you easily
    Place your codes inside { code } { code }

    Thanks.
    Naaryan
  • 4. Re: Populating a TableView
    895675 Newbie
    Currently Being Moderated
    ???

    Edited by: 892672 on 27/10/2011 10:07
  • 5. Re: Populating a TableView
    Narayan Pro
    Currently Being Moderated
    Hello user,
    Sorry for the delay in response. Because I was so busy on my project. Anyway your problem is that you have not miantained the cellfactory . Now to maintain these things look at these code:
    public class JavaFX extends Application {
    
        private ObservableList<ObservableList> data;
        private TableView viewbdr;
    
        public static void main(String args[]){
    
            Application.launch(args);
    
        }
    
        public void start(Stage view){
    
            viewbdr = new TableView();
            data = FXCollections.observableArrayList();
            view.setOnShowing(new EventHandler<WindowEvent>(){
                public void handle(WindowEvent event){
    
                    ResultSet rs;
                    Statement tabela;
                    String URL = "jdbc:mysql://localhost:3306/sgbdr";
                    String User = "root";
                    String Pass = "";
                    try{
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection Conexao = DriverManager.getConnection(URL,User,Pass);
                        tabela = Conexao.createStatement();
                        String SQL = "SELECT * FROM Customer";
                        rs = tabela.executeQuery(SQL);
    
                        while(rs.next()){
                            ObservableList<String> row = FXCollections.observableArrayList();
                            //All the rows are added here dynamically 
                            /*   To add the all the rows of the table with all columns then use this loop
                                  for(int i =1; i<=rs.getMetaData().getColumnCount(); i++){
                                     row.add(rs.getString(i)); 
                                  }                        
                            */
                            // Or if you like to add row of only specific column then 
                            //you need to use these lines instead of while loop
                            /*    row.add(rs.getString("col1_name"));
                                   row.add(rs.getString("col2_name"));
                                   ..
                                   .
                            */
                            data.add(row);
    
                        }
                        viewbdr.setItems(data);
                        rs.close();
                    }
                    catch(ClassNotFoundException ex){
                        System.out.println("Driver Not Found!!!");
                        System.exit(0);
                    }
                    catch(SQLException ex){
                        ex.printStackTrace();
                        System.out.println("Error SQL!!!");
                        System.exit(0);
                    }
                }
            });
    
            viewbdr.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
            viewbdr.setEditable(false);
            viewbdr.setTableMenuButtonVisible(true);
    
            TableColumn code = new TableColumn("Code");
            code.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>> (){                    
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                              
                    return new SimpleStringProperty(param.getValue().get(0).toString());                        
                }                    
            });
            
            
            TableColumn name = new TableColumn("Name");
            name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>> (){                    
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                              
                    return new SimpleStringProperty(param.getValue().get(1).toString());                        
                }                    
            });
    
            viewbdr.getColumns().addAll(code, name);
    
            Group gp = new Group();
            gp.getChildren().add(viewbdr);
    
            Scene sc = new Scene(gp);
    
            view.setScene(sc);
            view.show();
        }
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points