3

We want to show a hint for a JList that the user can select multiple items with the platform dependent key for multiselect.

However I have not found any way to show the OS X COMMAND symbol in a JLabel, which means the symbol that's printed on the apple keyboard on the command key, also called apple key.

Here's a picture of the symbol I want to display on OS X. COMMAND SYMBOL
(source: wikimedia.org)

Also I do want to have it platform independent.

I.e. something like

component.add( new JList() , BorderLayout.CENTER );
component.add( new JLabel( MessageFormat.format("With {0} you can " 
  + "select multiple items", 
  KeyStroke.getKeyStroke( ... , ... ) ) ) , BorderLayout.SOUTH );

Where instead of the {0} there should appear above seen symbol...

Does any one of you guys know how to do this? I know it must be possible somehow since in the JMenuItems there is the symbol...

My own (non graphical solutions) looks like this:

add( new JLabel( MessageFormat.format(
  "With {0} you can select multiple items" , 
  System.getProperty( "mrj.version" ) != null ? "COMMAND" : "CTRL" ) ) ,
  BorderLayout.SOUTH );
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Daniel Hiller
  • 3,415
  • 3
  • 23
  • 33

4 Answers4

2

The symbol in question is avaiable through Unicode, and the HTML character sets. All you need to do is make your JLabel display HTML by starting its text string with <html> and then include the character code.

JLabel label = new JLabel( "<html>&#8984; is the Apple command symbol." );

This will work on a Mac, but I've no idea what it'll do on other platforms, although you do seem to have that covered off.

banjollity
  • 4,490
  • 2
  • 29
  • 32
1

As David points out, you can use the Unicode escape sequence \u2318 although it must be displayed with a font supporting it.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
0

Your solution looks perfect. I assume you intend to factor out the hint code so you reuse it.

add( new JLabel( MessageFormat.format(
  "With {0} you can select multiple items", 
  getMetaKeyHint(),
  BorderLayout.SOUTH );

public String getMetaKeyHint() {
    return System.getProperty( "mrj.version" ) != null ? "COMMAND" : "CTRL" );
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • Yes that would be my intention. Although I'd prefer to show the COMMAND key _symbol_, which means the symbol that's printed on the keyboard on the apple/command key. – Daniel Hiller Oct 25 '08 at 10:45
0

I use the following code to check for the system and load accordingly

(System.getProperty("os.name").toUpperCase(Locale.US).indexOf("MAC OS X") == 0 )
Dan
  • 1,711
  • 1
  • 22
  • 36
  • Although your solution may also work, the mac developer guides state that the "proper way" to check wether your application platform is Mac OS X is checking the property "mrj.version" for a non-empty string. Annoying, eh? ;-) – Daniel Hiller Oct 25 '08 at 10:57