1

这是我的代码:

public class TabApp implements Initializable {

    @FXML
    Button newButton;
    @FXML
    TabPane tabPane = new TabPane();
    public Tab newTab;
    private SingleSelectionModel<Tab> selectionModel;
    public int zQueryTabCount = 2;

    @FXML
    public void handleNewButton(ActionEvent event) {
        System.out.println("new button is pressed");

        newTab = new Tab();

        selectionModel.select(newTab);

        newTab.setId("Query " + zQueryTabCount);

        newTab.setText("Query " + zQueryTabCount);

        tabPane.getTabs().add(tabPane.getTabs().size(), newTab);

        tabPane.getTabs().get(0).setClosable(false);

        newTab.setClosable(true);

        if (zQueryTabCount < 2) {

            tabPane.getTabs().get(0).setClosable(false);

        }

        zQueryTabCount++;

    }
}

我正在NullPointerException排队selectionModel.select(newTab);

我该如何解决?

4

1 回答 1

1

Your selectionModel variable is not initialized nor attached to TabPane.

Use next call instead:

[...]
tabPane.getTabs().add(tabPane.getTabs().size(), newTab);

// should be called after tab has been added to TabPane
tabPane.getSelectionModel().select(newTab);
于 2012-09-18T15:28:01.123 回答