Documentation


The radialmenu library contains two controls, dejv.jfx.radialmenu.RadialMenu and dejv.jfx.radialmenu.ContextRadialMenu. The former can be placed inside an application like a usual button (which reveals the menu, when clicked), while the latter is suited best for context menus (as the name implies) - it can be displayed dynamically on any location as needed.

To create one of these, an instance of javafx.scene.control.Menu has to be created, and populated with some items, first. RadialMenu is then generated out of this instance.

Usage

  • Import the library into your Maven project:
<dependency>
    <groupId>com.github.dejv78.jfx.radialmenu</groupId>
    <artifactId>radialmenu</artifactId>
    <version>1.0.1</version>
</dependency>

RadialMenu

  • Use it from your FXML like this:
<!-- Import the RadialMenu class: -->
<?import dejv.jfx.radialmenu.RadialMenu?>

<AnchorPane fx:id="pane" xmlns:fx="http://javafx.com/fxml" fx:controller="radialdemo.MyAppFXMLController">

    <RadialMenu fx:id="bMainMenu1" AnchorPane.topAnchor="10" AnchorPane.rightAnchor="10"/>

</AnchorPane>
  • In the next step, you’ll need to initialize the menu in the controller:
package radialdemo;

import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import dejv.jfx.radialmenu.RadialMenu;

public class MyAppFXMLController {

    @FXML
    private RadialMenu bMainMenu1;

    @FXML
    public void initialize() {
      // Create the "Menu" instance
      final Menu mainMenu1 = new Menu("Main menu 1", new Label("M"));

      // Add a sub-menu
      final Menu m11 = new Menu("Sub menu", new Label("1"));
      m11.getItems().add(new MenuItem("Sub menu item 1", new Label("A"));
      m11.getItems().add(new MenuItem("Sub menu item 2", new Label("B"));

      mainMenu1.getItems().add(m11);

      // Add some more items
      mainMenu1.getItems().add(new MenuItem("Main menu item 2", new Label("2"));
      mainMenu1.getItems().add(new MenuItem("Main menu item 3", new Label("3"));

      bMainMenu1.setMenu(mainMenu1);
      bMainMenu1.getRadialMenuParams()
              .setAngles(90, 180) // Set limit angles to 90 and 180 degrees
              .setStyleSheet("fxml/main.css"); // Set the stylesheet
    }
}

ContextRadialMenu

ContextRadialMenu can be used for example like this:

package radialdemo;

import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import dejv.jfx.radialmenu.ContextRadialMenu;

public class MyAppFXMLController {

    @FXML
    public void initialize() {

      final Menu contextMenu = new Menu("Context menu", new Label("C"));

      final MenuItem cmiAction1 = new MenuItem("Context action 1", new Label("1"));

      // Set an action to the menu item
      cmiAction1.setOnAction(event -> {
          // To access the coords of the location, where the menu was triggered:
          final RadialMenuEventSource eventSource = (RadialMenuEventSource) event.getSource();

          // ... do something interesting with the coords
          System.console.out.println("Coords: " + eventSource.getMenuTriggerCoords())
      });

      // Add the menu item to the context menu:
      contextMenu.getItems().add(cmiAddRectangle);

      // Create the ContextRadialMenu in reaction to mouse click:
      pane.addEventHandler(MouseEvent.MOUSE_RELEASED, (event) -> {
          if (event.getButton() == MouseButton.SECONDARY) {

              final ContextRadialMenu contextRadialMenu = new ContextRadialMenu(pane, contextMenu);
              contextRadialMenu.showAt(pane, event.getScreenX(), event.getScreenY());

              event.consume();
          }
      });
   }
}

API Documentation

  • Browse the API documentation here.
  • Download it here.