admin管理员组文章数量:1335091
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 | Show 2 more comments1 Answer
Reset to default 3The 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>"));
本文标签: javaJOptionPane ignore line feeds in HTMLStack Overflow
版权声明:本文标题:java - JOptionPane: ignore line feeds in HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372285a2462459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
\n
is valid HTML? – Abra Commented Nov 20, 2024 at 8:33javafx.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