2

I am trying to display my sql query result in listview in JavaFX. Below is my code which does not display anything in list view, shows blank.

Here observableArrayList() takes argument in list of String like "hello", "all" like wise I passed in it arraylist reference variable. though it is not showing result? where am I wrong?

Code ...

 ArrayList<String> arr = new ArrayList<>(10);

    @FXML
    public void dispalyData(){
        String sql = "SELECT cus_id, cus_name, cus_email FROM customer_detail";

        try{
            Connection conn = DataConnect.connect();
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            int i=0;
            while(rs.next()){
            arr.add(rs.getInt("cus_id") +"\t"+ rs.getString("cus_name") 
                              +"\t"+ rs.getString("cus_email"));
            }

            System.out.println(Arrays.asList(arr.toArray()));

        }
        catch(SQLException e){
            System.out.println(e.getMessage());
        } 
    }



    ObservableList<String> list = FXCollections.observableArrayList(arr);

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        viewlist.setItems(list);
    }      
3
  • Did you right write @fxml id ? Commented Jan 27, 2018 at 6:35
  • Initialize method works like 'post constructor' method. You init FXCollections.observableArrayList(arr); from empty arraylist...FXCollections.observableArrayList(arr); doesn't refer arraylist and observablearraylist, it's only copy elements. You decision -> add items into observablearraylist. Commented Jan 27, 2018 at 6:50
  • tip :if you don't have reputation, add comments into question paragraph Commented Jan 27, 2018 at 7:04

2 Answers 2

2

FXCollections.observableArrayList(Collection) copies the elements of the parameter to the ObservableList created. Even if you use FXCollections.observableList(List) which uses the object passed as parameter as a backing list, modifying the backing list does not trigger change events for the ObservableList. ListView relies on these events however to update itself...

Work with list in your dispalyData instead:

@FXML
public void dispalyData(){
    String sql = "SELECT cus_id, cus_name, cus_email FROM customer_detail";

    list.clear();
    try{
        Connection conn = DataConnect.connect();
        PreparedStatement pst = conn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();

        while(rs.next()){
            list.add(rs.getInt("cus_id") +"\t"+ rs.getString("cus_name") 
                          +"\t"+ rs.getString("cus_email"));
        }

        System.out.println(Arrays.asList(list.toArray()));

    } catch(SQLException e){
        System.out.println(e.getMessage());
    } 
}

ObservableList<String> list = FXCollections.observableArrayList();
Sign up to request clarification or add additional context in comments.

Comments

0

You are initializing list before running method displayData. You need to do this in the method. I suggest you add the following line after the end of your while loop.

list = FXCollections.observableArrayList(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.