2023 09 14 AWT List
2023 09 14 AWT List
List Class
The object of List class represents a list of text items.
With the help of the List class, user can choose either one item or multiple items.
Programs
C:\Users\HP\ListExample1.java
1 import java.awt.*;
2
3 public class ListExample1 {
4
5
6 ListExample1()
7 {
8 Frame f = new Frame();
9 List l1= new List();
10 l1.setBounds(50,50,300,50);
11 l1.add("AJP");
12 l1.add("HTML");
13 l1.add("Python");
14 l1.add("Machine Learbing");
15 l1.add("CSS");
16 l1.add("CPP");
17
18
19 f.add(l1);
20
21 f.setSize(600,600);
22 f.setLayout(null);
23 f.setVisible(true);
24 }
25
26 public static void main(String args[]) {
27 ListExample1 obj=new ListExample1();
28
29 }
30 }
31
Output :
‘
C:\Users\HP\ListWithActionListener.java
1 import java.awt.*;
2 import java.awt.event.*;
3
4
5 public class ListWithActionListener {
6
7
8 ListWithActionListener()
9 {
10 Frame f = new Frame();
11 List l1= new List();
12 l1.setBounds(50,50,300,50);
13 l1.add("AJP");
14 l1.add("HTML");
15 l1.add("Python");
16 l1.add("Machine Learning");
17 l1.add("CSS");
18 l1.add("CPP");
19
20 Label l=new Label("Selected Item : ");
21 l.setBounds(100,200,200,50);
22
23 Button b=new Button("Select Item");
24 b.setBounds(100,150,200,50);
25
26
27 f.add(l1);
28 f.add(l);
29 f.add(b);
30
31 b.addActionListener(new ActionListener(){
32 public void actionPerformed(ActionEvent e )
33 {
34 String s= l1.getItem(l1.getSelectedIndex());
35 // System.out.println(s);
36 l.setText("Item Selected : "+s);
37 }
38
39 });
40
41
42 // System.out.println(l1.getItemCount());
43 // l1.remove(1);
44 // System.out.println(l1.getItemCount());
45 //
46 // l1.removeAll();
47 // System.out.println(l1.getItemCount());
48
49 f.setSize(600,600);
50 f.setLayout(null);
51 f.setVisible(true);
52 }
53
54 public static void main(String args[]) {
55 ListWithActionListener obj=new ListWithActionListener();
56
57 }
58 }
59
Output:
15/09/2023
Output :