List in Java AWT


The List represents a list of text items. The list can be configured that user can choose either one item or multiple items.

Constructor Used for
List() new scrolling list.
List(int row_num) new scrolling list initialized with the given number of rows visible.
List(int row_num, Boolean multipleMode) new scrolling list initialized which displays the given number of rows.

Source Code

package awtDemo;
 
import java.awt.*;
import java.awt.event.*;
 
//List in Java AWT
class MyApp extends Frame{
 
	List lst;
	Button btn;
	Label lbl;
 
	public MyApp() {
		super("Tutor Joes");
		setSize(1000, 600);// w,h
		setLayout(null);
		setVisible(true);
		lst = new List(4, false);
		lst.setBounds(10, 50, 100, 100);
		lst.add("Mercury");
		lst.add("Venus");
		lst.add("Earth");
		lst.add("Mars");
		lst.add("Jupiter");
		lst.add("Saturn");
		lst.add("Uranus");
		lst.add("Neptune");
		lst.add("Pluto");
 
		btn = new Button("Show Details");
		btn.setBounds(10, 170, 100, 30);
 
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
 
				String list[] = lst.getSelectedItems();
 
				String data = "Selected Planet : ";
				for (String x : list)
					data += x + " , ";
				lbl.setText(data);
			}
		});
 
		lbl = new Label("Empty Label");
		lbl.setBounds(200, 170, 300, 30);
 
 
		add(lst);
		add(btn);
		add(lbl);
		// Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}
 
}
 
public class app {
	public static void main(String[] args) {
		MyApp frm = new MyApp();
	}
 
}
 
To download raw file Click Here

Output

Java AWT


Basic Programs