Showing posts with label change background of frame. Show all posts
Showing posts with label change background of frame. Show all posts

Monday, 17 June 2013

Program in java for JComboBox in swing


//WAP to check the combobox

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class combo extends JFrame implements ItemListener
{
    JComboBox cb;
    Container c;

    combo()
    {
        c=getContentPane();
        c.setLayout(null);

        cb=new JComboBox();
        cb.addItem("Red");
        cb.addItem("Blue");
        cb.addItem("Pink");
        cb.setBounds(50,50,100,30);
        cb.addItemListener(this);
        c.add(cb);
    }
    public void itemStateChanged(ItemEvent ie)
    {
        String str=(String)cb.getSelectedItem();

        if(str=="Red")
        {
            c.setBackground(Color.red);
        }
        if(str=="Blue")
        {
            c.setBackground(Color.blue);
        }

        if(str=="Pink")
        {
            c.setBackground(Color.pink);

        }
    }
    public static void main(String args[])
    {
        combo f=new combo();
        f.setTitle("demo");
        f.setSize(500,500);
        f.setVisible(true);
    }
}