In: Computer Science
JAVAFX:
Written as a Java API, JavaFX application code can reference APIs from any Java library. For example, JavaFX applications can use Java API libraries to access native system capabilities and connect to server-based middleware applications.
How Do I Run a Sample Application?
The steps in this section explain how to download and run the sample applications that are available as a separate download with the Java Platform (JDK 7).
To download and run the sample applications:
Go to the Java SE Downloads page .
Scroll down to locate the JDK 7 and JavaFX Demos and Samples section.
Click the Demos and Samples Download button to go to the downloads page.
On the Java SE Development Kit 7 Downloads page, scroll down to the JavaFX Demos and Samples Downloads section.
Download the zip file for the correct operating system and extract the files.
The javafx-samples-2.2.x directory is created and contains the
files for the available samples. The NetBeans projects for the
samples are in the javafx-samples-2.2.x\src
directory.
A menu bar is a horizontal bar that acts as a container for
menus. An instance of the MenuBar class represents a menu bar. You
can create a MenuBar
using its default
constructor:
Menu is a popup menu that contains several menu items that are displayed when the user clicks a menu. The user can select a menu item after which the menu goes into a hidden state
Constructor of the MenuBar class are:
ActionEvent
handlers are called.Using a menu
is a multistep process. The following
sections describe the steps in detail. The following is the summary
of steps:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FxMenuExample extends Application
{
// Create the Message Label
Label messageLbl = new Label("Press any Menu Item to see the message");
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create some menus
Menu openMenu = new Menu("open");
Menu subMenu = new Menu("filename");
MenuItem menuItem11 = new MenuItem("file1.suv");
Menu saveMenu = new Menu("save");
MenuItem menuItem2 = new MenuItem("file2");
menu.getItems().add(menuItem2);
// Create the MenuItem New
MenuItem newItem = new MenuItem("New");
newItem.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
printMessage("You have pressed the New Menu Item");
}
});
// Add menu items to the File menu
fileMenu.getItems().addAll(newItem, openItem);
// Create the MenuItem Copy
MenuItem copyItem = new MenuItem("Copy");
copyItem.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
printMessage("You have pressed the Copy Menu Item");
}
});
// Create the MenuItem Paste
MenuItem pasteItem = new MenuItem("Paste");
pasteItem.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
printMessage("You have pressed the Paste Menu Item");
}
});
// Add menu items to the Edit menu
editMenu.getItems().addAll(copyItem, pasteItem);
// Create a menu bar
MenuBar menuBar = new MenuBar();
// Add menus to a menu bar
menuBar.getMenus().addAll(fileMenu, editMenu);
// Create the Menu Box
HBox menu = new HBox();
// Add the MenuBar to the Menu Box
menu.getChildren().add(menuBar);
// Create the VBox
VBox root = new VBox();
// Add the children to the VBox
root.getChildren().addAll(menu,messageLbl);
// Set the Size of the VBox
root.setMinSize(350, 250);
/*
* Set the padding of the VBox
* Set the border-style of the VBox
* Set the border-width of the VBox
* Set the border-insets of the VBox
* Set the border-radius of the VBox
* Set the border-color of the VBox
*/
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Create the Scene
Scene scene = new Scene(root);
// Add the scene to the Stage
stage.setScene(scene);
// Set the title of the Stage
stage.setTitle("A Menu Example");
// Display the Stage
stage.show();
}
// Helper Method
public void printMessage(String message)
{
// Set the Text of the Label
messageLbl.setText(message);
}
}