I first encountered such a problem that when I save the text and load it again in the JTextPane, the centering remains in the position in which the text was saved for the first time. That is, if at the first time I made a center alignment and saved the text, then all further attempts to change any piece of text will end up with the text being saved in the center in any case.

I save the text in the database, unload the text from the database using unescapeJava to save the transitions to the new line

setText(unescapeJava(rs.getString("Text")); 

As a result of the first save I get

  <html> <head> </head> <body> <p style="margin-top: 0"> Left </p> <p align="center" style="margin-top: 0"> Centr </p> <p align="right" style="margin-top: 0"> Right </p> </body> </html> 

As a result of the second

  <html> <head> </head> <body> <p align="right" style="margin-top: 0"> Left </p> <p align="right" style="margin-top: 0"> Center </p> <p align="right" style="margin-top: 0"> Right </p> <p align="right" style="margin-top: 0"> Лево </p> <p align="right" style="margin-top: 0"> Центр </p> <p align="right" style="margin-top: 0"> Право </p> </body> </html> 

Moreover, if you change only the first three lines where "Left, Center, Right", then everything will work, if you add something, everything will be lost. In TextPane, when editing, everything is displayed as needed, while saving, you can see how it is recorded.

Here is the code of my text editor.

  import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; import static org.apache.commons.lang3.StringEscapeUtils.unescapeJava; public class TextEditor extends JFrame { private JPanel panel = new JPanel(new BorderLayout()); private JTextPane textPane = new JTextPane(); private int id = 0; private String nameAutor = ""; private String date = ""; public TextEditor(String edit, int id, String date, String nameAutor) { textPane.setContentType("text/html"); edit = edit.replaceAll("&lt;", "<"); edit = edit.replaceAll("&gt;", ">"); textPane.setText(edit); this.id = id; this.date = date; this.nameAutor = nameAutor; initComponent(); this.setSize(600, 500); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setLocationRelativeTo(null); this.setResizable(true); this.getContentPane().add(panel); this.setVisible(true); } public void initComponent() { panel.setBackground(Color.GRAY); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setBorder(null); nameAutor = nameAutor + " " + date; panel.add(new JLabel(nameAutor) {{ setForeground(Color.WHITE); }}, BorderLayout.PAGE_END); panel.add(new toolBar(), BorderLayout.PAGE_START); panel.add(scrollPane, BorderLayout.CENTER); } class toolBar extends JToolBar { public toolBar() { final String iconCenter = "/icons/AlignCenterHK.png"; final String iconLeft = "/icons/AlignLeftHK.png"; final String iconRight = "/icons/AlignRightHK.png"; final String iconKursiv = "/icons/ItalicHK.png"; final String iconUnder = "/icons/UnderlineHK.png"; final String iconJirniy = "/icons/BoldHK.png"; final String iconFind = "/icons/FindHK.png"; final String iconSave = "/icons/SaveHK.png"; setFloatable(false); setIgnoreRepaint(true); add(new JButton() {{ setAction(kursiv()); setText(""); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconKursiv))); }}); add(new JButton() {{ setAction(jirnyi()); setText(""); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconJirniy))); }}); add(new JButton() {{ setAction(normal()); setText(""); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconUnder))); }}); add(new JButton() {{ setAction(left()); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconLeft))); }}); add(new JButton() {{ setAction(center()); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconCenter))); }}); add(new JButton() {{ setAction(right()); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconRight))); //setIcon(new ImageIcon()); }}); add(new JButton() {{ setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconFind))); }}); add(new JButton("img") {{ }}); add(new JButton() {{ addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DataBase db = new DataBase(); db.DBConnect(); System.out.println(textPane.getText()); db.setArticle("Text", unescapeJava(textPane.getText()), String.valueOf(id)); } }); setOpaque(false); setFocusPainted(false); setIcon(new ImageIcon(this.getClass().getResource(iconSave))); }}); } } private Action left() { return new StyledEditorKit.AlignmentAction("", 3); } private Action center() { return new StyledEditorKit.AlignmentAction("", 1); } private Action right() { return new StyledEditorKit.AlignmentAction("", 2); } private Action kursiv() { return new StyledEditorKit.ItalicAction(); } private Action jirnyi() { return new StyledEditorKit.BoldAction(); } private Action normal() { return new StyledEditorKit.UnderlineAction(); } } 

UPDATE: A simplified version of the program, the result is the same: (1-3 Button (Left, Center, Right) 4 button (Save))

  import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private static String str = ""; public static void main(String[] args) { TextEditor te = new TextEditor(str); } } class TextEditor extends JFrame { private JPanel panel = new JPanel(new BorderLayout()); private JTextPane textPane = new JTextPane(); private String text = ""; public TextEditor(String edit) { this.text = edit; textPane.setContentType("text/html"); text = edit.replaceAll("&lt;", "<"); text = edit.replaceAll("&gt;", ">"); textPane.setText(text); initComponent(); this.setSize(600, 500); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setLocationRelativeTo(null); this.setResizable(true); this.getContentPane().add(panel); this.setVisible(true); } public void initComponent() { panel.setBackground(Color.GRAY); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setBorder(null); panel.add(new toolBar(), BorderLayout.PAGE_START); panel.add(scrollPane, BorderLayout.CENTER); } class toolBar extends JToolBar { public toolBar() { setFloatable(false); setIgnoreRepaint(true); add(new JButton() {{ setAction(left()); setOpaque(false); setFocusPainted(false); }}); add(new JButton() {{ setAction(center()); setOpaque(false); setFocusPainted(false); }}); add(new JButton() {{ setAction(right()); setOpaque(false); setFocusPainted(false); }}); add(new JButton() {{ addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { text = textPane.getText(); dispose(); TextEditor te = new TextEditor(text); } }); setOpaque(false); setFocusPainted(false); }}); } } private Action left() { return new StyledEditorKit.AlignmentAction("", 3); } private Action center() { return new StyledEditorKit.AlignmentAction("", 1); } private Action right() { return new StyledEditorKit.AlignmentAction("", 2); } } 

    0