Monday 2 September 2013

Demonstration of jlist


/**
 * @(#)Change_Bg.java
 *
 *
 * @author    - Anant Mahale
 * @version 1.00 2013/8/2
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ChangBackgroundWithList
{
    public static void main(String args[])
    {
        myframe f=new myframe();
        f.setTitle("Jlist program");
        f.setSize(500,500);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ListSelectionListener
    {
        JList lst;
        String[] lstColorNamess={ "black", "blue", "green", "yellow","white" };
        mypanel()
        {
           
            lst=new JList(lstColorNamess);
            lst.addListSelectionListener(this);
            add(lst);
        }
        public void valueChanged(ListSelectionEvent lse)
        {
            int i=lst.getSelectedIndex();
            if(i==0)
                setBackground(Color.black);   
                   
            if(i==1)
                setBackground(Color.blue);
               
            if(i==2)
                setBackground(Color.green);   
                   
            if(i==3)
                setBackground(Color.yellow);
               
            if(i==4)
                setBackground(Color.white);       
        }
    }
}

No comments: