JavaFX,离开并进入Tab控件

时间:2015-05-26 13:38:50

标签: javafx

我有一个JavaFX应用程序。主窗口包含一个TabPane(P1)。 P1包含4个不同的Tab控件(T11,T12,T13,T14)。 T11包含一个TabPane(P2),它包含两个Tab控件(T21,T22)。

我正在寻找一种简单的方法来检测然后任何Tab控件获取或失去可见性。使用SelectionModel事件是不够的。

// LG

1 个答案:

答案 0 :(得分:0)

您可以观察每个标签selectedProperty。对于T21和T22,我想你真的想知道T21和T11是否都被选中以知道T21是否可见;为此您只需观察t11.selectedProperty().and(t21.selectedProperty()):即您可以执行类似

的操作
t11.selectedProperty().and(t21.selectedProperty()).addListener(...);

并提供ChangeListener<Boolean>。当且仅当选择了t11和t12时,这将是true,并且如果收件人从false更改为true或反之亦然,则会通知收听者。

这是使用FXML的完整示例。

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TabSelectionDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("TabSelectionDemo.fxml")), 600, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

TabSelectionDemo.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.Label?>

<TabPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="TabSelectionDemoController" fx:id="p1">
    <Tab fx:id="t11" text="T11">
        <TabPane fx:id="p2">
            <Tab fx:id="t21" text="T21">
                <Label text="This is T21"/>
            </Tab>
            <Tab fx:id="t22" text="T22">
                <Label text="This is T22"/>
            </Tab>
        </TabPane>
    </Tab>
    <Tab fx:id="t12" text="T12">
        <Label text="This is T12"/>
    </Tab>
    <Tab fx:id="t13" text="T13">
        <Label text="This is T13"/>
    </Tab>
    <Tab fx:id="t14" text="T14">
        <Label text="This is T14"/>
    </Tab>
</TabPane>

TabSelectionDemoController.java:

import javafx.beans.binding.BooleanBinding;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.Tab;


public class TabSelectionDemoController {
    @FXML
    private Tab t11 ;
    @FXML
    private Tab t12 ;
    @FXML
    private Tab t13 ;
    @FXML
    private Tab t14 ;
    @FXML
    private Tab t21 ;
    @FXML
    private Tab t22 ;

    private BooleanBinding t21Selected ;
    private BooleanBinding t22Selected ;

    public void initialize() {
        t11.selectedProperty().addListener(createListener("t11"));
        t12.selectedProperty().addListener(createListener("t12"));
        t13.selectedProperty().addListener(createListener("t13"));
        t14.selectedProperty().addListener(createListener("t14"));

        t21Selected = t21.selectedProperty().and(t11.selectedProperty()) ;
        t22Selected = t22.selectedProperty().and(t11.selectedProperty()) ;

        t21Selected.addListener(createListener("t21"));
        t22Selected.addListener(createListener("t22"));
    }

    private ChangeListener<Boolean> createListener(String tabName) {
        return (obs, wasSelected, isNowSelected) -> {
            if (isNowSelected) {
                System.out.println(tabName + " gained visible selection");
            } else {
                System.out.println(tabName + " lost visible selection");
            }
        };
    }

}