Display the OSX Command symbol key in a JLabel

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 );

Asked by: Anna606 | Posted: 28-01-2022

Answer 1

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.

Answered by: Maddie160 | Posted: 01-03-2022

Answer 2

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

Answered by: Thomas471 | Posted: 01-03-2022

Answer 3

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", 
  BorderLayout.SOUTH );

public String getMetaKeyHint() {
    return System.getProperty( "mrj.version" ) != null ? "COMMAND" : "CTRL" );

Answered by: Sawyer590 | Posted: 01-03-2022

Answer 4

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 )

Answered by: Michael583 | Posted: 01-03-2022

Similar questions

java - How to run and display the result of a shell command ssh with JSch?

I try to use the library JSch - Java Secure Channel make an ssh connection in my Android app, it works. Now I would like to execute a command and retrieve the result. I tried several methods that works best is this. However, this method works only in part, because for some reason I can not explain, my program stops at the end of my wh...

java - How do i display linux command result in html

This question already has an answer here:

java - How to display an array to the user

I'm trying to display the contents of an ordered array in something like a JTextField. for (int i=0; i&lt;array.length; i++) { this.textField.setText(array[i]); } This won't work for two reasons. The first minor reason: if the array length is 4 then jtextfield is getting it's value reset 4 times rather than appending each element onto the last. Second reason: The JTextField only takes ...

How to display java applet inside GWT page?

I'm probably missing something simple here, but I can't find the answer elsewhere. I just want to display an applet in my GWT code. OS: Windows XP Java: JDK 1.6.0_10 Other: GWT, GWT-Ext 2.0.5 Here is the applet (obviously simplified for testing): package foo.applet; import javax.swing.JApplet; import java.awt.Graphics; public class HelloApplet extends JApplet { public void paint(Grap...

java - display image in servlet

I am having the following requirement. When the user cliock the buton the image should be displayed.The following code that i have tried gives me error Toolkit tk = Toolkit.getDefaultToolkit(); Image ima = Toolkit.getImage("C:\\DB.jpg"); MediaTracker mt = new MediaTracker(new Canvas()); mt.addImage(ima, 0); try { mt.waitForAll(); } catch(Exception x) {} OutputSt...

java - Display BMP in JLabel

Java can display png, jpg a some other picture formats, but i have to display a bmp file in a JLable by getting the file path. ImageIcon imageIcon = new ImageIcon(imageFile.getAbsolutePath()); ImageIcon support the typical png,gif,jpg images. In the project i am working, i can not open a bmp file and store the same file as a jpg, because i am not allow to store somethi...

java - display series of images with next button

How can I display a series of images one by one every time the user clicks the next button? I have few images stored in array. I used button next to go from image to image. How to programme the button? I used action listener but I just don't know how to get back to the array.

java - display of wrong time

I have written a method using JSF: public static String getCurrentTime(Locale locale) { Calendar cal=Calendar.getInstance(locale); return new SimpleDateFormat("HHmmss").format(cal.getTime()); } While testing my application in my local machine I am getting the time same as system time but when my friends are testing it time delay is of 4 minutes. What may be the ...

How can I write to XML via Java and display it via HTML?

How can we insert a record into an XML file using Java? How can we display one record from this XML file using HTML?

java - How to display image in mail after deleted in app server?

I have code which creates an image in an application server and that image has been referred in mail html. After sending mail, image will be deleted in application server. When I open the mail, the image is not getting displayed in the mail. I think my code is deleting the image before it is copied to the mail server. I have checked by deleting the image manually. First I opened the mail (this time image ...

java - How to display system icon for a file in SWT?

I want to display a file tree similarly to java2s.com 'Create a lazy file tree', but include the actual system icons - especially for folders. SWT does not seem to offer this (Program API does not support folders), so I came up with the following: public Image getImage(File file) { ImageIcon systemIcon =...

How do I get a an image to display in JTable in java swing

I have created a jTable and have put an ImageIcon objects in one of the columns. I would like to know how I can get it to display the image rather than the image name as a string. Thanks

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)