Please note: Although I mention Swing and MacOS here, this question has nothing to do with either of them: I'm just providing them as a concrete example of what I'm trying to do.
I'm trying to set a system property the groovy way. If you are developing a Swing app on a Mac, it is common practice to set the following system property so that your Swing app's menu looks the same as typical Mac apps:
System.setProperty("apple.laf.useScreenMenuBar", "true")
When I call that inside my main
method, it has the desired effect (the menu bar gets pulled off of the JFrame
and is pinned to the top of the screen).
But when I go to try and make that call groovier:
System.properties['apple.laf.useScreenMenuBar', 'true']
it doesn't work. No exceptions, it just stops working and doesn't have the desired effect in the UI. Why, and what can I do to fix it?
When a Groovy class definition declares a field without an access modifier, then a public setter/getter method pair and a private instance variable field is generated which is also known as "property" according to the JavaBeans specification.
Should be:
System.properties['apple.laf.useScreenMenuBar'] = true
or
System.properties.'apple.laf.useScreenMenuBar' = true
In this piece of code:
System.properties['apple.laf.useScreenMenuBar', 'true']
['apple.laf.useScreenMenuBar', 'true']
is taken as the key. See below:
def m = [ [1, 2,]:3, 2:4 ]
assert m[1, 2] == 3
The following piece of code returns correct results:
System.properties['lol'] = 2
assert 2 == System.properties['lol']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With