简体   繁体   中英

Java TrayIcon.displayMessage() and line-breaks

I'm writing a Java app with a SystemTray icon, and I'd like to put line-breaks into the TrayIcon's "display message", but the normal html trick doesn't seem to work (like it does inside JLabels).

In the example below, the trayIcon variable below is of type "java.awt.TrayIcon".

trayIcon.displayMessage("Title", "<p>Blah</p> \\r\\n Blah <br> blah ... blah", TrayIcon.MessageType.INFO);

Java ignores the \\r\\n, but displays the html tags.

Any ideas?

If not, I'll use a JFrame or something.

UPDATE: it seems to be a platform-specific issue, and I should have specified my OS in the question: I need this to work on Windows and Linux.

Nishan showed that a \\n works on Windows, and I confirmed with a Vista box I now have next to me. It looks like I'll need to make something custom with a JFrame or a messagebox

Cheers guys

As mentioned here , it is not possible to show new lines in tray icon messages in Linux.

Just now got a tricky idea which worked for me. Observed number of characters getting displayed per line in the message. For me it is showing 56 characters per line.

So, if a line has got less than 56 characters, fill the gap with spaces to make it 56 characters.

Knew this is not a proper way but could not find other alternatives. Now my output is as expected.

private java.lang.String messageToShow = null;
private int lineLength = 56;
/*This approach is showing ellipses (...) at the end of the last line*/
public void addMessageToShow(java.lang.String messageToShow) {
        if (this.messageToShow == null){
            this.messageToShow = messageToShow;
        }else{
            this.messageToShow += "\n" + messageToShow;//Working perfectly in windows.
        }
        if (System.getProperty("os.name").contains("Linux")){
            /*
             Fill with blank spaces to get the next message in next line.
             Checking with mod operator to handle the line which wraps 
             into multiple lines
            */
            while (this.messageToShow.length() % lineLength > 0){
                this.messageToShow += " ";
            }
        }
    }

So, i tried another approach

/*This approach is not showing ellipses (...) at the end of the last line*/
public void addMessageToShow(java.lang.String messageToShow) {
    if (this.messageToShow == null){
        this.messageToShow = messageToShow;
    }else{
        if (System.getProperty("os.name").contains("Linux")){
            /*
             Fill with blank spaces to get the next message in next line.
             Checking with mod operator to handle the line which wraps 
             into multiple lines
            */
            while (this.messageToShow.length() % lineLength > 0){
                this.messageToShow += " ";
            }
        }else{
            this.messageToShow += "\n";// Works properly with windows
        }
        this.messageToShow += messageToShow;
    }
}

And finally

trayIcon.displayMessage("My Title", this.messageToShow, TrayIcon.MessageType.INFO);

附加\\ n对我有用:

"<HtMl><p>Blah</p> \n Blah <br> blah ... blah"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM