Here is the topic, Java Program to Demo JList, JscrollPane.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /* <APPLET CODE="pandu116" WIDTH="500" HEIGHT="400"> </APPLET> */ public class Pandu116 extends JApplet implements ListSelectionListener { JLabel l1; JList<String> j1; JScrollPane jsp; String country[]=("INDIA" , "USA" , "GERMANY" , "FRANCE" , "ITALY" , "PAKISATN" , "CHINA" , "RUSSIA"); String str; public void init() { str = "Choose The Country:"; Container c = getContentPane(); c.setLayout(new FlowLayout()); l1 = new JLabel(str); j1 = new JList<String>(country); j1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jsp = new JScrollPane(j1); jsp.setPreferredSize(new Dimension(150, 100)); j1.addListSelectionListener(this); c.add(jsp); c.add(l1); } public void valueChanged(ListSelectionEvent lse) { int idx = j1.getSelectedIndex(); if(idx != -1) l1.setText("Current Selection: " + country[idx]); else l1.setText("Choose The Country: "); } } |
Comment below if you have queries related to the above topic, Java Program to Demo JList, JscrollPane.