NaveenBharadwaj
NaveenBharadwaj

Reputation: 1333

In JavaFX, is there a way to display Tab name first and then graphic?

I have a TabPane in JavaFX application. I had to write a listener to close a tab, so I removed the default close icon from tabs and added my custom close icon. Below is the code:

Tab gTab = new Tab("GlobalData.json");
Hyperlink hlink = new Hyperlink();
Image image = new Image(FileTreeItem.class.getResourceAsStream("../images/close.png"));
hlink.setGraphic(new ImageView(image));
hlink.setFocusTraversable(false);
gTab.setGraphic(hlink);
hlink.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        if (TabPaneUtil.getInstance().getSelectionModel().getSelectedItem().getText().endsWith("*")) {
            Response res = FXOptionPane.showConfirmDialog(new Main().getPrimaryStage(), "Would you like to save?", "Confirmation");
            if (res.equals(Response.YES))
                event.consume();
            else
                TabPaneUtil.getInstance().getTabs().remove(TabPaneUtil.getInstance().getSelectionModel().getSelectedItem());
        } else {
            TabPaneUtil.getInstance().getTabs().remove(TabPaneUtil.getInstance().getSelectionModel().getSelectedItem());
        }
    }
});
gTab.setContent(main1.globalDataView(ApplicationContext.getTreeView().getSelectionModel().getSelectedItem()));
TabPaneUtil.getInstance().getTabs().add(gTab);
TabPaneUtil.getInstance().getSelectionModel().select(gTab);

What is happening is I'm getting the close icon first and then the tab name. Is there any way I can get the tab name first and then the graphic?

P.S: I have seen some solutions like adding text in a Label and rotating graphic. I can't possibly do that because I will have to do a lot of code changes in my application and it gets a lot messy! Any help would be appreciated.

Upvotes: 2

Views: 685

Answers (2)

bichoFlyer
bichoFlyer

Reputation: 198

I'm, 5 years late but have two solutions that should work with JavaFX:

The first, would be:

hlink.setContentDisplay(ContentDisplay.RIGHT);

At least, with JavaFX 8 (or later) works fine.

On the other way, I see you're trying to put a close button, but JavaFX already has implemented it (at least on JavaFX 8). You should remove the hyperlink and image, and instead make your tab closable, and set a tab closing policy like this:

//Set a closing policy:
someMethodToGetTabPane().setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);

//... some other code

//Set a tab closable (can be omitted, since default value is true):
gTab.setClosable(true);

//Listen when someone requests closing the tab:
gTab.setOnCloseRequest(this::someMethodReference);

//Listen when the tab has been closed:
gTab.setOnClosed(this::doSomething);

//more code...

Upvotes: 0

James_D
James_D

Reputation: 209340

You can do this with an external CSS file: just include the rule

.tab-label {
    -fx-content-display: right ;
}

If you need, you can apply it only to specific tab panes with

.my-special-tab-pane .tab-label {
    -fx-content-display: right ;
}

and

someTabPane.getStyleClass().add("my-special-tab-pane");

Upvotes: 6

Related Questions