JavaFX Scene style with css

Description

JavaFX Scene style with css

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

   public static void main(String[] args) {
      Application.launch(args);//from w  w w . j a  va 2  s. c  o m
   }

   @Override
   public void start(Stage primaryStage) {

      primaryStage.setTitle("CSS ");
      Group root = new Group();
      final Scene scene = new Scene(root, 640, 480, Color.BLACK);
      MenuBar menuBar = new MenuBar();
      Menu menu = new Menu("Look and Feel");

      // New Modena Look and Feel
      MenuItem modenaLnf = new MenuItem("Modena");
      modenaLnf.setOnAction(enableCss(STYLESHEET_MODENA, scene));
      menu.getItems().add(modenaLnf);

      // Old default, Caspian Look and Feel
      MenuItem caspianLnf = new MenuItem("Caspian");
      caspianLnf.setOnAction(enableCss(STYLESHEET_CASPIAN, scene));

      menu.getItems().add(caspianLnf);

      menu.getItems().add(createMenuItem("Control Style 1", "controlStyle1.css", scene));
      menu.getItems().add(createMenuItem("Control Style 2", "controlStyle2.css", scene));

      menuBar.getMenus().add(menu);
      // stretch menu
      menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

      // Left and right split pane
      SplitPane splitPane = new SplitPane();
      splitPane.prefWidthProperty().bind(scene.widthProperty());
      splitPane.prefHeightProperty().bind(scene.heightProperty());

      VBox leftArea = new VBox(10);
      leftArea.getChildren().add(new Label("test"));

      HBox hbox = new HBox();
      hbox.getChildren().add(splitPane);
      VBox vbox = new VBox();
      vbox.getChildren().add(menuBar);
      vbox.getChildren().add(hbox);
      root.getChildren().add(vbox);
      splitPane.getItems().addAll(leftArea);

      primaryStage.setScene(scene);

      primaryStage.show();

   }

   protected final MenuItem createMenuItem(String label, String css, final Scene scene) {
      MenuItem menuItem = new MenuItem(label);
      ObservableList<String> cssStyle = loadSkin(css);
      menuItem.setOnAction(skinForm(cssStyle, scene));
      return menuItem;
   }

   protected final ObservableList<String> loadSkin(String cssFileName) {
      ObservableList<String> cssStyle = FXCollections.observableArrayList();
      cssStyle.addAll(getClass().getResource(cssFileName).toExternalForm());
      return cssStyle;
   }

   protected final EventHandler<ActionEvent> skinForm(final ObservableList<String> cssStyle, final Scene scene) {
      return (ActionEvent event) -> {
         scene.getStylesheets().clear();
         scene.getStylesheets().addAll(cssStyle);
      };
   }

   protected final EventHandler<ActionEvent> enableCss(String style, final Scene scene) {
      return (ActionEvent event) -> {

         scene.getStylesheets().clear();
         setUserAgentStylesheet(style);
      };
   }

}

/*//from   ww  w  . j av  a  2 s.c  o m
 * Copyright (c) 2011 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the distribution.
 *  - Neither the name of Oracle nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
    Document   : controlStyle1
*/

.root{
    -fx-font-size: 14pt;
    -fx-font-family: "Tahoma";
    -fx-base: #DFB951;
    -fx-background: #A78732;
    -fx-focus-color: #B6A678;
}

.button1{
    -fx-text-fill: #006464;
    -fx-background-color: #DFB951;
    -fx-border-radius: 20;
    -fx-background-radius: 20;
    -fx-padding: 5;
}

.button2{
    -fx-text-fill: #c10000;
    -fx-background-color: #DFB951;
    -fx-border-radius: 20;
    -fx-background-radius: 20;
    -fx-padding: 5;
}

.slider{
    -fx-border-color: white;
    -fx-border-style: dashed;
    -fx-border-width: 2;
}

/*/*from   w  ww. j  ava 2  s . co m*/
 * Copyright (c) 2011 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the distribution.
 *  - Neither the name of Oracle nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
    Document   : controlStyle2
*/

.root{
    -fx-font-size: 16pt;
    -fx-font-family: "Courier New";
    -fx-base: rgb(132, 145, 47);
    -fx-background: rgb(225, 228, 203);
}

.button{
    -fx-text-fill: rgb(49, 89, 23);
    -fx-border-color: rgb(49, 89, 23);
    -fx-border-radius: 5;
    -fx-padding: 3 6 6 6;
}

.borders{
    -fx-border-color: rgb(103, 100, 78);
    -fx-border-style: dotted;
    -fx-border-width: 1.5;
    -fx-border-insets: -5;
}



PreviousNext

Related