2017-12-13 8 views
0
나는 방법의 번호와 모달 윈도라는 클래스를 가지고

에 할당 창 내에서 내 모달 윈도 객체를 닫으면, 중요한 사람이 여기에있는 :내가 그것을

show(); 

hide(); 

getContent(); 

setContent(Pane p); 

내가 모달 윈도의 인스턴스를 생성, 나는 setContent (...); 미리 지정된 Pane 객체 (ModalWindow 인스턴스 생성 후 생성 된 객체)를 사용하여 show();를 호출합니다. hide(); 방법은, 공개적으로 액세스 할 수 있지만, 일반적으로 미리 생성 된 창 내에서 호출 - 난 (같은)

getPane(ModalWindow mW); 

내가 (숨기기에 액세스 할 수 있도록 내가 이렇게)라는 다른 방법을 사용하여이 작업을 수행; 내 창은 일반적으로 모달 윈도를 종료하는 데 사용되는 버튼이 있습니다 내 경우에는 창 개체 내에서 기능 (, 내가 지금, 그래서

closeButton.setOnMouseClicked(event -> mW.hide()); 

를 사용하여이 작업을 수행, 모달 윈도의 내 인스턴스의 대부분은 다음과 같이 :

ModalWindow mW = new ModalWindow(); 
mW.setContent(getPane(mW)); 
mW.show(); // ModalWindow usually closed from within itself 

내가 궁금하네요 것은 다음 모달 윈도가 창이 포함되고 있음을 개체에 액세스하는 반사 (또는 성격의 무언가를) 사용하는 방법 (생성 후 창 객체를 생성하지 않고있다 ModalWindow 인스턴스)?

다음은 전체입니다.

PaneHolder paneHolder = new PaneHolder(); 
ModalWindow mw = new ModalWindow(paneHolder); 

및 닫기 버튼이 작동을 예 :

public class Main { 
    public static void main(String[] args) { 
     ModalWindow mW = new ModalWindow(); 
     mW.setContent(PaneHolder.getPane(mW)); 
     mW.show(); 
    } 
} 

public class ModalWindow { 

    StackPane centeringPane = new StackPane(); 
    private BooleanProperty showing = new SimpleBooleanProperty(false); 

    public ModalWindow() { 
     centeringPane.setOpacity(0); 
     centeringPane.setAlignment(Pos.CENTER); 
    } 

    public ModalWindow(Pane content) { 
     centeringPane.setOpacity(0); 
     centeringPane.setAlignment(Pos.CENTER); 
     setContent(content); 
    } 

    public void show() { 
     centeringPane.setVisible(true); 
    } 

    public void hide() { 
     centeringPane.setVisible(false); 
    } 

    public boolean isShowing() { 
     return showing.get(); 
    } 

    public BooleanProperty showingProperty() { 
     return showing; 
    } 

    public void setShowing(boolean showing) { 
     this.showing.set(showing); 
    } 

    public Pane getContent() { 
     return (Pane) centeringPane.getChildren().get(0); 
    } 

    public void setContent(Pane content) { 
     centeringPane.getChildren().clear(); 
     centeringPane.getChildren().add(content); 
    } 
} 
public class PaneHolder { 
    Pane getPane(ModalWindow mW) { 
     Pane p = new Pane(); 
     Button closeButton = new Button("Close"); 
     closeButton.setOnAction(event -> mW.hide()); // I need the reference to mW here 
     p.getChildren.add(closeButton); 
     return p; 
    } 
} 
+0

간단히 말해서, 객체가 객체에 대한 참조를 보유하고있는 다른 객체를 발견 할 수있는 방법이 없습니다 (생각합니다.). (어쨌든 단 하나의 'ModalWindow'만 있다는 것을 어떻게 확신 할 수 있겠는가?) 아마도 실제 코드를 게시 할 수 있을까? –

+0

@James_D 전체 예제를 추가했으나 현재 얻고있는 것을 봅니다. –

답변

0

당신은 이런 식으로 뭔가를 수정할 수 있습니다 : 같은

public class ModalWindow { 

    StackPane centeringPane = new StackPane(); 

    // is this supposed to be bound to centeringPane.visibleProperty() somehow? 
    private BooleanProperty showing = new SimpleBooleanProperty(false); 

    private PaneHolder content ; 

    public ModalWindow() { 
     centeringPane.setOpacity(0); 
     centeringPane.setAlignment(Pos.CENTER); 
    } 

    public ModalWindow(PaneHolder content) { 
     centeringPane.setOpacity(0); 
     centeringPane.setAlignment(Pos.CENTER); 
     setContent(content); 
    } 

    public void show() { 
     centeringPane.setVisible(true); 
    } 

    public void hide() { 
     centeringPane.setVisible(false); 
    } 

    public boolean isShowing() { 
     return showing.get(); 
    } 

    public BooleanProperty showingProperty() { 
     return showing; 
    } 

    public void setShowing(boolean showing) { 
     this.showing.set(showing); 
    } 

    public PaneHolder getContent() { 
     return content ; 
    } 

    public void setContent(PaneHolder content) { 
     content.setModalWindow(this); 
     centeringPane.getChildren().clear(); 
     centeringPane.getChildren().add(content.getPane()); 
    } 
} 
public class PaneHolder { 

    private final Pane pane ; 
    private ModalWindow mw ; 

    public PaneHolder() { 
     pane = new Pane(); 
     Button closeButton = new Button("Close"); 
     closeButton.setOnAction(event -> mw.hide()); 
     p.getChildren.add(closeButton); 
     return p; 
    } 

    public Pane getPane() { 
     return pane ; 
    } 

    void setModalWindow(ModalWindow mw) { 
     this.mw = mw ; 
    } 
} 

그리고 당신이 할 수있는 일 추가 코드없이.

완전히 견고하지는 않습니다. (예를 들어, PaneHolder이 이미 다른 콘텐츠의 콘텐츠 인 경우 ModalWindow의 콘텐츠로 설정할 수 없다는 것을 강요하려는 것일 수도 있습니다.)하지만,이 개체의 수명주기에 대해 좀 더 생각해보십시오. 당신에게 아이디어를주십시오.

+0

나는 내일 이것을 보게 될 것이다. 당신의 도움을 주셔서 대단히 감사합니다. –