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

setText() bug in JTextPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.1.6
    • 1.1.4
    • client-libs
    • 1.1.6
    • x86
    • windows_nt
    • Not verified



        Name: joT67522 Date: 11/13/97


        sending setText(null) or setText("") to a JTextPane causes it to break. Without this, there is no way to clear out the text.


        Here's a sample application that reproduces this bug. It's a simple
        window with a JTextPane, an JTextField and a couple of buttons. The
        clear button sets the JTextPane's contents to the contents of the
        JTextField. If this field is empty (i.e. the text value is "") the
        exception I mentioned comes up.

        BTW - this is with Swing 0.5
         
        - -- J


        // Start source -----

        import java.awt.*;
        import java.awt.event.*;
        import com.sun.java.swing.*;
        import com.sun.java.swing.text.*;
        import com.sun.java.swing.border.BevelBorder;

        /**
         * This class serves as a test for the styled text pane (JTextPane).
         *
         * Currently, (0.5.1) this is very buggy.
         *
         */
        public class TextTest extends JFrame {

        static public void main(String[] args) {

                new TextTest();

        }

        public TextTest() {

                setTitle("Text Pane Test");
                getContentPane().add(new TextTestPane());
                setBackground(SystemColor.control);

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

                pack();
                show();

        }

        }

        class TextTestPane extends JPanel {

        private JTextPane text;
        private DefaultStyledDocument document;
        private StyleContext styles;
        private JButton makeRed;
        private JButton dumpButton;
        private JButton clearButton;
        private JTextField textField;

        public TextTestPane() {

                setupComponents();
                setupEvents();

        }

        private void setupComponents() {


                setLayout(new BorderLayout());

                document = new DefaultStyledDocument(styles = new
        StyleContext());
                JScrollPane sp = new JScrollPane();
                sp.getViewport().add(text = new JTextPane(document));
                text.setBorder(new BevelBorder(BevelBorder.LOWERED));
                add("Center", sp);

                JPanel buttons = new JPanel(new FlowLayout());

                add("South", buttons);
                buttons.add(textField = new JTextField("Sample"));
                buttons.add(makeRed = new JButton("Make Red"));
                //makeRed.setBackground(SystemColor.control);
                buttons.add(dumpButton = new JButton("Dump"));
                dumpButton.setBackground(SystemColor.control);
                buttons.add(clearButton = new JButton("Clear"));
                clearButton.setBackground(SystemColor.control);
                createStyles();

        }

        private void createStyles() {

                Style s = styles.addStyle("none", null);
                s = styles.addStyle("red", null);
                StyleConstants.setForeground(s, Color.red);

        }

        private void setupEvents() {

                clearButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                text.setText(textField.getText());
                        }
                });

                makeRed.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                makeRed();
                        }
                });

                dumpButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                dumpDocument();
                        }
                });

        }

        public void dumpDocument() {

                document.dump(System.out);

        }

        public void makeRed() {

                document.setCharacterAttributes(2, 5, styles.getStyle("red"),
        true);
                text.setDocument(text.getDocument());
                //document.setLogicalStyle(1, styles.getStyle("red"));

        }


        }



        ---------

        Exception occurred during event dispatching:
        java.util.NoSuchElementException
                at java.util.Vector.firstElement(Vector.java:339)
                at com.sun.java.swing.text.DefaultStyledDocument.insertUpdate(DefaultStyledDocument.java:363
        )
                at com.sun.java.swing.text.AbstractDocument.insertString(AbstractDocument.java:234)
                at com.sun.java.swing.text.JTextComponent.setText(JTextComponent.java:538)
                at TextTestPane$1.actionPerformed(TextTest.java:94)
                at com.sun.java.swing.AbstractButton.fireActionPerformed(AbstractButton.java:759)
                at com.sun.java.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java

        -------------------------


        1) Type some text in the JTextPane
        2) Clear out the JTextField (i.e. erase the word "Sample")
        3) Hit Clear

        You should get the exception.

        This same exception comes up every time setText("") is sent to a
        JTextPane.

        Feel free to call me at 212.902.3422 if you want more details.

        - -- J

        (Review ID: 19695)
        ======================================================================

              tprinzing Tim Prinzing (Inactive)
              johsunw Joon Oh (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: