Monday, 19 October 2015

Filled Under:

How To Create Pop-Up Menu In JavaFx

In this tutorial we show how to create pop up menu in javafx using context menu.Context menu can be created using ObservableList of menu items user have option to add any of menu like radio menu and checkmenu.For creating pop-up menu we have to first create a context menu then we have to add a menuitemin in context menu accoding to our need following program shows how to create a pop-up menu.

CODING:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class mybej extends Application 
{

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

@Override
public void start(Stage primaryStage) 
{
final Group root = new Group();
final ContextMenu cm = new ContextMenu();
MenuItem menu1 = new MenuItem("First Menu");
Menu menu = new Menu("Second Menu ");
MenuItem submenu = new MenuItem("First Sub Menu");
MenuItem submenu1 = new MenuItem("Second Sub Menu");
MenuItem menu3 = new MenuItem("Third Menu");
cm.getItems().add(menu1);
cm.getItems().add(menu);
menu.getItems().add(submenu);
menu.getItems().add(submenu1);
cm.getItems().add(menu3);
Scene scene = new Scene(root, 600, 350,Color.WHITE);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY)  
cm.show(root, e.getX(),e.getY());
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("POP MENU IN JAVAFX");
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
}

OUTPUT:


0 comments:

Post a Comment