Saturday, November 20, 2010

Program to demonstrate the concept of ListBox and ComboBox

/* Program : 11
   Program to demonstrate the concept of ListBox and ComboBox
   Development Status : Completed and Tested
   Developed by : Malhar Vora
   Developed on : 20-11-2010
   Email        : vbmade2000@gmail.com
   WebSite  : www.malhar2010.blogspot.com
 *****************************************************************/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class P11 extends JFrame implements ListSelectionListener
{
 
 JList lstCountry = null;
 JComboBox cboCity = null;
 JScrollPane listPane=null;
 String[] strCountry={ "India","USA","UK"};

 String[] strIndia={"Bombay","Ahmedabad"};
 String[] strUsa={"Chikago","Washington DC"};
 String[] strUk={"London","Manchester"};
    
 DefaultListModel model = null;
 DefaultComboBoxModel modelIndia = null;
 DefaultComboBoxModel modelUsa = null;
 DefaultComboBoxModel modelUk = null;
 
 P11()
 {
  
  
  lstCountry = new JList();
  cboCity  = new JComboBox();
  listPane = new JScrollPane(lstCountry);
  model = new DefaultListModel();
  
  modelIndia = new DefaultComboBoxModel(strIndia);
  modelUsa = new DefaultComboBoxModel(strUsa);
  modelUk = new DefaultComboBoxModel(strUk);
  
    
  setTitle("Program 11 - Developed by Malhar Vora");
  setSize(250,300);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
  setLayout(null);
  
  listPane.setBounds(10,50,200,100);
  cboCity.setBounds(10,170,200,30);
    
  this.getContentPane().add(listPane);
  this.getContentPane().add(cboCity);
    
  model.addElement("India");
  model.addElement("USA");
  model.addElement("UK");
    
  lstCountry.setModel(model);
  lstCountry.setSelectionMode(0);
  lstCountry.addListSelectionListener(this);
  
     
 }
 
 public void valueChanged(ListSelectionEvent e)
 {
  String selectedCountry = (String)lstCountry.getSelectedValue();
  
  if(selectedCountry.equals("India"))
  {
   cboCity.setModel(modelIndia);
  }
  else if(selectedCountry.equals("USA"))
  {
   cboCity.setModel(modelUsa);
  }
  else if(selectedCountry.equals("UK"))
  {
   cboCity.setModel(modelUk);
  }
  else
  {
   cboCity.setModel(null);
  }
 }
 
 public static void main(String []str)
 {
  P11 p = new P11();    
 }


}



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.