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

Italic attributes rather than bold using some fonts with JTextPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • client-libs
    • 2d
    • merlin
    • x86
    • windows_nt



      Name: sl110371 Date: 07/05/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      When paragraph attributes are to text in a JTextPane which has bold attributes,
      the 'bold' characters appear as italic with some fonts. The following
      application can be used to demonstrate the bug/feature. Each click of the add
      text button adds some text with/without attributes and finally an end of
      paragraph at which point the paragraph attributes are applied. Depending on the
      font the resulting text shows italic and bold or just italic even when the font
      family has appropriate definition files. There is a slight delay when
      applying theparagraph attributes when a font is used for the for the first time.

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.text.*;

      public class jtpbug {

      public static void main(String[] args) {
          JFrame f = new JFrame();
          JTextPane pane = new JTextPane();
          f.getContentPane().setLayout(new GridLayout(2,1));
          f.getContentPane().add(pane);
          JButton button = new JButton("Add Text");
          AddText at;
          button.addActionListener(at = new AddText(pane));
          f.getContentPane().add(button);
          f.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });
          f.setSize(400, 200);
          f.setVisible(true);
          }
      }

      class AddText implements ActionListener {
          private JTextPane pane;
          private int count = 0;
          private int scount = 0;
          private StyleContext sc = new StyleContext();
          private DefaultStyledDocument doc;
          // This array may be set to any number of valid fontfamily names.
          static final String[] fontfamily = {"Arial",
                                              "Bookman Old Style",
                                              "Times New Roman"};

          public AddText(JTextPane p) {
              pane = p;

              for (int i = 0; i < fontfamily.length;i++) {
                  Style s = sc.addStyle(fontfamily[i],null);
                  StyleConstants.setFontFamily(s,fontfamily[i]);
                  StyleConstants.setFontSize(s,14);
              }

              Style si = sc.addStyle("Italic",null);
              StyleConstants.setItalic(si,true);

              Style sb = sc.addStyle("Bold",null);
              StyleConstants.setBold(sb,true);

              doc = new DefaultStyledDocument(sc);
              pane.setDocument(doc);
          }

          public void actionPerformed(ActionEvent ae) {
              String s = null;
              switch (count%6) {
                  case 0:
                      pane.setText("");
                      s = "Here are words ";
                      pane.setCharacterAttributes(SimpleAttributeSet.EMPTY,true);
                      pane.replaceSelection(s);
                      break;
                  case 1:
                      s = "in italics";
                      pane.setCharacterAttributes(doc.getStyle("Italic"),true);
                      pane.replaceSelection(s);
                      break;
                  case 2:
                      s = " and here are words ";
                      pane.setCharacterAttributes(SimpleAttributeSet.EMPTY,true);
                      pane.replaceSelection(s);
                      break;
                  case 3:
                      s = "in bold";
                      pane.setCharacterAttributes(doc.getStyle("Bold"),true);
                      pane.replaceSelection(s);
                      break;
                  case 4:
                      s = " and these are in neither.";
                      pane.setCharacterAttributes(SimpleAttributeSet.EMPTY,true);
                      pane.replaceSelection(s);
                      break;
                  case 5:
                      s = "\n";
                      pane.setParagraphAttributes(SimpleAttributeSet.EMPTY,true);
                      String st = fontfamily[scount];
                      scount = (scount + 1)%fontfamily.length;
                      s = " [" + st + "]" + s;
                      pane.setParagraphAttributes(doc.getStyle(st),false);
                      pane.replaceSelection(s);
                      pane.setLogicalStyle(doc.getStyle("Serif"));
                      break;
             }
             count++;
          }
      }
      (Review ID: 106842)
      ======================================================================

            pkejriwasunw Parry Kejriwal (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: