在javafx中添加新窗口

时间:2015-07-29 06:40:34

标签: java javafx javafx-2 javafx-8

我有多屏幕的javafx应用程序,并且每次都在旧屏幕上覆盖新屏幕但是对于一个案例我需要2个屏幕如图所示:

但是当点击事件viewresult窗口时,新屏幕应该添加到另一个窗口。

喜欢:我有2个窗口窗格分析窗口和查看结果窗口窗格如果我在查看结果窗口上执行任何单击事件,则分析窗口应更改为新窗口。示例代码共享如下:

ScreensController

public class ScreensController  extends StackPane {
    //Holds the screens to be displayed

    private HashMap<String, Node> screens = new HashMap<>();

    public ScreensController() {
        super();
    }

    //Add the screen to the collection
    public void addScreen(String name, Node screen) {
        System.out.println(" adding ");
        screens.put(name, screen);
        System.out.println("checking :"+screens.get(name));
    }

    //Returns the Node with the appropriate name
    public Node getScreen(String name) {
        return screens.get(name);
    }

    //Loads the fxml file, add the screen to the screens collection and
    //finally injects the screenPane to the controller.
    public boolean loadScreen(String name, String resource) {
        System.out.println("Name :"+name +" and Resources is :"+resource);
        try {

            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
            //System.out.println("2");
            Parent loadScreen = (Parent) myLoader.load();
           // System.out.println("3");
            ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
           // System.out.println("4");
            myScreenControler.setScreenParent(this);
           // System.out.println("5");
            addScreen(name, loadScreen);
           // System.out.println("6");
            return true;
        } catch (Exception e) {
            System.out.println(e.toString());
            return false;
        }
    }

    //This method tries to displayed the screen with a predefined name.
    //First it makes sure the screen has been already loaded.  Then if there is more than
    //one screen the new screen is been added second, and then the current screen is removed.
    // If there isn't any screen being displayed, the new screen is just added to the root.
    public boolean setScreen(final String name) { 
        System.out.println("screen load :"+(name));
        if (screens.get(name) != null) {   //screen loaded
            final DoubleProperty opacity = opacityProperty();

            if (!getChildren().isEmpty()) {   //if there is more than one screen

                Timeline fade = new Timeline(
                        new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
                        new KeyFrame(new Duration(100), new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        getChildren().remove(0);                    //remove the displayed screen
                        getChildren().add(0, screens.get(name));     //add the screen
                        Timeline fadeIn = new Timeline(
                                new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                                new KeyFrame(new Duration(80), new KeyValue(opacity, 1.0)));
                        fadeIn.play();
                    }
                }, new KeyValue(opacity, 0.0)));
                fade.play();

            } else {
                setOpacity(0.0);
                getChildren().add(screens.get(name));       //no one else been displayed, then just show
                Timeline fadeIn = new Timeline(
                        new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                        new KeyFrame(new Duration(2500), new KeyValue(opacity, 1.0)));
                fadeIn.play();
            }
            return true;
        } else {
            System.out.println("screen hasn't been loaded!!! \n");
            return false;
        }


        /*Node screenToRemove;
         if(screens.get(name) != null){   //screen loaded
         if(!getChildren().isEmpty()){    //if there is more than one screen
         getChildren().add(0, screens.get(name));     //add the screen
         screenToRemove = getChildren().get(1);
         getChildren().remove(1);                    //remove the displayed screen
         }else{
         getChildren().add(screens.get(name));       //no one else been displayed, then just show
         }
         return true;
         }else {
         System.out.println("screen hasn't been loaded!!! \n");
         return false;
         }*/
    }

    //This method will remove the screen with the given name from the collection of screens
    public boolean unloadScreen(String name) {
        if (screens.remove(name) == null) {
            System.out.println("Screen didn't exist");
            return false;
        } else {
            return true;
        }
    }
}

ViewResults

Class ViewResults{
@Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        ViewResults vr=new ViewResults();
        vr.formateviolationxml(ud.dirpath+"\\.sonar\\export\\violations.xml");
        vr.xmlprase();

           ObservableList obList = FXCollections.observableList(vr.getResList());
           resourcelist.getItems().clear();
           resourcelist.setItems(obList);

        //   metricstabview.setOnMouseClicked(new EventHandler<MouseEvent>() {

           violationtabview.setOnMousePressed(new EventHandler<MouseEvent>() {
                    @Override 
                    public void handle(MouseEvent event) {
                        if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
                            System.out.println(violationtabview.getSelectionModel().getSelectedItem());
                            ViolationData datamet=violationtabview.getSelectionModel().getSelectedItem();
                            System.out.println("viol line :"+datamet.getLine());
                            System.out.println("passing control to source importer");
                            myController.loadScreen(Main.sourceImportID, Main.sourceImportFile);
                            myController.setScreen(Main.sourceImportID);

                        }
                    }
                });
    }}

分析仪

class Analyzer {
@FXML
    public void ccapresultsmenu(ActionEvent event) {
        System.out.println("entered view results");
        //myController.loadScreen(Main.sourceImportID, Main.sourceImportFile);
        // myController.setScreen(Main.sourceImportID);
        // myController.loadScreen(Main.viewResultsID, Main.viewResultsFile);
        // myController.setScreen(Main.viewResultsID);

         Group root = new Group();
            Stage stage = new Stage();

            AnchorPane frame;
            try {
                frame = FXMLLoader.load(getClass().getResource("/cts/gto/ccap/ui/view/ViewResults.fxml"));
                 root.getChildren().add(frame);
                    Scene scene = new Scene(root);

                    stage.setScene(scene);
                    stage.show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }


}

0 个答案:

没有答案