Adding a listener to a TabPane created with TabPan

2019-08-05 05:07发布

问题:

I have the following code:

TabPaneBuilder.create()
    .tabs(
        TabBuilder.create()
            .text("Main")
            .closable(false)
            .build(),
        TabBuilder.create()
            .text("Preview")
            .content(createPreviewSplitMenu())
            .closable(false)
            .build()
        )
   .build()

Is it possible to add a listener here so that something is done when the tab "preview" is selected? If so I can't seem to find it and I have looked at the API.

I'm not asking how to do it in general, just when using TabPaneBuilder.

Thanks.

Edit: would also like the simplest regular way to do it if what I am asking is not possible.

Edit2: what I'd like to do is have the content of "Preview" tab redrawn when it is selected.

回答1:

You need to add the listener to the TabBuilder!

(Need to give it a name first, so you can reference it later:)

TabPaneBuilder.create()
.tabs(
    TabBuilder.create()
        .text("Main")
        .closable(false)
        .build(),

//New code coming through

    previewTab = TabBuilder.create()
        .text("Preview")
        .content(createPreviewSplitMenu())
        .closable(false)

        .onSelectionChanged(new EventHandler<Event>() {
        public void handle(Event evt) {
          if (previewTab.isSelected()) {
            //code to update the tab
          }
        }
      })

      .build()
    )
.build()


标签: java javafx-2