2 Replies Latest reply: Jun 30, 2013 5:57 PM by Jörg RSS

    JTextPane questions

    Jörg

      Hello,

       

      I'm trying to get my first JTextPane working. So I have three questions to the following code:

       

      1. My first attempt to increase the initial font size was in firing an action. But nothing happened at all. Why?

      2. I thought my code would give the second of the three sample lines an attribute of BOLD. But instead the size is increased. Again why?

      3. When saving the document, the only line having an attribute is not saved. Any explanation for this?

       

      Regards

      Jörg

       

      PS:

      Are there no code tags and no way to preview one's posting in the forum any more?

      {code}

      import java.awt.*;

      import java.awt.event.*;

      import java.awt.font.*;

      import java.io.*;

      import javax.swing.*;

      import javax.swing.text.*;

      import javax.swing.text.html.*;

       

      public class SaveAndLoadTextPane {

        Font initialFont;

        JButton bLoad, bSave;

        JTextPane tp;

        String filspc= "MySavedTextPane.html";

       

       

        public SaveAndLoadTextPane() {

          tp= new JTextPane();

          tp.setContentType("text/html");

          initialFont= new Font("Serif", Font.PLAIN, 19);

      //  1)

      //    Action fontAction= new StyledEditorKit.FontSizeAction("19", 19);

      //    fontAction.actionPerformed(null);

      //  2)

          tp.setFont(initialFont);

          tp.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

       

      //  Put some styled text.

          HTMLDocument doc= (HTMLDocument)tp.getStyledDocument();

          SimpleAttributeSet attrs= new SimpleAttributeSet();

          attrs.addAttribute(TextAttribute.FONT, new Font("Serif", Font.BOLD, 19));

          try {

            doc.insertString(0, "This is the first line.\n", null);

            doc.insertString(doc.getLength(), "Here comes line two.\n", attrs);

            doc.insertString(doc.getLength(), "Follows number three.\n", null);

          }

          catch (BadLocationException e) {

            e.printStackTrace();

          }

        }

       

       

        public static void main(String args[]) {

          EventQueue.invokeLater(new Runnable() {

            public void run() {

          new SaveAndLoadTextPane().createAndShowGUI();

            }

          });

        }

       

       

        private void createAndShowGUI() {

          JFrame frame = new JFrame("Save and Load a JTextPane");

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          frame.setContentPane(createContent());

          frame.setSize(300, 300);

          frame.setLocationRelativeTo(null);

          frame.setVisible(true);

        }

       

       

        private JPanel createButtonPanel() {

          JPanel p= new JPanel();

          ActionListener buttonListener= new ButtonListener();

          bSave= new JButton("Save");

          bSave.addActionListener(buttonListener);

          p.add(bSave);

          bLoad= new JButton("Load");

          bLoad.setEnabled(false);

          bLoad.addActionListener(buttonListener);

          p.add(bLoad);

          return p;

        }

       

       

        private Container createContent() {

          Container c= new Container();

          c.setLayout(new BorderLayout());

          c.add(tp, BorderLayout.CENTER);

          c.add(createButtonPanel(), BorderLayout.SOUTH);

          return c;

        }

       

       

        private void loadTp() {

          try (BufferedReader in= new BufferedReader(new FileReader(filspc)) ) {

            HTMLEditorKit kit = new HTMLEditorKit();

            HTMLDocument doc= (HTMLDocument)tp.getStyledDocument();

            kit.read(in, doc, 0);

          }

          catch (Exception e) {

            e.printStackTrace();

          }

        }

       

       

        private void saveTp() {

          try (BufferedWriter out= new BufferedWriter(new FileWriter(filspc)) ) {

            out.write(tp.getText().trim());

      /*    Writing via the editorKit delivers the same result.

            HTMLEditorKit kit = new HTMLEditorKit();

            HTMLDocument doc= (HTMLDocument)tp.getStyledDocument();

            kit.write(out, doc, 0, doc.getLength());

      */

          }

          catch (Exception e) {

            e.printStackTrace();

          }

          tp.setText("");

        }

       

       

        class ButtonListener implements ActionListener {

          public void actionPerformed(ActionEvent e) {

            Object obj= e.getSource();

            if (obj==bLoad)

          loadTp();

            else {

          saveTp();

          bLoad.setEnabled(true);

            }

          }

        }

       

      }

      {code}

        • 1. Re: JTextPane questions
          Jörg

          In the meantime I found that one has to go via Styles:

           

          public class SaveAndLoadTextPane {
            HTMLDocument doc
            HTMLEditorKit kit;
            JButton bLoad, bSave;
            JTextPane tp;
            String filspc= "MySavedTextPane.html";
          
            public SaveAndLoadTextPane() {
              tp= new JTextPane();
              tp.setContentType("text/html");
              doc= (HTMLDocument)tp.getStyledDocument();
          
          //  Put some styled text.
              SimpleAttributeSet attrs= new SimpleAttributeSet();
              StyleConstants.setFontFamily(attrs, "Serif");
              StyleConstants.setFontSize(attrs, 18);
              try {
                doc.insertString(0, "This is the first line.\n", attrs);
                StyleConstants.setBold(attrs, true);
                doc.insertString(doc.getLength(), "Here comes line two.\n", attrs);
                doc.insertString(doc.getLength(), "Follows number three.\n", null);
              }
              catch (BadLocationException e) {
                e.printStackTrace();
              }
            }
          .
          .
          .
            private void saveTp() {
              try (BufferedWriter out= new BufferedWriter(new FileWriter(filspc)) ) {
          //    out.write(tp.getText().trim());
          //    Writing via the editorKit delivers the same result.
                if (kit==null) kit= (HTMLEditorKit)tp.getEditorKit();
                kit.write(out, doc, 0, doc.getLength());
              }
              catch (Exception e) {
                e.printStackTrace();
              }
              tp.setText("");
            }
          

          But two questions remain:

          1. I don't find a way to give an empty textPane an initial font (size). Probably that's because it is a character attribute - and without a character the textPane reverts always to its default.

          3. The saving problem changed insofar, that now the attributed lines are not saved.

          • 2. Re: JTextPane questions
            Jörg

            To set an initial font, add the following lines after setting the StyleConstants

            kit= (HTMLEditorKit)tp.getEditorKit();
            MutableAttributeSet inputAttributes = kit.getInputAttributes();
            inputAttributes.addAttributes(attrs);
            

            So only question 3 is open.