JavaFX Navigation

JavaPeter

Mitglied
Hallo,

kann mir jemand helfen, zeigen wie ich meine MenuBar und ToolBar zusammen in eine Datei auslagern kann und diese dann später in mehreren anderen Fenstern verwenden?


Java:
package ubung;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ubung extends Application {

    @Override
    public void start(Stage primaryStage) {

        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu("File");
        Menu menuEdit = new Menu("Edit");

        menuBar.getMenus().addAll(menuFile, menuEdit);

        ToolBar toolBar = new ToolBar(
                new Button("New"),
                new Button("Open"),
                new Button("Save"),
                new Button("Clean"),
                new Button("Compile"),
                new Button("Run"),
                new Button("Debug"),
                new Button("Profile")
        );

        VBox vbox = new VBox();
        vbox.getChildren().addAll(menuBar, toolBar);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(vbox);

        StackPane root = new StackPane();
        root.getChildren().add(borderPane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Übung 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

Danke
 

dzim

Top Contributor
Eclipse oder NetBeans?

Ich habe vor kurzen mit OSGi und den notwendigsten Teilen von e4 (für die Dependency Ijection) eine kleine Platform geschrieben, das eigentlich nur aus einem Menü, einer Toolbar (solange kein Plugin hier etwas eingestellt hat, ist die Toolbar aber unsichtbar) und einem Panel der eine Accordion und ein TabPane enthält, sowie eine Statuszeile.

Während der OSGi- und DI-Teil für dich vielleicht eher Overkill ist, ist das UI der Navigation grob im Stil eines Master-Detail-Blocks für dich vielleicht interessant. Die Frage ob du Eclipse oder NetBeans verwendest ist insofern interessant, weil ich unter Eclipse das Plugin e(fx)clipse verwende und die meisten UIs nicht in Code sondern in FXGraph - einer DSL, die zu FXML-Dateien übersetzt wird - schreibe.

Code:
package my.platform.app.ui.layout

import javafx.scene.layout.HBox
import javafx.scene.layout.BorderPane
import javafx.scene.control.MenuBar
import javafx.scene.control.Menu
import javafx.scene.control.MenuItem
import javafx.scene.control.TextField
import javafx.scene.control.Label
import javafx.scene.control.ProgressBar
import javafx.scene.control.Button
import javafx.geometry.Insets
import javafx.scene.control.SplitPane
import javafx.scene.control.Accordion
import javafx.scene.control.TitledPane
import javafx.scene.control.TabPane
import javafx.scene.layout.VBox
import javafx.scene.control.ToolBar
import javafx.scene.input.KeyCodeCombination
import javafx.scene.input.KeyCharacterCombination
import my.platform.app.ui.RootController
import java.lang.Double
import javafx.scene.control.ScrollPane
import javafx.scene.control.SeparatorMenuItem

component Root controlledby RootController resourcefile "../../res/strings/strings.properties" styledwith
"/css/default.css" {
	BorderPane {
		top : VBox id vboxMenuToolBar {
			children : [
				MenuBar id menuBar {
					menus : [
						Menu id menuFile {
							text : rstring "menu.file",
							items : [
								MenuItem id menuItemSave {
									disable : true,
									text : rstring "menu.file.save",
									accelerator : rstring "menu.file.save.accel",
									onAction : controllermethod menuItemSave
								},
								MenuItem id menuItemSaveAs {
									disable : true,
									text : rstring "menu.file.saveAs",
									accelerator : rstring "menu.file.saveAs.accel",
									onAction : controllermethod menuItemSaveAs
								},
								MenuItem id menuItemSaveAll {
									disable : true,
									text : rstring "menu.file.saveAll",
									onAction : controllermethod menuItemSaveAll
								},
								SeparatorMenuItem id menuItemFileAddition {
									visible : false
								},
								SeparatorMenuItem id menuItemFilePreExit {
									visible : true
								},
								MenuItem id menuItemExit {
									text : rstring "menu.file.exit",
									accelerator : rstring "menu.file.exit.accel",
									onAction : controllermethod handleMenuItemExit
								}
							]
						},
						Menu id menuSettings {
							text : rstring "menu.settings",
							items : [
								SeparatorMenuItem id menuItemSettingsAddition {
									visible : false
								},
								SeparatorMenuItem id menuItemSettingsPreSettings {
									visible : false
								},
								MenuItem id menuItemAppSettings {
									text : rstring "menu.settings.app",
									onAction : controllermethod menuItemAppSettings
								}
							]
						},
						Menu id menuHelp {
							text : rstring "menu.help",
							items : [
								SeparatorMenuItem id menuItemHelpAddition {
									visible : false
								},
								SeparatorMenuItem id menuItemHelpPreAbout {
									visible : false
								},
								MenuItem id menuItemAbout {
									text : rstring "menu.help.about",
									onAction : controllermethod handleMenuItemAbout
								}
							]
						}
					]
				},
				ToolBar id toolBar {
					visible : false,
					minHeight : 0,
					maxHeight : 0 // const ToolBar#USE_PREF_SIZE

				}
			], static margin : Insets {
				top : 25,
				bottom : 5
			}
		},
		center : SplitPane id splitPaneBody {
			items : [
				ScrollPane id scrollPaneNavigation {
					hbarPolicy : "AS_NEEDED",
					vbarPolicy : "AS_NEEDED",
					prefWidth : 250,
					fitToHeight : true,
					fitToWidth : true,
					content : Accordion id accordionNavigation {
						minWidth : const Accordion#USE_PREF_SIZE,
						minHeight : const Accordion#USE_PREF_SIZE
					}
				},
				ScrollPane id scrollPaneContent {
					hbarPolicy : "AS_NEEDED",
					vbarPolicy : "AS_NEEDED",
					prefWidth : 500,
					maxWidth : const Double#MAX_VALUE,
					fitToHeight : true,
					fitToWidth : true,
					content : TabPane id tabPaneContent {
						minWidth : const TabPane#USE_PREF_SIZE,
						minHeight : const TabPane#USE_PREF_SIZE
					}
				}
			], static margin : Insets {
				left : 2,
				right : 2
			}
		},
		bottom : HBox id hboxStatus {
			spacing : 5, Label id labelStatus {
				maxWidth : const Double#MAX_VALUE,
				alignment : "CENTER_LEFT", static margin : Insets {
					left : 5
				}, static hgrow : "ALWAYS"
			}, ProgressBar id progressStatus {
				visible : false,
				maxHeight : 50, static margin : Insets {
					left : 5
				}, static hgrow : "NEVER"
			}, Button id buttonCancel {
				visible : false,
				onAction : controllermethod handleButtonCancel,
				alignment : "CENTER_RIGHT", static margin : Insets {
					right : 5
				}, static hgrow : "NEVER"
			}, static margin : Insets {
				top : 5
			}, static alignment : "CENTER", static margin : Insets {
				top : 5,
				left : 2,
				right : 2,
				bottom : 2
			}
		}
	}
}
Das Ganze wird dann in folgendes FXML-Dokument umgewandelt, dass du dann im Code über den FXMLLoader laden kannst:
[XML]
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?scenebuilder-preview-i18n-resource ../../res/strings/strings.properties?>
<?scenebuilder-stylesheet /css/default.css?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="my.platform.app.ui.RootController">

<top>
<VBox fx:id="vboxMenuToolBar">
<children>
<MenuBar fx:id="menuBar">
<menus>
<Menu fx:id="menuFile" text="%menu.file">
<items>
<MenuItem fx:id="menuItemSave" disable="true" text="%menu.file.save" accelerator="%menu.file.save.accel" onAction="#menuItemSave"/>
<MenuItem fx:id="menuItemSaveAs" disable="true" text="%menu.file.saveAs" accelerator="%menu.file.saveAs.accel" onAction="#menuItemSaveAs"/>
<MenuItem fx:id="menuItemSaveAll" disable="true" text="%menu.file.saveAll" onAction="#menuItemSaveAll"/>
<SeparatorMenuItem fx:id="menuItemFileAddition" visible="false"/>
<SeparatorMenuItem fx:id="menuItemFilePreExit" visible="true"/>
<MenuItem fx:id="menuItemExit" text="%menu.file.exit" accelerator="%menu.file.exit.accel" onAction="#handleMenuItemExit"/>
</items>
</Menu>
<Menu fx:id="menuSettings" text="%menu.settings">
<items>
<SeparatorMenuItem fx:id="menuItemSettingsAddition" visible="false"/>
<SeparatorMenuItem fx:id="menuItemSettingsPreSettings" visible="false"/>
<MenuItem fx:id="menuItemAppSettings" text="%menu.settings.app" onAction="#menuItemAppSettings"/>
</items>
</Menu>
<Menu fx:id="menuHelp" text="%menu.help">
<items>
<SeparatorMenuItem fx:id="menuItemHelpAddition" visible="false"/>
<SeparatorMenuItem fx:id="menuItemHelpPreAbout" visible="false"/>
<MenuItem fx:id="menuItemAbout" text="%menu.help.about" onAction="#handleMenuItemAbout"/>
</items>
</Menu>
</menus>
</MenuBar>
<ToolBar fx:id="toolBar" visible="false" minHeight="0" maxHeight="0"/>
</children>
<BorderPane.margin>
<Insets top="25" bottom="5"/>
</BorderPane.margin>
</VBox>
</top>
<center>
<SplitPane fx:id="splitPaneBody">
<items>
<ScrollPane fx:id="scrollPaneNavigation" hbarPolicy="AS_NEEDED" vbarPolicy="AS_NEEDED" prefWidth="250" fitToHeight="true" fitToWidth="true">
<content>
<Accordion fx:id="accordionNavigation">
<minWidth><Accordion fx:constant="USE_PREF_SIZE" /></minWidth>
<minHeight><Accordion fx:constant="USE_PREF_SIZE" /></minHeight>
</Accordion>
</content>
</ScrollPane>
<ScrollPane fx:id="scrollPaneContent" hbarPolicy="AS_NEEDED" vbarPolicy="AS_NEEDED" prefWidth="500" fitToHeight="true" fitToWidth="true">
<maxWidth><Double fx:constant="MAX_VALUE" /></maxWidth>
<content>
<TabPane fx:id="tabPaneContent">
<minWidth><TabPane fx:constant="USE_PREF_SIZE" /></minWidth>
<minHeight><TabPane fx:constant="USE_PREF_SIZE" /></minHeight>
</TabPane>
</content>
</ScrollPane>
</items>
<BorderPane.margin>
<Insets left="2" right="2"/>
</BorderPane.margin>
</SplitPane>
</center>
<bottom>
<HBox fx:id="hboxStatus" spacing="5" BorderPane.alignment="CENTER">
<Label fx:id="labelStatus" alignment="CENTER_LEFT" HBox.hgrow="ALWAYS">
<maxWidth><Double fx:constant="MAX_VALUE" /></maxWidth>
<HBox.margin>
<Insets left="5"/>
</HBox.margin>
</Label>
<ProgressBar fx:id="progressStatus" visible="false" maxHeight="50" HBox.hgrow="NEVER">
<HBox.margin>
<Insets left="5"/>
</HBox.margin>
</ProgressBar>
<Button fx:id="buttonCancel" visible="false" onAction="#handleButtonCancel" alignment="CENTER_RIGHT" HBox.hgrow="NEVER">
<HBox.margin>
<Insets right="5"/>
</HBox.margin>
</Button>
<BorderPane.margin>
<Insets top="5"/>
</BorderPane.margin>
<BorderPane.margin>
<Insets top="5" left="2" right="2" bottom="2"/>
</BorderPane.margin>
</HBox>
</bottom>
</BorderPane>
[/XML]

Wie das aussehen kann, siehst du im Anhang - die Transparenz und die Fensterdekoration werden über CSS und das jar "Undecorator" gemacht (bin ich mal drüber gestolpert)...
 

Anhänge

  • platform.png
    platform.png
    83,1 KB · Aufrufe: 40

dzim

Top Contributor
Das wurde aus deiner Frage nicht deutlich - und ich finde das auch nicht so praktisch, aber jedem das Seine!
 

Ähnliche Java Themen

Neue Themen


Oben