Skip to content
Advertisement

Text blocks in JOptionPane

The following code shows two labels with the same multiline html text. The labels differ, however, in the way their text is defined: one as a usual string, the other as a text block. The labels are displayed as expected, namely without any difference. The OptionPanes use the very same strings as the labels, but the text block is not rendered properly. Any idea why? (java version “16”)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextBlocks extends JFrame {
  public static final long serialVersionUID = 100L;

  JButton b1, b2;
  String txt1, txt2;

  public TextBlocks() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 240);

    txt1= "<html>"+
        "<h3><font color=fuchsia>Caption</font></h3><font size=3>"+
        "Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>"+
        "sed do eiusmod tempor incidunt ut labore et dolore<br><br>"+ 
        "<h3>Topic 1</h3><font size=3>"+
        "Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>"+
        "sed do eiusmod tempor incidunt ut labore et dolore magna<br>"+
        "aliqua. Ut enim ad minim veniam, quis nostrud<br><br>"+
        "<h3>Topic 2</h3><font size=3>"+
        "Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>"+
        "sed do eiusmod tempor incidunt ut labore et dolore magna<br>"+
        "aliqua. Ut enim ad minim veniam, quis nostrud exercita-<br>"+
        "tion ullamco laboris nisi ut aliquid ex ea commodi</html>";
    JLabel lb= new JLabel(txt1);
    add(lb, BorderLayout.WEST);
    txt2= """
    <html><h3><font color=fuchsia>Caption</font></h3><font size=3>
    Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>
    sed do eiusmod tempor incidunt ut labore et dolore<br><br>
    <h3>Topic 1</h3><font size=3>
    Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>
    sed do eiusmod tempor incidunt ut labore et dolore magna<br>
    aliqua. Ut enim ad minim veniam, quis nostrud<br><br>
    <h3>Topic 2</h3><font size=3>
    Lorem ipsum dolor sit amet, consectetur adipisici elit,<br>
    sed do eiusmod tempor incidunt ut labore et dolore magna<br>
    aliqua. Ut enim ad minim veniam, quis nostrud exercita-<br>
    tion ullamco laboris nisi ut aliquid ex ea commodi</html>
    """;
    lb= new JLabel(txt2);
    add(lb, BorderLayout.CENTER);

    JPanel p= new JPanel();
    b1= new JButton("JOptionPane with text 1");
    ActionListener listener= new ActListener();
    b1.addActionListener(listener);
    p.add(b1);
    b2= new JButton("JOptionPane with text 2");
    b2.addActionListener(listener);
    p.add(b2);
    add(p, BorderLayout.SOUTH);
    pack();
    setVisible(true);
  }


  static public void main(String args[]) {
    EventQueue.invokeLater(TextBlocks::new);
  }


  class ActListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      String buf= evt.getSource()==b1 ? txt1 : txt2;
      JOptionPane.showMessageDialog(TextBlocks.this, buf, "Demo",
                    JOptionPane.INFORMATION_MESSAGE);
    }
  }

}

Advertisement

Answer

The trailing carriage returns in txt2 make the text block unmanageable by your JOptionPane, instead explicitly consider the text block as a one-liner by using at the end of each line

txt2 = """
        <html><h3><font color=fuchsia>Caption</font></h3><font size=3> 
        Lorem ipsum dolor sit amet, consectetur adipisici elit,<br> 
        sed do eiusmod tempor incidunt ut labore et dolore<br><br> 
        <h3>Topic 1</h3><font size=3> 
        Lorem ipsum dolor sit amet, consectetur adipisici elit,<br> 
        sed do eiusmod tempor incidunt ut labore et dolore magna<br> 
        aliqua. Ut enim ad minim veniam, quis nostrud<br><br> 
        <h3>Topic 2</h3><font size=3> 
        Lorem ipsum dolor sit amet, consectetur adipisici elit,<br> 
        sed do eiusmod tempor incidunt ut labore et dolore magna<br> 
        aliqua. Ut enim ad minim veniam, quis nostrud exercita-<br> 
        tion ullamco laboris nisi ut aliquid ex ea commodi</html> 
        """;

This produces the following

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement