JavaFX - How to synchronize CustomTab (extending Tab) and TabPane

737 views Asked by At

I am developing an IDE. I created a CustomTab.java class by extending the default JavaFX Tab. I wanted to add a color transition animation, but I ran into a problem. The method in TabPane named getTabs(), which returns ObservableList<Tab>, is final. This means I can't override it to return ObservableList<CustomTab>. It seems that constructions like this:

for (Tab tab : tabPane.getTabs()) {
    ((CustomTab) tab).stopFlash();
}

used in Controller.java are neck breaking and wrong. What is the right way to do this?

1

There are 1 answers

1
ChaosGramer On

I think my reply is quiete late, but I think there is a way to do so. Use the function

tab.setUserData(Object c);

In that Object you can store all the informations you'll need to proceed.

so for example create an Object holding all the objects you need. E.g. you can hold the reference to your webView.

Also if you need to have different informations you could use:

tab.getProperties().put(Object key, Object data);
// receive the value with 
tab.getProperties().get(key); 

Here a link to the documentation I found

Maybe the Javadoc is better, but I found this one.

Here is a little example that I wrote:

public SQLEditorTab getNewTab(){
    SQLEditorTab tab = new SQLEditorTab("new Tab " + number);
    tab.setId("newTab" + number);
    tab.setOnCloseRequest(this);
    tab.setUserData(tab.tabContent);
    number ++; 
    return tab;
} // just to create the tab 

In my listener I receive the result simply by:

    sqlEditorPane.getTabs().forEach((Tab t) -> {
        if(t.getUserData() != null){
            System.out.print(t.getUserData().getClass());
        }
    });