Note that there are some explanatory texts on larger screens.

plurals
  1. POEnforcing <strong> instead of <b> in HTMLEditorKit/JEditorPane
    text
    copied!<p>I am trying to ensure that the JEditorPane with HTMLEditorKit uses &lt;<strong>strong</strong>> tags instead of &lt;<strong>b</strong>> tags. The code below loads a JFrame with an JEditorPane. Try selecting a part of the text and clicking the button to turn the selection bold. The System.out shows that the bolding is being caused by tags. </p> <p>How do I set it up so that it is XHTML compliant and uses tags instead?</p> <pre><code>import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.StyledEditorKit; import javax.swing.text.html.HTMLEditorKit; public class BStrongTest extends JPanel { /** * @param args */ public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new BStrongTest()); frame.setSize(300, 200); frame.setVisible(true); } public BStrongTest() { setLayout(new BorderLayout()); final JEditorPane pane = new JEditorPane(); pane.setEditorKit(new HTMLEditorKit()); pane.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent bibendum."); add(pane, BorderLayout.NORTH); JButton boldButton = new JButton(); boldButton.setAction(new StyledEditorKit.BoldAction()); boldButton.setText("Boldify"); add(boldButton, BorderLayout.SOUTH); pane.getDocument().addDocumentListener(new DocumentListener() { @Override public void changedUpdate(DocumentEvent e) { System.out.println(pane.getText()); } @Override public void insertUpdate(DocumentEvent e) { } @Override public void removeUpdate(DocumentEvent e) { } }); } } </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