JavaFX Tab fit full size of header

后端 未结 3 1977
梦如初夏
梦如初夏 2021-01-15 16:21

I want the tabs in my tabpane to fit the complete size of the tabpane which they are in. So basically there shall be no header area visible, everything should be covered by

3条回答
  •  天命终不由人
    2021-01-15 16:42

    I'm not sure if this is do-able in CSS, and there might be a simpler way in Java to do this, but I wrote a class that extends TabPane in order to stretch the tabs to fill all the space.

    public class StretchedTabPane extends TabPane {
    
        public StretchedTabPane() {
            super();
            setUpChangeListeners();
        }
    
        public StretchedTabPane(Tab... tabs) {
            super(tabs);
            setUpChangeListeners();
        }
    
        private void setUpChangeListeners() {
    
            widthProperty().addListener(new ChangeListener() {
                @Override public void changed(ObservableValue value, Number oldWidth, Number newWidth) {
                    Side side = getSide();
                    int numTabs = getTabs().size();
                    if ((side == Side.BOTTOM || side == Side.TOP) && numTabs != 0) {
                        setTabMinWidth(newWidth.intValue() / numTabs - (20));
                        setTabMaxWidth(newWidth.intValue() / numTabs - (20));
                    }
                }
            });
    
            heightProperty().addListener(new ChangeListener() {
                @Override public void changed(ObservableValue value, Number oldHeight, Number newHeight) {
                    Side side = getSide();
                    int numTabs = getTabs().size();
                    if ((side == Side.LEFT || side == Side.RIGHT) && numTabs != 0) {
                        setTabMinWidth(newHeight.intValue() / numTabs - (20));
                        setTabMaxWidth(newHeight.intValue() / numTabs - (20));
                   }
               }
            });
    
            getTabs().addListener(new ListChangeListener() {
                public void onChanged(ListChangeListener.Change change){
                    Side side = getSide();
                    int numTabs = getTabs().size();
                    if (numTabs != 0) {
                        if (side == Side.LEFT|| side == Side.RIGHT) {
                            setTabMinWidth(heightProperty().intValue() / numTabs - (20));
                            setTabMaxWidth(heightProperty().intValue() / numTabs - (20));
                        }
                        if (side == Side.BOTTOM || side == Side.TOP) {
                            setTabMinWidth(widthProperty().intValue() / numTabs - (20));
                            setTabMaxWidth(widthProperty().intValue() / numTabs - (20));
                        }
                    }
                }
            });
       }
    }
    

    This will automatically adjust the width of each tab when the height, width or number of tabs changes.

提交回复
热议问题