Skip to content

Instantly share code, notes, and snippets.

@mansueli
Created July 7, 2014 16:50
Show Gist options
  • Save mansueli/9bbc77828ad6893d1147 to your computer and use it in GitHub Desktop.
Save mansueli/9bbc77828ad6893d1147 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("menu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Menu");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MenuController">
<children><MenuButton layoutX="0.37109375" layoutY="7.5" mnemonicParsing="false" text="FooMenu">
<items>
<MenuItem fx:id="foo1" mnemonicParsing="false" text="Foo1" />
<MenuItem fx:id="foo2" mnemonicParsing="false" text="Foo2" /><MenuItem fx:id="foo3" mnemonicParsing="false" text="Foo3" />
</items>
</MenuButton><MenuButton layoutX="99.7421875" layoutY="7.5" mnemonicParsing="false" text="BarMenu">
<items>
<MenuItem fx:id="bar1" mnemonicParsing="false" text="Bar1" />
<MenuItem fx:id="bar2" mnemonicParsing="false" text="Bar2" /><MenuItem fx:id="bar3" mnemonicParsing="false" text="Bar3" />
</items>
</MenuButton>
</children></AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
public class MenuController implements Initializable {
@FXML private MenuButton OkMenu;
@FXML private MenuItem foo1;
@FXML private MenuItem bar2;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Define foo1 and bar 2 as errors:
foo1.getStyleClass().add("error-menu-item");
bar2.getStyleClass().add("error-menu-item");
}
}
.error-menu-item
{
-fx-background-color: #ffbbbb;
}
.error-menu-item:focused .label
{
-fx-text-fill: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment