Forum Stats

  • 3,853,894 Users
  • 2,264,290 Discussions
  • 7,905,498 Comments

Discussions

i m trying to make a jcombobox that has 3 colors red, yellow and green.........its a run time error.

Guru_100
Guru_100 Member Posts: 1
edited Feb 2, 2015 3:48AM in New To Java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Exp4;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.JComboBox;

import javax.swing.JFrame;

/**

*

* @author Gursimran

*/

public class combox extends JFrame implements ItemListener{

   

    public combox()

    {

        JComboBox cb;

       

       

        setLayout(new GridLayout(10,10));

        cb= new JComboBox<>();

        cb.addItem("Red");

        cb.addItem("Green");

        cb.addItem("Blue");

       

        add(cb);

        cb.addItemListener(this);

       

        setVisible(true);

        setSize(400,400);

    }

    public static void main(String[] args)

    {

       

        combox obj=new combox();

       

    }

    @Override

    public void itemStateChanged(ItemEvent e)

    {

        if(e.getItemSelectable().equals(0))

                {

                    this.getContentPane().setBackground(Color.red);

                }

        else if(e.getItemSelectable().equals(1))

                {

                    this.getContentPane().setBackground(Color.green);

                }

           

     }

       

    }

Answers

This discussion has been closed.