0

I have developed the table view in javaFX which retrieve data from database and display, but it only shows the double values which in price column, I have attach the code here.

public class stockViewController implements Initializable{

    @FXML
    private TextField search_txt;

    @FXML
    private TableView<stock> tableView;

    @FXML
    private TableColumn<stock, String> name12;

    @FXML
    private TableColumn<stock, String> categ12;

    @FXML
    private TableColumn<stock, Double> price12;

    @FXML
    private TableColumn<stock, Integer> qty12;


    @FXML
    private TableColumn<stock, Integer> bill12;


    @FXML
    private Button addNew;

    @FXML
    private Button additem;




    @FXML
    void additems(ActionEvent event) throws IOException{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/stockMain.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));  
        stage.show();


    }

    @FXML
    void viewCategory(ActionEvent event) throws IOException {

       FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/pieChart.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));  
        stage.show();


    }


    @FXML
    void stockmain(ActionEvent event) throws IOException {

       FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/stockMain.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));  
        stage.show();

    }

    //new one
    ObservableList<stock>filltable = FXCollections.observableArrayList();

    public void fillTableValues(){

        try { 
            Connection con = null;
            PreparedStatement preparedStatement = null;
            ResultSet rs =null;
            con = DBConnection.getDBConnection();
            stock st1 = new stock();
            String query = "select * from stock3"; 
            preparedStatement = con.prepareStatement(query);
            rs = preparedStatement.executeQuery();
            while(rs.next()){   
              int bill12 = rs.getInt("bill");
              String categ12 = rs.getString("categ");
              String name12 = rs.getString("name");
              int qty12 = rs.getInt("qty");
              Double price12 = rs.getDouble("price");

              stock st2= new stock();
                           st2.setbill(bill12);
              st2.setcategory(categ12);
              st2.setname(name12);
              st2.setqty(qty12);
              st2.setPrice(price12);
               filltable.add(new stock(bill12,categ12,name12,qty12,price12));
              tableView.setItems(filltable);
            // tableView.setItems(st2.getbill(),st2.getcategory(),st2.getname(),st2.getqty(),st2.getPrice());
              System.out.println(bill12);
              System.out.println(categ12);
              System.out.println(qty12);
              System.out.println(price12);
              //System.out.println(st2);
            }
        } catch (SQLException ex) {

            Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {

            Logger.getLogger(StockFinalFinal.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }  

    @FXML
    private void onSearch(ActionEvent event) {


            Connection con = null;
            PreparedStatement preparedStatement = null;
            ResultSet rs =null;
            if(search_txt.getText().equals("")){    
                fillTableValues();
                JOptionPane.showMessageDialog(null, "Error! No value searched!!");

            }else{
                try {
                 filltable.clear();
                con = DBConnection.getDBConnection();

                String searchquery = "SELECT * from stock3 where price LIKE '%"+search_txt.getText()+"%'";
                preparedStatement = con.prepareStatement(searchquery);
                //preparedStatement.setString(1,search_txt.getText());
                rs = preparedStatement.executeQuery();
                //preparedStatement = con.prepareStatement(searchquery);
                if(rs.next()){


                filltable.add(new stock(rs.getInt(2),rs.getString(4)," "+rs.getString(5),rs.getInt(6),rs.getDouble(7)));

                }
                tableView.setItems(filltable);


            } catch (ClassNotFoundException ex) {
                Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(stockMainController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        bill12.setCellValueFactory(new PropertyValueFactory<>("categ"));
        name12.setCellValueFactory(new PropertyValueFactory<>("categ"));
        categ12.setCellValueFactory(new PropertyValueFactory<>("name"));
        qty12.setCellValueFactory(new PropertyValueFactory<>("qty")); 
        price12.setCellValueFactory(new PropertyValueFactory<>("price"));

        fillTableValues();


    }

}

This only shows the tablecolumn price, But I want to display all the columns which include String (categ12, name12) and Integer (qty12, bill12). So how can I do it, what should I change?

fabian
  • 80,457
  • 12
  • 86
  • 114
  • 2
    Probably the issue is this one: https://stackoverflow.com/a/25503220/2991525 . Furthermore please note that adhering to the java naming conventions makes code easier to read for others. (Type names should start with an uppercase letter and field names (except for `static final` ones) should use camelCase instead of `_`.) – fabian Sep 09 '18 at 17:30
  • Agree with @fabian and I would mention here that you can always avoid such problems by using `Callback` instead of `PropertyValueFactory`. Since `PVF` uses reflection the `Callback` is the way to use. Here is fabian's answer in a question, I suggest reading it : https://stackoverflow.com/questions/38049734/java-setcellvaluefactory-lambda-vs-propertyvaluefactory-advantages-disadvant. I would add a note here too, if you are familiar to java8/javafx8 you can use lambdas in `setCellValueFactory` instead of an anonymous inner class. – Sunflame Sep 10 '18 at 07:15
  • Thanks for guiding me to the correct path. I found the error – Pasan Kamburugamuwa Sep 13 '18 at 12:45

0 Answers0