【问题标题】:Opening a TabPane when button is clicked from another view从另一个视图单击按钮时打开 TabPane
【发布时间】:2018-04-17 12:47:07
【问题描述】:

我有两个单独的视图,一个包含一个按钮,例如 view A,另一个包含 3 个选项卡的 TabPane,称为 view B。这两个视图由两个独立的视图控制器类控制。我希望能够在 view A 中单击一个按钮,并能够在 view B's TabPane 中打开特定标签。

到目前为止,我已经尝试使用 view B 扩展 view A 的控制器,这样我就可以获得在 view B's 控制器中定义的 TabPane然后调用 myTabPane.getSelectionModel().select(myTab); 但这并没有奏效,因为它抛出了 NullPointerException。

我的问题是可以点击 view A 中的一个按钮,这样它会打开 view B 并在 view B's 上打开一个特定的选项卡选项卡窗格。

我也看过这些链接,但没有运气 1.setting selected tab,2.switch through tabs programatically,3.switch between tabs in tabpane

假设上面的图片是view A,当你点击右键应该打开view B并在view B's中打开一个特定的标签选项卡窗格。

假设上面的图片是view B,当点击view A上的按钮时,它应该打开view B并设置选项卡到选项卡 C。

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.control.SelectionModel;

public class SucessfulCreateProjectViewController extends AdminViewController {


 @FXML
 private Button OkButton;

@FXML
void handleCreateTasksButtonAction(ActionEvent event) {


try{
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("AdminView.fxml"));
    Scene scene = new Scene(fxmlLoader.load());
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.show();
    AdminTabPane.getSelectionModel().select(1);; 

   }catch(Exception e){

    ErrorHandlerController.infoBox("Error Opening AdminPage", "Fail", null);
    e.printStackTrace();
   }

}

@FXML
void handleOKButtonAction(ActionEvent event) {

  Stage stage = (Stage) OkButton.getScene().getWindow();
  stage.close();

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    }

【问题讨论】:

  • 为此使用调解器。
  • 你介意解释一下吗

标签: java javafx javafx-8 javafx-2


【解决方案1】:

Mediator 是一种设计模式,用于在互不认识的对象之间进行通信。

这是一个可以完成您工作的调解员示例。

public class Mediator {
    private static Mediator instance;

    private Consumer<String> consumer;

    public static Mediator getInstance() {
        if(instance == null) {
            instance = new Mediator();
        }
        return instance;
    }

    private Mediator() {
    }

    public void register(Consumer<String> consumer) {
        this.consumer = consumer;
    }

    public void fireEvent(String string) {
        if(consumer != null) {
            consumer.accept(string);
        }
    }
}

分别是两个控制器

public class ViewAController {
    @FXML
    private Button btnL, btnR;

    @FXML
    private void initialize() {
        btnL.setOnAction(event -> Mediator.getInstance().fireEvent("left"));
        btnR.setOnAction(event -> Mediator.getInstance().fireEvent("right"));
    }
}

public class ViewBController {

    @FXML
    private TabPane tabPane;

    @FXML
    private void initialize() {
        Mediator.getInstance().register(s -> {
            switch (s) {
                case "left":
                    tabPane.getSelectionModel().select(0);
                    break;

                case "right":
                    tabPane.getSelectionModel().select(2);
                    break;
            }
        });
    }
}

这是一个同时打开两个窗口的测试应用程序。

public class Main extends Application {

    @Override
    public void start(Stage stageA) throws Exception{
        Parent viewA = FXMLLoader.load(getClass().getResource("view_a.fxml"));
        Parent viewB = FXMLLoader.load(getClass().getResource("view_b.fxml"));

        stageA.setTitle("View A");
        stageA.setScene(new Scene(viewA));
        stageA.show();

        Stage stageB = new Stage();
        stageB.setTitle("View B");
        stageB.setScene(new Scene(viewB));
        stageB.show();
    }


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

如果您不想同时使用两个窗口,只需更改中间人即可。

【讨论】:

  • 谢谢你,答案很详细,我现在明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多