/* From: Expert's Exchange (www.experts-exchange.com) Question 507 in previously-asked questions section of Java area Answered my Marc Limotte (mlimotte@bdsinc.com) Employed by: Business Data Systems, Inc. (www.bdsinc.com) Went to school at Rutgers University (limotte@gandalf.rutgers.edu) Title: "replace a item from a Choice" Value: 5 Points Question: I want to replace or delete a item from a Choice but there is no method like in the List class ( delItem(int pos)). Is there a posibility to do this? Answer from mlimotte... If you are writing an application (as oppsed to an applet), you can use the JDK 1.1. It has the required methods, as zone pointed out. You should be able to use it from within MS J++, but I would consider the details on how to do that another question... one which I don't know the answer to. If you need an applet (to run in a browser), the only browser which currently supports java 1.1 is Sun's HotJava. NS and IE support should be available soon. As you noticed, there is no support for doing this in Java 1.0, I've used the following (admitedly klugy) solution, but it's slow: Essentially, I've put a Choice on a panel by itself, and I recreate the choice whenever I want to clear() it. You could add a delItem method by saving the current list of items in an array, and then reloading the choice after the clear() method. Marc */ import java.awt.*; public class ChoicePanel extends Panel { Choice choice; int width = 0, height = 0; public ChoicePanel(int width, int height) { this(); this.width = width; this.height = height; } public ChoicePanel() { super(); setLayout(new BorderLayout()); makeChoice(); } private void makeChoice() { choice = new Choice(); // appletviewer automatically resizes Choice widget to contents // Netscape Java does not; assumes size of (0, 0) // next line added to fix this -- gcw 8/25/1997 choice.resize(width, height); choice.setBackground(Color.white); add("Center", choice); } // marc's original version returned a string public String marcsClear() { String result; choice.hide(); result = choice.getSelectedItem(); makeChoice(); return result; } // return the index of the selected item -- gcw 8/24/1997 public int clear() { int result; choice.hide(); result = choice.getSelectedIndex(); makeChoice(); return result; } public void addItem(String item) { choice.addItem(item); } public int countItems() { return choice.countItems(); } public String getItem(int index) { return choice.getItem(index); } public int getSelectedIndex() { return choice.getSelectedIndex(); } public String getSelectedItem() { return choice.getSelectedItem(); } public void select(int index) { choice.select(index); } public void select(String str) { choice.select(str); } public Dimension preferredSize() { return new Dimension(width, height); } public Dimension minimumSize() { return preferredSize(); } } // end of file