ContextMenu is a part of the JavaFX library. ContextMenu can be associated with controls such as labels, textfield etc. The context menu is activated on right clicking over the associated controls. It shows a popup containing several menuitems or sub menu.
The Constructors of the class are:
- ContextMenu(): creates a new empty context menu.
- ContextMenu(MenuItem… i): creates a context menu that contains the menuitems.
Commonly used methods:
method | explanation |
---|---|
getItems() | returns the items of the context menu |
getOnAction() | returns the value of the property OnAction |
hide() | hides the context menu |
setOnAction(EventHandler v) | sets the value of the property onAction |
show(Node a, double X, double Y) | displays the context menu at a specified position of the screen |
Below programs illustrate the use of ContextMenu:
- Program to create a context menu and add it to label: A ContextMenu will be created by name ‘contextMenu’ and 3 menuitems: menuItem1, menuItem2, menuItem3 will be added to the menu contextMenu and the menu contextMenu will be associated with a label ‘label’. The label will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results.
// Program to create a context menu and add it to label
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
javafx.stage.Stage;
import
javafx.scene.text.Text.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
public
class
contextMenu_1
extends
Application {
   Â
// labels
   Â
Label l;
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
       Â
// set title for the stage
       Â
stage.setTitle(
"creating contextMenu "
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a label
       Â
Label label1 =
new
Label(
"This is a ContextMenu example "
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a menu
       Â
ContextMenu contextMenu =
new
ContextMenu();
Â
ÂÂ Â Â Â Â Â Â Â
// create menuitems
       Â
MenuItem menuItem1 =
new
MenuItem(
"menu item 1"
);
       Â
MenuItem menuItem2 =
new
MenuItem(
"menu item 2"
);
       Â
MenuItem menuItem3 =
new
MenuItem(
"menu item 3"
);
Â
ÂÂ Â Â Â Â Â Â Â
// add menu items to menu
       Â
contextMenu.getItems().add(menuItem1);
       Â
contextMenu.getItems().add(menuItem2);
       Â
contextMenu.getItems().add(menuItem3);
Â
ÂÂ Â Â Â Â Â Â Â
// create a tilepane
       Â
TilePane tilePane =
new
TilePane(label1);
Â
ÂÂ Â Â Â Â Â Â Â
// setContextMenu to label
       Â
label1.setContextMenu(contextMenu);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene sc =
new
Scene(tilePane,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(sc);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String args[])
   Â
{
       Â
// launch the application
       Â
launch(args);
   Â
}
}
Output:
- Program to create a context menu and add it to label and associate the context menu with window event listener: A Contextmenu will be created by name contextMenu and 3 menuitems menuItem1, menuItem2, menuItem3 will be added to the menu contextMenu and the contextMenu will be associated with a label l. The label will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results. A Window event will be created that will handle the window events of the context menu and will display the state of the context menu by a Label ‘label’. The window event will be associated with the label using setOnHiding() and setOnShowing() functions.
// Program to create a context menu and add it to label
// and associate the context menu with window event listener
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.WindowEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
javafx.stage.Stage;
import
javafx.scene.text.Text.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
public
class
contextMenu
extends
Application {
   Â
// labels
   Â
Label label;
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
       Â
// set title for the stage
       Â
stage.setTitle(
"creating contextMenu "
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a label
       Â
Label label1 =
new
Label(
"This is a ContextMenu example "
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a menu
       Â
ContextMenu contextMenu =
new
ContextMenu();
Â
ÂÂ Â Â Â Â Â Â Â
// create menuitems
       Â
MenuItem menuItem1 =
new
MenuItem(
"menu item 1"
);
       Â
MenuItem menuItem2 =
new
MenuItem(
"menu item 2"
);
       Â
MenuItem menuItem3 =
new
MenuItem(
"menu item 3"
);
Â
ÂÂ Â Â Â Â Â Â Â
// add menu items to menu
       Â
contextMenu.getItems().add(menuItem1);
       Â
contextMenu.getItems().add(menuItem2);
       Â
contextMenu.getItems().add(menuItem3);
Â
ÂÂ Â Â Â Â Â Â Â
// label to display events
       Â
Label label =
new
Label(
"context menu hidden"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create window event
       Â
EventHandler<WindowEvent> event =
new
EventHandler<WindowEvent>() {
           Â
public
void
handle(WindowEvent e)
           Â
{
               Â
if
(contextMenu.isShowing())
                   Â
label.setText(
"context menu showing"
);
               Â
else
                   Â
label.setText(
"context menu hidden"
);
           Â
}
       Â
};
Â
ÂÂ Â Â Â Â Â Â Â
// add event
       Â
contextMenu.setOnShowing(event);
       Â
contextMenu.setOnHiding(event);
Â
ÂÂ Â Â Â Â Â Â Â
// create a tilepane
       Â
TilePane tilePane =
new
TilePane(label1);
Â
ÂÂ Â Â Â Â Â Â Â
tilePane.getChildren().add(label);
Â
ÂÂ Â Â Â Â Â Â Â
// setContextMenu to label
       Â
label.setContextMenu(contextMenu);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene sc =
new
Scene(tilePane,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(sc);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String args[])
   Â
{
       Â
// launch the application
       Â
launch(args);
   Â
}
}
Output:
Note : The above programs might not run in an online compiler please use an offline IDE.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html