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

Choice.removeAll() followed by Choice.addItem dumps core in some situtations

    XMLWordPrintable

Details

    • 1.1.5
    • sparc
    • generic
    • Verified

    Backports

      Description

        Create a Choice component with 20 items. Use Choice.getItem(int) to get each item and store it in a Vector. Then use Choice.removeAll() to remove all items from the Choice. Then re-add the original 20 items from the Vector, and add one more item via Choice.addItem(). The last addItem() causes a core dump.


        Below is a java application that reproduces the problem. Click on the add item button and the application will core dump.




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

        public class testChoice extends java.applet.Applet implements
        ActionListener,
        ItemListener {
            Frame frame;
            Choice choice;
            Button addItemBtn;


            public static void main(String args[]) {
        Frame f = new Frame("Choice test");
        testChoice clnt = new testChoice();
        clnt.init();
        clnt.start();

        f.add("Center", clnt);
        f.setSize(500, 300);
        f.pack();
        f.show();
            }

            public void init() {


        // text to send and send button
          setLayout(new GridLayout(2,2,1,1));

        add(choice = new Choice());

        for ( int i = 0; i < 20; i++ )
        choice.addItem("test");

        add(new Label(""));

        choice.addItemListener(this);

        add(new Label(""));
        addItemBtn = new Button("Add Item");
        add(addItemBtn);
        addItemBtn.addActionListener(this);
            }
            
            public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == choice) {
        } else if (source == addItemBtn) {
        Vector v = new Vector();

        int numItems = choice.countItems();

        for ( int i = 0; i < numItems; i++ )
        v.addElement(choice.getItem(i));

        choice.removeAll();

        for ( int i = 0; i < numItems; i++ )
        {
        String s = (String)v.elementAt(i);

        choice.addItem(s);
        }

        choice.addItem("New Item");
        }
            }

            public void itemStateChanged(ItemEvent e) {
            }

            private Frame getFrame() {
        if (frame == null) {
        Container parent = (Container)this;

                    while(parent != null && ! (parent instanceof Frame)) {
                        parent = (Container)parent.getParent();
        }

                    frame = (Frame) parent;
        }

        return (frame);
            }
        }


        Attachments

          Issue Links

            Activity

              People

                xdengsunw Xianfa Deng (Inactive)
                duke J. Duke
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: