5 Replies Latest reply on Apr 19, 2013 9:24 PM by ytw

    How can I hide the horizontal scroll bar in TableView?

    zmirc
      Hi, guys!

      Here is my problem: when a TableView has more rows than its height supports, a vertical scroll bar apperas. The appearance of this uses 11px of table's width, causing the appearance of the horizontal scroll bar.

      Possible, but not wanted, solution: increase table width by 11px, but this causes the appearance of a new column of 11px width (without name, of course).

      Here is my question: is there any method that hides the horizontal scroll bar in a TableView?

      Thanks.
        • 1. Re: How can I hide the horizontal scroll bar in TableView?
          jsmith
          Does using a CONSTRAINED_RESIZE_POLICY work for you?
          http://docs.oracle.com/javafx/2.0/api/javafx/scene/control/TableView.html#CONSTRAINED_RESIZE_POLICY

          Edited by: jsmith on Apr 16, 2012 2:45 PM

          Hmm, scratch that idea, I tried table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); and it did not seem to solve your problem.
          • 2. Re: How can I hide the horizontal scroll bar in TableView?
            925065
            Sorry for digging out this old post but I'm experiencing the same problem with JavaFX 2.2 (JDK 1.7.0_09). Has anybody found a solution for this problem? Thanks in advance.
            • 3. Re: How can I hide the horizontal scroll bar in TableView?
              KonradZuse
              In Swing you were able to say if you wanted the horiz/vert scroll bars always, never, or ocassional. Setting it to normal would work,b ut I'm no too sure where that applies here...

              I would create a jira post.
              • 4. Re: How can I hide the horizontal scroll bar in TableView?
                shakir.gusaroff
                The following should work. Bur it is not working. It hides the vertical scroll bar:
                        stage.show();
                        final ScrollBar  scrollBarH = (ScrollBar) table.lookup(".scroll-bar:hotizontal");
                        scrollBarH.setVisible(false);
                        
                • 5. Re: How can I hide the horizontal scroll bar in TableView?
                  ytw
                  I think you can hide it with css.

                  Before image: [http://imgur.com/23fiElF]
                  After image: [http://imgur.com/17Fu2SH]


                  Test Program
                  package test;
                  
                  import javafx.application.Application;
                  import javafx.beans.property.SimpleStringProperty;
                  import javafx.collections.FXCollections;
                  import javafx.collections.ObservableList;
                  import javafx.geometry.Insets;
                  import javafx.scene.Group;
                  import javafx.scene.Scene;
                  import javafx.scene.control.Label;
                  import javafx.scene.control.TableColumn;
                  import javafx.scene.control.TableView;
                  import javafx.scene.control.cell.PropertyValueFactory;
                  import javafx.scene.layout.VBox;
                  import javafx.scene.text.Font;
                  import javafx.stage.Stage;
                   
                  public class JavaFx2HideTableViewHScrollBar extends Application {
                   
                      private TableView<Person> table = new TableView<Person>();
                      private final ObservableList<Person> data =
                          FXCollections.observableArrayList(
                              new Person("Jacob", "Smith", "jacob.smith@example.com"),
                              new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                              new Person("Ethan", "Williams", "ethan.williams@example.com"),
                              new Person("Emma", "Jones", "emma.jones@example.com"),
                              new Person("Michael", "Brown", "michael.brown@example.com"),
                              new Person("Jacob", "Smith", "jacob.smith@example.com"),
                              new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                              new Person("Ethan", "Williams", "ethan.williams@example.com"),
                              new Person("Emma", "Jones", "emma.jones@example.com"),
                              new Person("Michael", "Brown", "michael.brown@example.com"),
                              new Person("Jacob", "Smith", "jacob.smith@example.com"),
                              new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                              new Person("Ethan", "Williams", "ethan.williams@example.com"),
                              new Person("Emma", "Jones", "emma.jones@example.com"),
                              new Person("Michael", "Brown", "michael.brown@example.com"),
                              new Person("Jacob", "Smith", "jacob.smith@example.com"),
                              new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                              new Person("Ethan", "Williams", "ethan.williams@example.com"),
                              new Person("Emma", "Jones", "emma.jones@example.com"),
                              new Person("Michael", "Brown", "michael.brown@example.com")
                          );
                     
                      public static void main(String[] args) {
                          launch(args);
                      }
                   
                      @Override
                      public void start(Stage stage) {
                          Scene scene = new Scene(new Group());
                          stage.setTitle("Table View Sample");
                          stage.setWidth(450);
                          stage.setHeight(500);
                   
                          final Label label = new Label("Address Book");
                          label.setFont(new Font("Arial", 20));
                   
                          table.setEditable(true);
                   
                          TableColumn firstNameCol = new TableColumn("First Name");
                          firstNameCol.setMinWidth(100);
                          firstNameCol.setCellValueFactory(
                                  new PropertyValueFactory<Person, String>("firstName"));
                   
                          TableColumn lastNameCol = new TableColumn("Last Name");
                          lastNameCol.setMinWidth(100);
                          lastNameCol.setCellValueFactory(
                                  new PropertyValueFactory<Person, String>("lastName"));
                   
                          TableColumn emailCol = new TableColumn("Email");
                          emailCol.setMinWidth(200);
                          emailCol.setCellValueFactory(
                                  new PropertyValueFactory<Person, String>("email"));
                   
                          table.setItems(data);
                          table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
                   
                          final VBox vbox = new VBox();
                          vbox.setSpacing(5);
                          vbox.setPadding(new Insets(10, 0, 0, 10));
                          vbox.getChildren().addAll(label, table);
                   
                          ((Group) scene.getRoot()).getChildren().addAll(vbox);
                   
                          
                          // Enable to hide horizontal scroll bar
                          // ************************************************************************
                          
                          scene.getStylesheets().add("./test/JavaFx2HideTableViewHScrollBar.css");
                          
                          // ************************************************************************
                          
                          
                          stage.setScene(scene);
                          stage.show();
                      }
                   
                      public static class Person {
                   
                          private final SimpleStringProperty firstName;
                          private final SimpleStringProperty lastName;
                          private final SimpleStringProperty email;
                   
                          private Person(String fName, String lName, String email) {
                              this.firstName = new SimpleStringProperty(fName);
                              this.lastName = new SimpleStringProperty(lName);
                              this.email = new SimpleStringProperty(email);
                          }
                   
                          public String getFirstName() {
                              return firstName.get();
                          }
                   
                          public void setFirstName(String fName) {
                              firstName.set(fName);
                          }
                   
                          public String getLastName() {
                              return lastName.get();
                          }
                   
                          public void setLastName(String fName) {
                              lastName.set(fName);
                          }
                   
                          public String getEmail() {
                              return email.get();
                          }
                   
                          public void setEmail(String fName) {
                              email.set(fName);
                          }
                      }
                  } 
                  CSS
                  .table-view *.scroll-bar:horizontal *.increment-button,
                  .table-view *.scroll-bar:horizontal *.decrement-button {
                      -fx-background-color: null;
                      -fx-background-radius: 0;
                      -fx-background-insets: 0;
                      -fx-padding: 0;
                  }
                  
                  .table-view *.scroll-bar:horizontal *.increment-arrow, 
                  .table-view *.scroll-bar:horizontal *.decrement-arrow {
                      -fx-background-color: null;
                      -fx-background-radius: 0;
                      -fx-background-insets: 0;
                      -fx-padding: 0;
                      -fx-shape: null;
                  }
                  Edited by: ytw on Apr 19, 2013 2:23 PM