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

Font size are incompatable b/w StyledEditorKit.FontSizeAction and JEditorPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.2, 1.3.0
    • client-libs
    • beta
    • generic, x86
    • generic, windows_nt



      Name: skT88420 Date: 08/06/99


      The StyledEditorKit.FontSizeAction's are generating invalid
      font tags or the JEditorPane is interpreting the font tag
      <font size="8"> incorrectly.

      Using the program below, follow these steps to see the effect
      of the bug.

      ---------------------------begin--------------------------------

      import java.awt.*;
      import java.awt.event.*;
      import java.net.*;
      import java.io.*;

      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;


      public class HtmlDemo extends JPanel {

          JEditorPane theEditor;
          JTextArea htmlTextArea, dumpMarkup;
          JToolBar fontBar;

        public HtmlDemo() {
          String initialText = "<html> <font size=0>Font Size 0</font><br> <font size=1>Font Size 1</font><br> <font size=2>Font Size 2</font><br> <font size=3>Font Size 3</font><br> <font size=4>Font Size 4</font><br>";

              htmlTextArea = new JTextArea(10, 20);
              htmlTextArea.setText(initialText);
              JScrollPane scrollPane = new JScrollPane(htmlTextArea);

              JButton changeTheLabel = new JButton("Change the label");
              changeTheLabel.setMnemonic(KeyEvent.VK_C);
              changeTheLabel.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      try {
                          theEditor.setText(htmlTextArea.getText());
                      } catch (Throwable exc) {
                          JOptionPane.showMessageDialog(
                                  HtmlDemo.this,
                                  "The HTML you specified was invalid.");
                      }
                  }
              });
              changeTheLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

              theEditor = new JEditorPane("text/html", initialText) {
                  public Dimension getPreferredSize() {
                      return new Dimension(200, 200);
                  }
                  public Dimension getMinimumSize() {
                      return new Dimension(200, 200);
                  }
                  public Dimension getMaximumSize() {
                      return new Dimension(200, 200);
                  }
              };

              final HTMLEditorKit kit = (HTMLEditorKit) theEditor.getEditorKit();
              final HTMLDocument doc = (HTMLDocument) theEditor.getDocument();

              fontBar = new JToolBar();
              fontBar.add(new StyledEditorKit.FontSizeAction(" 8 ", 8));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 10 ", 10));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 12 ", 12));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 14 ", 14));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 18 ", 18));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 24 ", 24));
              fontBar.add(new StyledEditorKit.FontSizeAction(" 36 ", 36));

              dumpMarkup = new JTextArea(10, 20);
              JScrollPane dumpScrollPane = new JScrollPane(dumpMarkup);


              JButton dumpLabel = new JButton("Dump the Markup");
              dumpLabel.setMnemonic(KeyEvent.VK_D);
              dumpLabel.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      try {
                        Writer w = new StringWriter();
                        kit.write(w, doc, 0, doc.getLength());
                        w.close();
                        dumpMarkup.setText(w.toString());
                      } catch (Throwable exc) {
                          JOptionPane.showMessageDialog(
                                  HtmlDemo.this,
                                  "Error dumping content.");
                      }
                  }
              });
              changeTheLabel.setAlignmentX(Component.CENTER_ALIGNMENT);


              JPanel leftPanel = new JPanel();
              leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
              leftPanel.setBorder(BorderFactory.createCompoundBorder(
                      BorderFactory.createTitledBorder(
                          "Edit the HTML, then click the button"),
                      BorderFactory.createEmptyBorder(10,10,10,10)));
              leftPanel.add(scrollPane);
              leftPanel.add(Box.createRigidArea(new Dimension(0,10)));
              leftPanel.add(changeTheLabel);

              JPanel rightPanel = new JPanel();
              rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
              rightPanel.setBorder(BorderFactory.createCompoundBorder(
                              BorderFactory.createTitledBorder("A label with HTML"),
                              BorderFactory.createEmptyBorder(10,10,10,10)));
              rightPanel.add(theEditor);
              rightPanel.add(Box.createRigidArea(new Dimension(0,10)));
              rightPanel.add(fontBar);


              JPanel dumpPanel = new JPanel();
              dumpPanel.setLayout(new BoxLayout(dumpPanel, BoxLayout.Y_AXIS));
              dumpPanel.setBorder(BorderFactory.createCompoundBorder(
                              BorderFactory.createTitledBorder("The markup as interpreted by the JEditorPane"),
                              BorderFactory.createEmptyBorder(10,10,10,10)));

              dumpPanel.add(dumpScrollPane);
              dumpPanel.add(Box.createRigidArea(new Dimension(0,10)));
              dumpPanel.add(dumpLabel);


              setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
              setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
              add(leftPanel);
              add(Box.createRigidArea(new Dimension(10,0)));
              add(rightPanel);
              add(Box.createRigidArea(new Dimension(10,0)));
              add(dumpPanel);
          }

          public static void main(String args[]) {
              JFrame f = new JFrame("HtmlDemo");

              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });

              f.getContentPane().add(new HtmlDemo());
              f.pack();
              f.setVisible(true);
          }
      }

      ----------------------------end---------------------------------

      In the left window is some html text which uses relative fonts,
      <font size=0> <font size=1> etc

      The center window is the JEditorPane's rendering of this html.

      The right window displays the html markup as interpreted by the
      JEditorPane

      1. click the "Dump" button, note that the html generated is as
      displayed in the left window with <font size=0> etc.

      2. Highlight "Font Size 0", the smallest text in the center
      window. Click on the "18" button.

      3. Click on the "Dump" button, note that the html generated
      displays the tag <font size="18">

      4. Modify the text "<font size="18">Font Size 0</font>" from
      the
      left window. Click "Change the label" button.

      Expected Results: The text "Font Size 0" should be equal in
      size to "Font Size 4".
       But it is much bigger.

      Results: Font size are incompatable between
      StyledEditorKit.FontSizeAction and JEditorPane
      (Review ID: 93597)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: