Skip to main content

ScalaFX Menu Basics

I am working on an application for a personal project, and learning ScalaFX in the process.
Since Menu bars are important parts of the user interface, with easy access to a range of application functionality, I plan to build a menu bar into my product.
This post explores some of what I am discovering about Menu-related features of ScalaFX.

First, let me set up a basic framework for my exploration, a scala object extending JFXApp.
I also import the scene, layout and control packages, since I will need them for the Scene, MenuBar and VBox classes.

import scalafx.application.JFXApp
import scalafx.scene.layout._
import scalafx.scene._
import scalafx.scene.control._

/**
 * Exploring Menu-related classes in ScalaFX
 */

object SfxMenuExplore extends JFXApp {

  // We will define the menu object here.
  
  val wholeLayout = new VBox {
    children = List(menu)
  }
  val myScene = new Scene(wholeLayout, 300, 200)
  stage = new JFXApp.PrimaryStage {
    scene = myScene
    title = "Menu Exploring"
  }
  stage.show
}

Since VBox lays out controls in a vertical stack, I will use it to place the menu bar at the top of the input form.
The code above does not yet define the menu object, just sets the stage for a 300 x 200 pixel window.

Let's create a very basic menu and add it to the above. Something like:



  val menu = new MenuBar {
    menus = List(
      new Menu("File") {
        items = List(
          new MenuItem("New..."),
          new MenuItem("Save")
        )
      },
      new Menu("Edit") {
        items = List(
          new MenuItem("Cut"),
          new MenuItem("Copy"),
          new MenuItem("Paste")
        )
      },
      new Menu("Help") {
        items = List(
            new MenuItem("About")
        )
      }
    )
  }

This code defines a MenuBar that will contain the other elements.
My initial, basic menu bar consists of 3 menus, each with one or more  MenuItems within.
Stick that code into the framework and run it, and get this:


Pretty nice and simple. We get a standard menu system at the top of the form, that application users can interact with in the usual way.

Some users, me included, love to use keyboard fast-keys or accelerators or short-cuts. On the menu above, I can press the ALT-key on my keyboard and use the arrow keys to navigate the menu structure.

Let's define some of the standard keyboard accelerators for our menus. We will add an underscore right before the key letter, which in these cases are the first letter, but can be later ones if desired.

Also set the mnemonicParsing attribute to true. Our MenuBar definition now looks like this:

  val menu = new MenuBar {
    menus = List(
      new Menu("_File") {
        mnemonicParsing = true
        items = List(
          new MenuItem("New..."),
          new MenuItem("Save")
        )
      },
      new Menu("_Edit") {
        mnemonicParsing = true
        items = List(
          new MenuItem("Cut"),
          new MenuItem("Copy"),
          new MenuItem("Paste")
        )
      },
      new Menu("_Help") {
        mnemonicParsing = true
        items = List(
            new MenuItem("About")
        )
      }
    )
  }

And the result is:



Now our menu visually shows the standard accelerators, and functions as expected, too.

We can do more with the ScalaFX menu classes. We can add sub-menus to our structure, for example. We'll add a Recent Items list to the File menu. To get a sub-menu, we add a new Menu object into the list of MenuItems.

Then, each of the sub-items gets added to this sub-Menu. Here's what the File Menu becomes:

      new Menu("_File") {
        mnemonicParsing = true
        items = List(
          new MenuItem("New..."),
          new MenuItem("Save"),
          new Menu("Recent _Items") {
            items = List( 
                new MenuItem("Some Item AAA"), 
                new MenuItem("Another item BBB") 
            )
          }
        )
      }

This gives us a sub-menu with two items, as shown below.



A menu can also show the state of some user options, by putting a check beside the item.

ScalaFX has two kinds of checkable menu item. The first is an independent item, that can be toggled on or off. Use the CheckMenuItem class for this kind of menu entry.

The second kind sets up a group of items, from which only one may be selected. Use the RadioMenuItem class for this kind.

This example shows both kinds.

  val nameOrderDisplay = new ToggleGroup
  val menu = new MenuBar {
    menus = List(
// duplicated bits skipped ...
      new Menu("_Display") {
        mnemonicParsing = true
        items = List(
          new RadioMenuItem("FirstName LastName") { toggleGroup = nameOrderDisplay },
          new RadioMenuItem("LastName, FirstName") { toggleGroup = nameOrderDisplay },
          new SeparatorMenuItem,
          new CheckMenuItem("Prefixes"),
          new CheckMenuItem("Suffixes")
        )
      },
// duplicated bits skipped ...
    )
  }

I've added a new Display menu. Under it, there are four items. The first two are grouped together so that only one at a time may be selected.

We use the RadioMenuItem class to define the items, and assign them to the same toggleGroup, which we've defined at the top of the code block.

Now, when we use the application, these items define the display order of the names of individuals.
The second kind of items appear below the separator (defined with the SeparatorMenuItem class). Since they are not grouped, and are independent of one another, we can set none, one, or both of them.

The result is this:



There is lots more to explore in the ScalaFX Menu classes. For one thing, our menu does not currently do anything, we have not attached any actions to the items.
I'll talk about some more Menu topics next time.

Popular posts from this blog

Git Reset in Eclipse

Using Git and the Eclipse IDE, you have a series of commits in your branch history, but need to back up to an earlier version. The Git Reset feature is a powerful tool with just a whiff of danger, and is accessible with just a couple clicks in Eclipse. In Eclipse, switch to the History view. In my example it shows a series of 3 changes, 3 separate committed versions of the Person file. After commit 6d5ef3e, the HEAD (shown), Index, and Working Directory all have the same version, Person 3.0.

Scala Collections: A Group of groupBy() Examples

Scala provides a rich Collections API. Let's look at the useful groupBy() function. What does groupBy() do? It takes a collection, assesses each item in that collection against a discriminator function, and returns a Map data structure. Each key in the returned map is a distinct result of the discriminator function, and the key's corresponding value is another collection which contains all elements of the original one that evaluate the same way against the discriminator function. So, for example, here is a collection of Strings: val sports = Seq ("baseball", "ice hockey", "football", "basketball", "110m hurdles", "field hockey") Running it through the Scala interpreter produces this output showing our value's definition: sports: Seq[String] = List(baseball, ice hockey, football, basketball, 110m hurdles, field hockey) We can group those sports names by, say, their first letter. To do so, we need a disc

Java 8: Rewrite For-loops using Stream API

Java 8 Tip: Anytime you write a Java For-loop, ask yourself if you can rewrite it with the Streams API. Now that I have moved to Java 8 in my work and home development, whenever I want to use a For-loop, I write it and then see if I can rewrite it using the Stream API. For example: I have an object called myThing, some Collection-like data structure which contains an arbitrary number of Fields. Something has happened, and I want to set all of the fields to some common state, in my case "Hidden"

How to do Git Rebase in Eclipse

This is an abbreviated version of a fuller post about Git Rebase in Eclipse. See the longer one here : One side-effect of merging Git branches is that it leaves a Merge commit. This can create a history view something like: The clutter of parallel lines shows the life spans of those local branches, and extra commits (nine in the above screen-shot, marked by the green arrows icon). Check out this extreme-case history:  http://agentdero.cachefly.net/unethicalblogger.com/images/branch_madness.jpeg Merge Commits show all the gory details of how the code base evolved. For some teams, that’s what they want or need, all the time. Others may find it unnecessarily long and cluttered. They prefer the history to tell the bigger story, and not dwell on tiny details like every trivial Merge-commit. Git Rebase offers us 2 benefits over Git Merge: First, Rebase allows us to clean up a set of local commits before pushing them to the shared, central repository. For this

Code Coverage in C#.NET Unit Tests - Setting up OpenCover

The purpose of this post is to be a brain-dump for how we set up and used OpenCover and ReportGenerator command-line tools for code coverage analysis and reporting in our projects. The documentation made some assumptions that took some digging to fully understand, so to save my (and maybe others') time and effort in the future, here are my notes. Our project, which I will call CEP for short, includes a handful of sub-projects within the same solution. They are a mix of Web APIs, ASP MVC applications and Class libraries. For Unit Tests, we chose to write them using the MSTest framework, along with the Moq mocking framework. As the various sub-projects evolved, we needed to know more about the coverage of our automated tests. What classes, methods and instructions had tests exercising them, and what ones did not? Code Coverage tools are conveniently built-in for Visual Studio 2017 Enterprise Edition, but not for our Professional Edition installations. Much less for any Commun