Reputation: 117
Would anyone know how to change the line break attribute for a JEditorPane
?
I can't locate line breaks in my JTextPane
s text (it's neither \n neither \r), and so I can't count the number of lines of this text properly. I would like to change the line break attribute for \n.
Upvotes: 1
Views: 1180
Reputation: 57421
Use javax.swing.text.Utilities.getRowStart()/getRowEnd()
to count number of lines.
In fact when text is wrapped no char is inserted. See http://java-sl.com/wrap.html to understand how the wrap works.
Upvotes: 3
Reputation: 347314
This example works for me...
public class TestEditorPane {
public static void main(String[] args) {
new TestEditorPane();
}
public TestEditorPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new EditorPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class EditorPane extends JPanel {
private JEditorPane editor;
public EditorPane() {
setLayout(new GridBagLayout());
editor = new JEditorPane();
editor.setContentType("text/plain");
JButton button = new JButton("Dump");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = editor.getText();
String[] parts = text.split(System.getProperty("line.separator"));
// String[] parts = text.split("\n\r");
for (String part : parts) {
if (part.trim().length() > 0) {
System.out.println(part);
}
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(editor, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
add(button, gbc);
}
}
}
Under windows, the line break seems to be /n/r
. I've used the system property line.separator
as well, and it seems to work for me.
Upvotes: 3