简体   繁体   English

JavaFX TabPane:如何监听选择更改

[英]JavaFX TabPane: How to listen to selection changes

I want to do some actions when user goes from one tab to another, since i made my form design with Scene Builder I cannot use code mentioned here (He used TabPaneBuilder class) 当用户从一个选项卡转到另一个选项卡时,我想做一些操作,因为我用Scene Builder进行了表单设计我不能使用这里提到的代码(他使用了TabPaneBuilder类)

I guessed this code would work but it doesn't react to tab selection changes. 我猜这个代码可以工作,但它不会对选项卡选择更改做出反应。

@FXML
protected TabPane chatTabs;
.
.    
.
chatTabs.selectionModelProperty().addListener(
    new ChangeListener<SingleSelectionModel<Tab>> {
            @Override
            public void changed(ObservableValue<? extends SingleSelectionModel<Tab>> ov, SingleSelectionModel<Tab> t, SingleSelectionModel<Tab> t1) {
                System.err.println("changed");
            }
        }
    }
);

The right way to use change listener is this: 使用更改侦听器的正确方法是:

chatTabs.getSelectionModel().selectedItemProperty().addListener(
    new ChangeListener<Tab>() {
        @Override
        public void changed(ObservableValue<? extends Tab> ov, Tab t, Tab t1) {
            System.out.println("Tab Selection changed");
        }
    }
);

Why code in question didn't work? 为什么有问题的代码不起作用? I guess its because your change listener listens to changes in " selectionModel " instead of " selectedItem " 我猜是因为你的更改侦听器侦听“ selectionModel ”而不是“ selectedItem ”中的更改


Finding out when a tab has been added or removed is a little trickier: 找出添加或删除选项卡的时间有点棘手:

tabs.addListener( (Change<? extends Tab> change) -> {
  while( change.next() ) {
    if( change.wasAdded() ) {
      for( final Tab tab : change.getAddedSubList() ) {
        System.out.println( "Tab Added: " + tab );
      }
    } else if( change.wasRemoved() ) {
      // ...
    }
  }
} );

Or in Java 8 using lambda expression.... 或者在Java 8中使用lambda表达式....

chatTabs.getSelectionModel().selectedItemProperty().addListener((ov, oldTab, newTab) -> {
        System.err.println("changed");
    });

I think a much better and more natural approach is using Tab.setOnSelectionChanged. 我认为更好更自然的方法是使用Tab.setOnSelectionChanged。 Here's a complete little program that implements that approach. 这是一个实现该方法的完整小程序。 You can see a MUCH more complete example here: http://sandsduchon.org/duchon/cs335/fx020.html 你可以在这里看到更完整的例子: http//sandsduchon.org/duchon/cs335/fx020.html

Note that you should also use Tab.isSelected to react correctly to selecting this tab or unselecting that this tab. 请注意,您还应该使用Tab.isSelected正确选择此选项卡或取消选择此选项卡。

import javafx.application.Application; // FX base, requires start(Stage)
import javafx.stage.Stage;             // required by start (Stage)
import javafx.scene.Scene;             // no scene --> no display

import javafx.scene.control.TabPane;
import javafx.scene.control.Tab;

public class TabDemo extends Application {

   public void start (Stage stage) {
      TabPane tabPane = new TabPane ();

      Tab tba = new Tab ("one");
      Tab tbb = new Tab ("two");

      tabPane.getTabs().addAll (tba, tbb);

      tba.setOnSelectionChanged (e -> 
        System.out.println (
           tba.isSelected()?
           "a selected":
           "a unselected"
        )
      );

      Scene scene = new Scene (tabPane, 200, 50);
      stage.setScene (scene);
      stage.setTitle ("A Study of tab listeners");
      stage.show ();
   } // end start

} // end class TabDemo

In addition to MJafar Mash answer above, you can use " selectedIndexProperty() " to get the index of the selected tab instead of " selectedItemProperty() " which gets the selected tab itself. 除了上面的MJafar Mash答案之外,您还可以使用“ selectedIndexProperty() ”来获取所选标签的索引,而不是“ selectedItemProperty() ”来获取所选标签本身。

chatTabs.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number> (){
     @Override
     public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
           int selectedIndex = newValue.intValue();
           //where index of the first tab is 0, while that of the second tab is 1 and so on.
     }
 });

And this is the lambda expression version of it 这是它的lambda表达式版本

chartTabs.getSelectionModel().selectedIndexProperty().addListener( (observable, oldValue, newValue) -> {
       int selectedIndex = newValue.intValue();
       //where index of the first tab is 0, while that of the second tab is 1 and so on.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM