0% found this document useful (0 votes)
11 views7 pages

2023 09 14 AWT List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

2023 09 14 AWT List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

14/Sept/2023

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.

It inherits the Component class.

List Class Constructors :


Sr. Constructor Description
no.
1. List() It constructs a new scrolling list.
2. List(int row_num) It constructs a new scrolling list
initialized with the given number of
rows visible.
3. List(int row_num, Boolean It constructs a new scrolling list
multipleMode) initialized which displays the given
number of rows.

List Class Methods


Sr. Method name Description
no.
1. void add(String item) It adds the specified item into the end of scrolling list.
2. void add(String item, int index) It adds the specified item into list at the given index
position.
3. void It adds the specified action listener to receive action
addActionListener(ActionListener l) events from list.
4. void addItemListener(ItemListener l) It adds specified item listener to receive item events
from list.
5. String getItem(int index) It fetches the item related to given index position.
6. int getItemCount() It gets the count/number of items in the list.
7. String[] getItems() It fetched the items from the list.
8. int getRows() It fetches the count of visible rows in the list.
9. int getSelectedIndex() It fetches the index of selected item of list.
10. int[] getSelectedIndexes() It gets the selected indices of the list.
21. String getSelectedItem() It gets the selected item on the list.
12. String[] getSelectedItems() It gets the selected items on the list.
13. void remove(int position) It removes the item at given index position from the
list.
14. void remove(String item) It removes the first occurrence of an item from list.
15. void removeAll() It removes all the items from the list.
16. void replaceItem(String newVal, int It replaces the item at the given index in list with the
index) new string specified.
17. void select(int index) It selects the item at given index in the list.
18. void setMultipleMode(boolean b) It sets the flag which determines whether the list will
allow multiple selection or not.

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

F:\Advanced Java Training Aug2023\Programs\2023-09-


15\ListItemListener.java
1 // List with Item Listener Date 15-Sept-2023
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6
7 public class ListItemListener {
8
9 List courselist;
10 Label l;
11 String s="";
12 ListItemListener()
13 {
14 Frame f = new Frame("List with ItemListener ");
15
16 //Label Object Creation
17 l=new Label("Course Selected : ");
18 l.setBounds(100,200,200,50);
19
20
21 // Couse List Object Creation
22 courselist = new List();
23 courselist.setBounds(100,100,200,50);
24 courselist.add("Computer Engineering");
25 courselist.add("Mechanical Engineering");
26 courselist.add("Civil Engineering");
27 courselist.add("Electrical Engineering");
28 courselist.add("Electronics Engineering");
29 courselist.add("Information Technology");
30
31
32 f.add(courselist);
33 f.add(l);
34
35 courselist.addItemListener(new ItemListener(){
36 public void itemStateChanged(ItemEvent e)
37 {
38 s=courselist.getItem(courselist.getSelectedIndex());
39 //System.out.println(s);
40 l.setText(s);
41
42 }
43 });
44
45 f.setSize(400,400);
46 f.setLayout(null);
47 f.setVisible(true);
48 }
49
50
51
52 public static void main(String args[]) {
53
54 ListItemListener obj=new ListItemListener();
55
56 }
57 }
58

Output :

You might also like