Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to toggle menu items in JavaFX?
JavaFX supports three kinds of menu items namely − check menu item, custom menu item, and, radio menu item.
A RadioMenuItem is a special MenuItem that has a checkmark (tick) similar to a checkbox. This has two states selected (with a checkmark) and unselected (without checkmark). It is represented by the javafx.scene.control.RadioMenuItem class.
You can add a bunch of radio-menu-items to a toggle group just like a toggle button or a radio button.
Example
Following JavaFX example demonstrates the creation of a toggled group of radio-menu-items
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class RadioMenuItem_Toggle extends Application {
@Override
public void start(Stage stage) {
//Creating a menu
Menu fileMenu = new Menu("File");
//Creating menu Items
Menu view = new Menu("Align");
MenuItem edit = new MenuItem("Edit");
MenuItem exit = new MenuItem("Exit");
//Creating menu items for the sub item edit
RadioMenuItem sub1 = new RadioMenuItem("Right");
RadioMenuItem sub2 = new RadioMenuItem("Left");
RadioMenuItem sub3 = new RadioMenuItem("Center");
//Adding the Sub menu items to a toggle group
ToggleGroup group = new ToggleGroup();
sub1.setToggleGroup(group);
sub2.setToggleGroup(group);
sub3.setToggleGroup(group);
//Adding toggle group to the view menu item
view.getItems().addAll(sub1, sub2, sub3);
//Creating a separator
SeparatorMenuItem sep = new SeparatorMenuItem();
//Adding all the menu items to the menu
fileMenu.getItems().addAll(edit, view, sep, exit);
//Creating a menu bar and adding menu to it.
MenuBar menuBar = new MenuBar(fileMenu);
menuBar.setTranslateX(200);
menuBar.setTranslateY(20);
//Setting the stage
Group root = new Group(menuBar);
Scene scene = new Scene(root, 595, 200, Color.BEIGE);
stage.setTitle("Menu Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
Output

Advertisements