2

The TableView has a built-in scroll bar, but why do I not see the vertical scroll bar?

Here is my code when I put TableView in a HBox:

HBox HBox_tabelDatabase = new HBox();
                HBox_tabelDatabase.setAlignment(Pos.TOP_CENTER);
                HBox_tabelDatabase.setPadding(new Insets(5,5,5,5));
                HBox_tabelDatabase.setStyle(  "-fx-border-style: solid;"
                + "-fx-border-width: 1;"
                + "-fx-border-color: #ADDFFF;"
                + "-fx-accent: #ADDFFF;"
                + "-fx-accent: #ADDFFF;"
               + "-fx-base:  #ADDFFF;"
                + "-fx-background: white;"
                + "-fx-control-inner-background: white;"
                + "-fx-focus-color: #ADDFFF;"
                + "-fx-dark-text-color: deepskyblue;"
                + "-fx-mid-text-color: black;"
                + "-fx-light-text-color: blue;");

Tab tab6 = new Tab();

HBox_tabelDatabase.getChildren().add(tableview);
HBox_tabelDatabase.setPrefHeight(200);

tab6.setText("Tabel Database");
tab6.setContent(HBox_tabelDatabase);

Here is my code to define TableView and its columns and rows:

private void buildData(){
          Connection c ;
          data = FXCollections.observableArrayList();
          try{
            c = DBConnect.connect();
            //SQL FOR SELECTING ALL OF APP.tabelJembatan
            String SQL = "SELECT * from APP.tabelJembatan";
            //ResultSet
            ResultSet rs = c.createStatement().executeQuery(SQL);

            /**********************************
             * TABLE COLUMN ADDED DYNAMICALLY *
             **********************************/
            for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
                //We are using non property style for making dynamic table
                final int j = i;                
                TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
                col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                    
                    public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                              
                        return new SimpleStringProperty(param.getValue().get(j).toString());                        
                    }                    
                });

                tableview.getColumns().addAll(col); 
                col.setMinWidth(200);
                System.out.println("Column ["+i+"] ");
            }

            /********************************
             * Data added to ObservableList *
             ********************************/
            while(rs.next()){
                //Iterate Row
                ObservableList<String> row = FXCollections.observableArrayList();
                for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
                    //Iterate Column
                    row.add(rs.getString(i));
                }
                System.out.println("Row [1] added "+row );
                data.add(row);

            }

            //FINALLY ADDED TO TableView
            tableview.setItems(data);
          }catch(Exception e){
              e.printStackTrace();
              System.out.println("Error on Building Data");             
          }

Image for : The Vertical scroll bar on TableView is not Visible

The Vertical scroll bar on TableView is not Visible

and then, i try this to put the TableView into a ScrollPane, but the header is also moving when I’m scrolling data vertically. I mean, the header didn’t stay in that position. I used this code:

ScrollPane scrollpane1 = new ScrollPane(); 
scrollpane1.setContent(tableview);

Image for : The Header is also moving when I scrolled vertically

The Header is also moving when I scrolled vertically

Is there any way to make the first row (Header) stay in that position when I am scrolling vertically?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
  • I can not find it documented any where (though I looked), so I did not put it as an answer, my assumption is that the vertical scroll bar would only become visible once it is needed. So looking at your example, if you had 10 rows of data, then it would appear. – Jacob Schoen Jun 27 '12 at 19:46
  • Also by the way, Welcome to Stack Overflow, and great job with giving all the details and code to make your question make sense on the first go. – Jacob Schoen Jun 27 '12 at 19:48
  • thank you Mr.jschoen... really helpful.. i will add more rows data, and i will look if it's working.. and i will back to here again to say if it's working or not.... once more, thank you so much :) – Linda Fitriani Jun 28 '12 at 02:05
  • Actually in your screenshot there is only one record in the table. Other below rows does not exist, they are rows only in visual. You may "remove" them by changing tableview CSS style. See [the tutorial](http://docs.oracle.com/javafx/2/ui_controls/custom.htm#CACCBDID). – Uluk Biy Jun 28 '12 at 08:08
  • @UlukBiy , thank u for the information, that link really really helpful for me, i'm confusing about that empty table.. thank u so much.. – Linda Fitriani Jun 28 '12 at 13:47

0 Answers0