Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent HTML JTextPane from formatting text input
    text
    copied!<p>I've written a sample application that consists of a <code>JTextPane</code> and a button that prints out the contents of the text pane. The text pane's content type is set to "text/html".</p> <p>When I enter some text and print it out to console, the text is automatically wrapped.</p> <p>For example, given the following input:</p> <pre><code>Watercress shallot radish chickweed bitterleaf bush tomato salad garlic beet greens black-eyed pea green bean salsify bell pepper. Catsear celery green bean broccoli rabe spring onion tatsoi prairie turnip wattle seed cucumber burdock. Chickweed cucumber chickpea bush tomato wakame taro fava bean arugula catsear azuki bean. </code></pre> <p>The output comes out as such</p> <pre><code>Watercress shallot radish chickweed bitterleaf bush tomato salad garlic beet greens black-eyed pea green bean salsify bell pepper. Catsear celery green bean broccoli rabe spring onion tatsoi prairie turnip wattle seed cucumber burdock. Chickweed cucumber chickpea bush tomato wakame taro fava bean arugula catsear azuki bean. </code></pre> <p>Is there a way to preserve the format of the input text? I am planning to store the input in a database, so I'm replacing new-line characters with <code>&lt;br&gt;</code>, but the text pane is automatically inserting its own new-lines.</p> <p>If there are alternate solutions I could try those as well, but for the most part I want to be able to display HTML while allowing users to modify input.</p> <p>Here is the code for the sample. You should be able to just compile and run it.</p> <pre><code>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextPane; public class TextPaneTest extends JFrame { private JTextPane textPane = null; public static void main(String args[]) { new TextPaneTest(); } public TextPaneTest() { // Main panel JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); // Panel holding text JPanel textPanel = new JPanel(); textPanel.setLayout(new BorderLayout()); // Text pane textPane = new JTextPane(); textPane.setContentType("text/html"); // Button JButton button = new JButton(); button.setText("Print Text"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = textPane.getText(); text = text.substring(text.indexOf("&lt;body&gt;") + 6, text.indexOf("&lt;/body&gt;")); System.out.println(text); } }); textPanel.add(textPane); panel.add(textPanel, BorderLayout.CENTER); panel.add(button, BorderLayout.SOUTH); // Add everything to the frame this.add(panel); this.setSize(1000, 500); setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload