Adding and Deleting items from AWT list in Java
This article is going to present how to add and delete items in AWT list used in Java.
Adding items in AWT list
- import java.applet.Applet;
- import java.awt.List;
- import java.awt.Color;
-
- public class Demo extends Applet
- {
-
- public void init()
- {
-
- List l = new List(8, true);
-
-
- l.add("Desktop");
- l.add("Laptop");
- l.add("Tablet");
- l.add("Phone");
- l.add("Kindle");
- l.add("Screen");
- l.add("System");
-
- add(l);
-
- setBackground(Color.blue);
- }
- }
We can see in the above applet image all the items added in the AWT list with blue background color.
Deleting items for AWT list
By adding a line of code to remove or delete the items in the above code example.
- add(l);
-
- l.remove(3);
-
- setBackground(Color.blue);
We can see that the 3rd positioned item (according to list) “Phone” has been removed or deleted from the list. We can also delete multiple items in a single time by defining item’s position.
Note: Items can also be deleted by defining item’s name as given below.
- l.remove("Desktop”);
- l.remove("Laptop");
- l.remove("Tablet");
We can see that the 3 items have been removed at once. All the items can be removed at once by using the following syntax.
Thank you keep learning and sharing.