1

I need to selectively format parts of a paragraph, and JEditorPane is the only thing I could find that would do that for me. But when I have a line that wraps around in the editor pane and there is an element bound below it in SpringLayout, the editor pane doesn't display all of its text: it only shows as many lines as there would be without text wrapping. As shown by the example below, whatever element you put below the editor pane has the right spacing for if the text wrapped, and if you look closely or try to copy/paste the contents you can see the text is there, just behind the background.

When you go to resize it, things really get fun. At first it looked like resizing the window would fix the problem, but then I noticed something. If you change the window size one pixel at a time, when the text wraps to a new line or stops wrapping to an old one, the editor pane keeps its old display size. The element below it moves to where it should be, but the pane doesn't get printed correctly until you resize the window again. It's like the pane is being repainted, then the work to see whether a resize is needed is done after, but never displayed. So the editor pane will always be a step behind.

package main.java.com.characterCreator.GUI;

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

import static javax.swing.SpringLayout.*;

public class JavaPlz extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(()  -> new JavaPlz().buildMainScreen());
    }

    public JavaPlz() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(1080, 720);
        // because I'm edgy, and once you go black, you never go back.
        setBackground(Color.BLACK);
    }

    public void buildMainScreen() {
        setContentPane(new JPanel());
        SpringLayout layout = new SpringLayout();
        getContentPane().setLayout(layout);

        JLabel topLabel = new JLabel("This is the first thing");
        getContentPane().add(topLabel);
        layout.putConstraint(NORTH, topLabel, 10, NORTH, getContentPane());
        layout.putConstraint(WEST, topLabel, 10, WEST, getContentPane());
        layout.putConstraint(EAST, topLabel, -10, EAST, getContentPane());

        JEditorPane problematicDisplay = new JEditorPane("text/html", null);
        problematicDisplay.setText("<b>Bold stuff:</b> Not bold stuff<br><b>More Bold stuff</b> Lots of not bold stuff. Like, a copious, gargantuan, insanely way too much amount of not bold stuff. So much, even, that it will not all fit on the same line and will be forced to spill over onto the next, causing the problem I have that I am trying to solve.<br><b>Second to last line</b><br><i>jazz hands</i>");
        getContentPane().add(problematicDisplay);
        layout.putConstraint(NORTH, problematicDisplay, 10, SOUTH, topLabel);
        layout.putConstraint(WEST, problematicDisplay, 10, WEST, getContentPane());
        layout.putConstraint(EAST, problematicDisplay, -10, EAST, getContentPane());

        JLabel bottomLabel = new JLabel("This is the last thing.");
        getContentPane().add(bottomLabel);
        layout.putConstraint(NORTH, bottomLabel, 10, SOUTH, problematicDisplay);
        layout.putConstraint(WEST, bottomLabel, 10, WEST, getContentPane());
        layout.putConstraint(EAST, bottomLabel, -10, EAST, getContentPane());
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

0 Answers0