最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - JOptionPane: ignore line feeds in HTML - Stack Overflow

programmeradmin0浏览0评论

Can I make Swing's JOptionPane tolerate line feeds in HTML? For example, this displays the string hello (as I expect).

package demos.dialog.optionPane;

import javax.swing.JOptionPane;

public class JOptionPaneDemo {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "<html>Hello</html>");
    }
}

while this one displays Hello</html> instead

package demos.dialog.optionPane;

import javax.swing.JOptionPane;

public class JOptionPaneDemo {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "<html>\nHello</html>");
    }
}

Note the HTML text comes from a DB, I cannot prevent such inputs. Besides, it's a valid HTML anyway, so there's nothing to sanitize really. Unless I'm misunderstanding the spec, all whitespace must be ignored during HTML rendering. Online renderers like this one also have no problem parsing it.

Is there a solution to this problem that doesn't involve manually replacing all line feeds to make Swing happy?

Java 8.

Can I make Swing's JOptionPane tolerate line feeds in HTML? For example, this displays the string hello (as I expect).

package demos.dialog.optionPane;

import javax.swing.JOptionPane;

public class JOptionPaneDemo {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "<html>Hello</html>");
    }
}

while this one displays Hello</html> instead

package demos.dialog.optionPane;

import javax.swing.JOptionPane;

public class JOptionPaneDemo {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "<html>\nHello</html>");
    }
}

Note the HTML text comes from a DB, I cannot prevent such inputs. Besides, it's a valid HTML anyway, so there's nothing to sanitize really. Unless I'm misunderstanding the spec, all whitespace must be ignored during HTML rendering. Online renderers like this one also have no problem parsing it.

Is there a solution to this problem that doesn't involve manually replacing all line feeds to make Swing happy?

Java 8.

Share Improve this question edited Nov 28, 2024 at 13:06 user85421 29.7k11 gold badges65 silver badges94 bronze badges asked Nov 20, 2024 at 8:16 CagepiCagepi 3111 silver badge7 bronze badges 7
  • Are you saying that \n is valid HTML? – Abra Commented Nov 20, 2024 at 8:33
  • @Abra line feeds (and white space in general) should be ignored in HTML, sure. I believe the spec says exactly that – Cagepi Commented Nov 20, 2024 at 8:40
  • @Abra well, for one thing, you didn't follow all the steps to reproduce. I mentioned in my question Java 8 (we didn't upgrade any further). Maybe, it's relevant – Cagepi Commented Nov 20, 2024 at 8:51
  • 1 You probably know this, but… Swing’s HTML renderer is ancient and currently is barely maintained. It is not fit for general display of HTML documents. It’s really only good for displaying developer-controlled HTML content, and developers have to tailor that content so it only uses HTML 3.2 and early CSS capabilities. For displaying HTML that you don’t control, I would seriously consider the WebView class of JavaFX. (But beware the security implications of external links in such content.) – VGR Commented Nov 20, 2024 at 13:36
  • 1 @VGR actually, we have that in one place. We get a Swing panel and put a JavaFX component inside (or rather javafx.embed.swing.JFXPanel, an FX/Swing adapter). Imo, mixing two GUI libraries like that is messy, unlikely best practice – Cagepi Commented Nov 20, 2024 at 14:06
 |  Show 2 more comments

1 Answer 1

Reset to default 3

The problem is that JOptionPane does not just delegate the text rendering to another component like JLabel but has a multi-line support on its own that interferes with the other text processing.

E.g. JOptionPane.showMessageDialog(null, "hello\nworld"); will render two lines of text, using two JLabel instances.

When you use the message "<html>\nHello</html>", the JOptionPane will split it into two “lines”, "<html>" and "Hello</html>", and create a JLabel for each. The first will have HTML rendering enabled but produce no visible content whilst the second will have HTML rendering disabled, to the result we see.

The solution is to enforce having a single JLabel as a message, processing the entire string as one HTML page:

JOptionPane.showMessageDialog(null, new JLabel("<html>\nHello</html>"));
发布评论

评论列表(0)

  1. 暂无评论