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

JTextArea.insert() does not behave as expected with invalid position

XMLWordPrintable

    • 5.0
    • b124
    • generic
    • generic

      In the following testcase, it is trying to insert "abc" at position 11 in "1234567890".

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

      public class JTextAreaInsert extends JFrame implements ActionListener {
        final static String s = "1234567890";
        JTextArea ta;
        JTextField tf1, tf2;
        JButton b1, b2;
        JTextAreaInsert() {
          Container c = getContentPane();
          ta = new JTextArea(s, 4, 30);
          tf1 = new JTextField("abc",10);
          tf2 = new JTextField("11",5);
          b1 = new JButton("Action");
          b2 = new JButton("Reset");
          JPanel p = new JPanel();
          p.setLayout(new FlowLayout());
          p.add(tf1);
          p.add(tf2);
          p.add(b1);
          p.add(b2);
          b1.addActionListener(this);
          b2.addActionListener(this);
          c.add(ta, BorderLayout.CENTER);
          c.add(p, BorderLayout.SOUTH);
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          pack();
          setVisible(true);
        }
        public void actionPerformed(ActionEvent ae) {
          if (ae.getSource().equals(b1)) {
            String text = tf1.getText();
            System.out.println(ta.getDocument());
            System.out.println("["+ta.getText()+"]");
            int pos = Integer.parseInt(tf2.getText());
            ta.insert(text, pos);
          } else if (ae.getSource().equals(b2)) {
            ta.setText(s);
          }
        }
        public static void main(String[] args) throws Exception {
          new JTextAreaInsert();
        }
      }
      ======================================================
      Steps to recreate:
      1. Compile and run above test program
      2. Press "Action" button
      Output is
      ---------------
      123456789
      ab
      ---------------
      3. Press "Reset" button, then press "Action" button
      ---------------
      123456789cab
      ---------------
       
      I tested with openjdk 6 and 8, they have the same results.

      Actually, the position 11 in the testcase is invalid as the length of "1234567890" is 10. But it still can insert "abc" without any exceptions. If changing 11 to 12. Then we can see the following exception occurs. So the expected behavior when 11 is set is to throw java.lang.IllegalArgumentException as well.

         Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid insert
              at javax.swing.JTextArea.insert(JTextArea.java:449)
              at JTextAreaInsert.actionPerformed(JTextAreaInsert.java:38)
              at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
              at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
              at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

            ssadetsky Semyon Sadetsky (Inactive)
            youdwei Deven You (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: