2013-02-22 1 views
4

CSS로 스타일을 지정할 수있는 간단한 팝업 컨트롤을 구현하고 싶습니다. 유일한 문제는 컨텐츠를 추가하는 것입니다 (JavaFX의 노드).JavaFX 2.2.6에서 PopupControl에 내용을 추가하는 방법은 무엇입니까?

PopupWindow.getContent() 메서드는 JavaFX 2.2.6에서 사용되지 않으며 CSS와 작동하지 않지만 내용을 볼 수 있지만 CSS 선택기는 작동하지 않습니다.

콘텐츠를 직접 추가하는 가장 좋은 방법은 무엇입니까? 그 목적을 위해 자체적 인 스킨 클래스를 구현해야합니까, 아니면 그냥 작동시킬 수있는 간단한 방법이 있습니까?

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.stage.Stage; 

public class MainApplication extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
     Group root = new Group(); 
     final Scene scene = new Scene(root); 
     scene.getStylesheets().add(MainApplication.class.getResource("style.css").toExternalForm()); 

     final Button button = new Button("show popup"); 
     button.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event) { 
       PopupTest popup = new PopupTest(); 
       popup.show(scene.getWindow()); 

      } 
     }); 
     root.getChildren().add(button); 

     stage.setScene(scene); 
     stage.show(); 
    } 

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

있는 style.css :

.popup { 
    -fx-font-size: 24px; 
} 

.popup .rect { 
    -fx-fill: green; 
} 

.popup .text { 
    -fx-text-fill: white; 
    -fx-font-weight: bold; 
} 

은 "완성도를 들어

import javafx.scene.control.Label; 
import javafx.scene.control.PopupControl; 
import javafx.scene.layout.StackPane; 
import javafx.scene.shape.Rectangle; 

public class PopupTest extends PopupControl { 
    public PopupTest() { 
     getStyleClass().add("popup"); // not working!? 

     StackPane pane = new StackPane(); 
     pane.getStyleClass().add("pane"); 
     Rectangle rectangle = new Rectangle(250, 250); 
     rectangle.getStyleClass().add("rect"); 
     Label text = new Label("popup test"); 
     text.getStyleClass().add("text"); 
     pane.getChildren().addAll(rectangle, text); 

     // how to display to pane when the popup is shown? 
     getContent().addAll(pane); 
    } 
} 

여기 내 MainApplication 및 CSS 파일은 다음과 같습니다

나는 간단한 사용 사례를 준비했습니다. popup "selector가 여기에서 작동하지 않습니다."pane "으로 설정하면 CSS가 올바르도록 팝업 창이 스타일이 적용됩니다 : pane.getStyleClass().add("popup"); // working with this "fix". 보인다

+0

javafx.stage.Popup을 사용해 보셨나요? –

+0

이전에 그 중 하나를 사용해 보았지만 기본적으로 Popup 클래스는 PopupWindow와 같으며 PopupControl (PopupWindow도 확장 됨)은 javafx.scene.control.Skinnable 인터페이스를 구현하므로 CSS를 통해 스타일을 변경할 수 있어야합니다. – Xander

답변

2

작동합니다 :

getScene().setRoot(pane); 

을 스타일 클래스가 작동하지 소개 : PopupControl이 getStylesheets() 방법이 없습니다. 그래서 어쩌면 그것은 setStyle(...)에 의해서만 직접 스타일링 될 수 있습니까? 단순히 pane의 스타일을 지정하거나 pane을 루트 창에 배치하고 스타일을 적용하여 해결할 수 있습니다.

관련 문제