How can I hide the column header of a TableView in JavaFX 8?

I need to have an observable list of type to be displayed in a single column TableView that, when selected, will display the rest of the information on the right. The TableView is wrapped in a TitledPane, which is wrapped in an Accordion. See image below:

enter image description here

As you can see in this script, I don't want to show the column header.

I've tried following the instructions here , which leads to here :

Pane header = (Pane) list.lookup("TableHeaderRow");
header.setMaxHeight(0);
header.setMinHeight(0);
header.setPrefHeight(0);
header.setVisible(false);

      

However, it doesn't seem to work for JavaFX 8. The lookup ("TableHeaderRow") method returns null, which makes me think the "TableHeaderRow" selector no longer exists.

Is there an updated workaround for removing / hiding table header in JavaFX 8?

+3


source to share


4 answers


As noted in the comments, searches don't work until CSS is applied to the node, which is usually in the first render of the frame that the node renders. Your suggested solution works fine as long as you execute the code you posted after displaying the table.

For a better approach in this case, a one-column "table" with no header is easy ListView

. ListView

has a cell rendering engine that is similar to the one used for TableColumn

(but simpler since you don't have to worry about multiple columns). I would use ListView

in your script instead of a css hack to make the title disappear.



ListView<Album> albumList = new ListView<>();
albumList.setCellFactory((ListView<Album> lv) -> 
    new ListCell<Album>() {
        @Override
        public void updateItem(Album album, boolean empty) {
            super.updateItem(album, empty);
            if (empty) {
                setText(null);
            } else {
                // use whatever data you need from the album
                // object to get the correct displayed value:
                setText(album.getTitle());
            }
        }
    }
);

albumList.getSelectionModel().selectedItemProperty()
    .addListener((ObservableValue<? extends Album> obs, Album oldAlbum, Album selectedAlbum) -> {
        if (selectedAlbum != null) {
            // do something with selectedAlbum
        }
);

      

+7


source


I ran into the problem of hiding column headers lately and could solve it with css.

I have created a styleclass:

.noheader .column-header-background {
    -fx-max-height: 0;
    -fx-pref-height: 0;
    -fx-min-height: 0;
}

      



and added it to the TableView:

tableView.getStyleClass().add("noheader");

      

Someone needs an alternative approach just in case. It also gives the flexibility to switch column headings.

+3


source


No CSS or styling or skin manipulation needed. Just subclass TableView and override the size like

class XTableView extends TableView {
    @Override
    public void resize(double width, double height) {
        super.resize(width, height);
        Pane header = (Pane) lookup("TableHeaderRow");
        header.setMinHeight(0);
        header.setPrefHeight(0);
        header.setMaxHeight(0);
        header.setVisible(false);
    }
}

      

This works great since June 2017 in Java 8.

+1


source


Also, I would recommend using this for the time being.

tableView.skinProperty().addListener((a, b, newSkin) -> { TableHeaderRow headerRow = ((TableViewSkinBase) newSkin).getTableHeaderRow(); ... });

This can be done at initialization time, another method as mentioned above will return null if executed at initialization time.

0


source







All Articles