2014-09-22 6 views
0

단순히로드 된 fxml이 포함 된 슬라이드 바를 프로그래밍하려고합니다. 이 FXML은 제 메뉴입니다. 이 메뉴를 사용하면 스레드 선호 작업 배경을 사용하지 않고 다른 FXML 파일을 특수 창에로드해야 업데이트를 요청하거나 생성 할 수 있습니다.다른 스테이지에서 이벤트를 업데이트하려면 어떻게해야합니까? (다른 컨트롤러에서)

내 SlideOut.java (실행) :`package slideout; 내 SlideBarConntent.fxml-컨트롤러 folowing 액션 이벤트의 컨트롤러에 입력이 후

import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.animation.*; 
import javafx.application.Application; 
import javafx.event.*; 
import javafx.fxml.FXMLLoader; 
import javafx.geometry.Pos; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.scene.text.Text; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 
import javafx.util.Duration; 
import javafx.scene.web.WebEngine; 

/** 
* Example of a sidebar that slides in and out of view 
*/ 
public class SlideOut extends Application { 

    public String mainConntent; 
    String currentPage; 
    Pane mainView; 
    Stage staged; 

    public void changeConntent(String Conntent){ 
     FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource(Conntent)); 
     try { 
      mainView = (Pane) fxmlMainLoader.load(); 
     } catch (IOException ex) { 
      Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     mainView.setPrefSize(800, 600); 

// create a sidebar with some content in it. 

     final Pane lyricPane = createSidebarContent(); 
     SideBar sidebar = new SideBar(250, lyricPane); 
     VBox.setVgrow(lyricPane, Priority.ALWAYS); 

// layout the scene. 
     final BorderPane layout = new BorderPane(); 
     Pane mainPane = VBoxBuilder.create().spacing(10) 
       .children(
         sidebar.getControlButton(), 
         mainView 
       ).build(); 
     layout.setLeft(sidebar); 
     layout.setCenter(mainPane); 

// show the scene 

     Scene scene = new Scene(layout); 
     scene.getStylesheets().add(getClass().getResource("slideout.css").toExternalForm()); 
     staged.setScene(scene); 
     staged.showAndWait(); 
     } 





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

    public void start(final Stage stage){ 

     stage.setTitle("SLideOutExample"); 

// create a WebView to show to the right of the SideBar. 
     mainView = new Pane(); 

     FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource("Home.fxml")); 
     try { 
      mainView = (Pane) fxmlMainLoader.load(); 
     } catch (IOException ex) { 
      Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     mainView.setPrefSize(800, 600); 

// create a sidebar with some content in it 

     final Pane lyricPane = createSidebarContent(); 
     SideBar sidebar = new SideBar(250, lyricPane); 
     VBox.setVgrow(lyricPane, Priority.ALWAYS); 

// layout the scene 

     final BorderPane layout = new BorderPane(); 
     Pane mainPane = VBoxBuilder.create().spacing(10) 
       .children(
         sidebar.getControlButton(), 
         mainView 
       ).build(); 
     layout.setLeft(sidebar); 
     layout.setCenter(mainPane); 

// show the scene 

     Scene scene = new Scene(layout); 
     scene.getStylesheets().add(getClass().getResource("slideout.css").toExternalForm()); 
     stage.setScene(scene); 
     stage.show();//showAndWait();? and something to do? 

    } 

    private BorderPane createSidebarContent() {// create some content to put in the sidebar. 
     final BorderPane lyricPane = new BorderPane(); 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SlideBarConntent.fxml")); 
     Pane cmdPane = null; 
     try { 
      cmdPane = (Pane) fxmlLoader.load(); 
     } catch (IOException ex) { 
      Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     try { 
      lyricPane.setCenter(cmdPane); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return lyricPane; 
    } 

    /** 
    * Animates a node on and off screen to the left. 
    */ 
    class SideBar extends VBox { 

     /** 
     * @return a control button to hide and show the sidebar 
     */ 
     public Button getControlButton() { 
      return controlButton; 
     } 
     private final Button controlButton; 

     /** 
     * creates a sidebar containing a vertical alignment of the given nodes 
     */ 
     SideBar(final double expandedWidth, Node... nodes) { 
      getStyleClass().add("sidebar"); 
      this.setPrefWidth(expandedWidth); 
      this.setMinWidth(0); 

// create a bar to hide and show. 
      setAlignment(Pos.CENTER); 
      getChildren().addAll(nodes); 

// create a button to hide and show the sidebar. 
      controlButton = new Button("Collapse"); 
      controlButton.getStyleClass().add("hide-left"); 
      controlButton.setId("ControlButton"); 

// apply the animations when the button is pressed 

      controlButton.setOnAction(new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(ActionEvent actionEvent) { 
// create an animation to hide sidebar 

        final Animation hideSidebar = new Transition() { 
         { 
          setCycleDuration(Duration.millis(250)); 
         } 

         protected void interpolate(double frac) { 
          final double curWidth = expandedWidth * (1.0 - frac); 
          setPrefWidth(curWidth); 
          setTranslateX(-expandedWidth + curWidth); 
         } 
        }; 
        hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() { 
         @Override 
         public void handle(ActionEvent actionEvent) { 
          setVisible(false); 
          controlButton.setText("Show"); 
          controlButton.getStyleClass().remove("hide-left"); 
          controlButton.getStyleClass().add("show-right"); 
         } 
        }); 
// create an animation to show a sidebar 

        final Animation showSidebar = new Transition() { 
         { 
          setCycleDuration(Duration.millis(250)); 
         } 

         protected void interpolate(double frac) { 
          final double curWidth = expandedWidth * frac; 
          setPrefWidth(curWidth); 
          setTranslateX(-expandedWidth + curWidth); 
         } 
        }; 
        showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() { 
         @Override 
         public void handle(ActionEvent actionEvent) { 
          controlButton.setText("Collapse"); 
          controlButton.getStyleClass().add("hide-left"); 
          controlButton.getStyleClass().remove("show-right"); 
         } 
        }); 
        if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED) { 
         if (isVisible()) { 
          hideSidebar.play(); 
         } else { 
          setVisible(true); 
          showSidebar.play(); 
         } 
        } 
       } 
      }); 
     } 
    } 
} 
` 

:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package slideout; 

import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.Pane; 

/** 
* FXML Controller class 
* 
* @author tobiasg 
*/ 
public class SlideBarConntentController{ 

    SlideOut mainJava = new SlideOut(); 
    String Home = "Home.fxml"; 
    String Example = "FXMLExampelConntent.fxml"; 
    Pane dustbin; 


    @FXML void loadHomeAction(ActionEvent event) { 
     try { 
      mainJava.changeConntent(Home); 
     } catch (Exception ex) { 
      Logger.getLogger(SlideBarConntentController.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    @FXML void loadFXMLConntentExampleAction(ActionEvent event) { 
     try { 
      mainJava.changeConntent(Home); 
     } catch (Exception ex) { 
      Logger.getLogger(SlideBarConntentController.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 


} 

나는 누군가가 나를 도와 내 나쁜 영어 실력 나를 exuse 수 있기를 바랍니다.

답변

1

이유가 작동하지 않는 이유는 사용자가 SlideOut 클래스의 새 인스턴스를 작성하기 때문입니다. changeContent에 전화하면 표시되는 응용 프로그램을 나타내는 인스턴스가 아니라 새 인스턴스에서 호출합니다.

귀하의 SlideBarContentController에 현재 콘텐츠의 속성이 노출되도록하여이 문제를 해결할 것입니다. 그러면 응용 프로그램에서이 속성을 관찰하고 응답 할 수 있습니다. 이렇게하면 컨트롤러와 응용 프로그램 클래스 간의 결합이 제거되므로 바람직합니다.

컨트롤러는 다음과 같이 보일 것이다 :

FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource("Home.fxml")); 
    try { 
     mainView = (Pane) fxmlMainLoader.load(); 
     SlideBarContentController contentController = (SlideBarContentController) fxmlMainLoader.getController(); 
     contentController.contentProperty().addListener(new ChangeListener<String>() { 
      @Override 
      public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) { 
       changeContent(newValue); 
      } 
     }); 
    } catch (IOException ex) { 
     Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex); 
    } 

이 (당신은 몇 가지를 추가해야합니다

import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.Pane; 

/** 
* FXML Controller class 
* 
* @author tobiasg 
*/ 
public class SlideBarConntentController{ 

    String Home = "Home.fxml"; 
    String Example = "FXMLExampelConntent.fxml"; 
    Pane dustbin; 

    private final StringProperty content = new SimpleStringProperty(this, "content", ""); 

    public StringProperty contentProperty() { 
     return content ; 
    } 
    public final String getContent() { 
     return contentProperty().get(); 
    } 
    public final void setContent(String content) { 
     contentProperty().set(content); 
    } 

    @FXML void loadHomeAction(ActionEvent event) { 
     content.set(Home); 
    } 

    @FXML void loadFXMLConntentExampleAction(ActionEvent event) { 
     content.set(Example); 
    } 


} 

지금 응용 프로그램 클래스에서, 컨트롤러를 액세스하고 속성을 관찰 할 필요가)

+0

감사합니다. 문제가 있습니다. 그런데 어떻게 백그라운드 작업자 나 그런 식으로하지 않고 GUI로 이벤트를 업데이트 할 수 있습니까? changeConntent (String Conntent)를 호출하는 무언가가 필요하기 때문에? 주어진 Buttons (인스턴스 문제를 기반으로 생각하지 않음)로이를 실현할 수 있습니까? 또는 나는 새로 고침 버튼과 같은 것을 사용합니다. – xXTobiXx

+0

나는이 질문을 이해할 수 있을지 모르겠다. 그러나 FXML에서는 '