Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6540255

JTextPane.getText returns old document content however UI shows the correct one

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.5.0_09"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
      Java HotSpot(TM) Client VM (build 1.5.0_09-b03, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JTextPane having HTMLEditorKit works fine when a new document is opened. However if an already saved document is opened and right/left/center alignment attributes are added we do not get the changed document with JtextPane.getText method. UI does show that text is aligned as per the action but getText returns the old document. If we open a new document it works fine.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Execute this programm below:
      2. In the text pane type "Hello" on first line and "there" on second line
      3. right align the second line
      4. save the content to a file
      5. exit the programm
      6. again launch the programm and open the saved file.
      7. You will notice that contents are correct
      8. now center align the second line
      9. save the file
      10. You will notice that content is not saved however UI does show that alignment is applied. This can be seen on console as well when you click "SaveFile" button.
      Some time it happens the other way round, UI does not show the change but getText returns correct text with proper alignment.





      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      this behaviour is not expected
      ACTUAL -
      when content is saved to a file using getText and then same content is opened in TextPane and edited we should be able to set the alignment.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      <pre>
      package editorpane;

      import java.awt.GridLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.FileWriter;
      import java.io.IOException;

      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JFrame;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      import javax.swing.JTextPane;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.Document;
      import javax.swing.text.SimpleAttributeSet;
      import javax.swing.text.StyleConstants;
      import javax.swing.text.StyledDocument;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;

      public class TestAlignment extends JFrame
      {
        private JTextPane myTextPane;
        private SimpleAttributeSet sas;
        private StyledDocument theDocument;
        private File currentFile;

        public TestAlignment()
        {
          super();

          setSize( 500, 500);
          getContentPane().setLayout( new GridLayout( 1, 2));

          myTextPane = new JTextPane();
          myTextPane.setEditorKit(new HTMLEditorKit());
          myTextPane.setSize( 250, 250);
          getContentPane().add( myTextPane);
          myTextPane.setText( "<html>\n<head>\n\n</head>\n<body>\n<p style=\"margin-top: 0\">Hello</p>\n</body>\n</html>");

          sas = new SimpleAttributeSet();

          JButton leftAlignButton = new JButton( "left");
          JButton rightAlignButton = new JButton( "right");
          JButton centreButton = new JButton( "centered");
          JButton saveAction = new JButton( "SaveFile");
          JButton openAction = new JButton ("OpenFile");
          JPanel buttonPanel = new JPanel();

          leftAlignButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e)
            {
              StyleConstants.setAlignment( sas, StyleConstants.ALIGN_LEFT);
              myTextPane.setParagraphAttributes( sas, false);

              myTextPane.repaint();
            }
          });

          rightAlignButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e)
            {
              StyleConstants.setAlignment( sas, StyleConstants.ALIGN_RIGHT);
              myTextPane.setParagraphAttributes( sas, false);

              myTextPane.repaint();
            }
          });

          centreButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e)
            {
              StyleConstants.setAlignment( sas, StyleConstants.ALIGN_CENTER);
              myTextPane.setParagraphAttributes( sas, false);

              myTextPane.repaint(); }
          });

          saveAction.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e)
            {
              System.out.println(myTextPane.getText());
              if (currentFile != null){
                  try{
                      FileWriter fw = new FileWriter(currentFile);
                      fw.write(myTextPane.getText());
                      fw.close();
                  }catch(FileNotFoundException fnfe){
                      System.err.println("FileNotFoundException: " + fnfe.getMessage());
                  }catch(IOException ioe){
                      System.err.println("IOException: " + ioe.getMessage());
                  }
              }else{
                  saveDocumentAs();
              }
            }
          });
          
          openAction.addActionListener( new ActionListener() {
              public void actionPerformed( ActionEvent e)
              {
                  openDocument();
              }
              });
          buttonPanel.add( leftAlignButton);
          buttonPanel.add( centreButton);
          buttonPanel.add( rightAlignButton);
          buttonPanel.add( saveAction);
          buttonPanel.add ( openAction);

          getContentPane().add( buttonPanel);
        }


        public void saveDocumentAs(){
            try{
                File current = new File(".");
                JFileChooser chooser = new JFileChooser(current);
                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                chooser.setFileFilter(new HTMLFileFilter());
                int approval = chooser.showSaveDialog(this);
                if (approval == JFileChooser.APPROVE_OPTION){
                    File newFile = chooser.getSelectedFile();
                    if (newFile.exists()){
                        String message = newFile.getAbsolutePath()
                            + " already exists. \n"
                            + "Do you want to replace it?";
                        if (JOptionPane.showConfirmDialog(this, message) == JOptionPane.YES_OPTION){
                            currentFile = newFile;
                            setTitle(currentFile.getName());
                            FileWriter fw = new FileWriter(currentFile);
                            fw.write(myTextPane.getText());
                            fw.close();
                            
                        }
                    }else{
                        currentFile = new File(newFile.getAbsolutePath());
                        setTitle(currentFile.getName());
                        FileWriter fw = new FileWriter(currentFile);
                        fw.write(myTextPane.getText());
                        fw.close();
                                    
                    }
                }
            }catch(FileNotFoundException fnfe){
                System.err.println("FileNotFoundException: " + fnfe.getMessage());
            }catch(IOException ioe){
                System.err.println("IOException: " + ioe.getMessage());
            }
        }
        public void openDocument(){
            try{
                File current = new File(".");
                JFileChooser chooser = new JFileChooser(current);
                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                chooser.setFileFilter(new HTMLFileFilter());
                int approval = chooser.showSaveDialog(this);
                if (approval == JFileChooser.APPROVE_OPTION){
                    currentFile = chooser.getSelectedFile();
                    setTitle(currentFile.getName());
                    FileReader fr = new FileReader(currentFile);
                    Document oldDoc = myTextPane.getDocument();
                  
                    HTMLEditorKit editorKit = new HTMLEditorKit();
                    HTMLDocument document = (HTMLDocument)editorKit.createDefaultDocument();
                    editorKit.read(fr,document,0);
                    myTextPane.setDocument(document);
                  
                }
            }catch(BadLocationException ble){
                System.err.println("BadLocationException: " + ble.getMessage());
            }catch(FileNotFoundException fnfe){
                System.err.println("FileNotFoundException: " + fnfe.getMessage());
            }catch(IOException ioe){
                System.err.println("IOException: " + ioe.getMessage());
            }

        }
        class HTMLFileFilter extends javax.swing.filechooser.FileFilter{
            
            public boolean accept(File f){
                return ((f.isDirectory()) ||(f.getName().toLowerCase().indexOf(".htm") > 0));
            }
            
            public String getDescription(){
                return "html";
            }
        }
        public static void main( String args[])
        {
          TestAlignment myFrame = new TestAlignment();
          myFrame.setVisible(true);
        }
      }
      </pre>
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround
      Using JDK7b04 loaded copy is shown correct alignment to the right.
      However, subsequent update is not saved properly (i.e. steps 8-10 are still reproducible).

            peterz Peter Zhelezniakov
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: