0

I am trying to create a fortune teller that lets you pick a color from one JCombobox then lets you pick a number from another JCombobox in accordance to what color you have chosen. For example if I pick "Red" or "Yellow", then this set of #'s are shown - {1,3,4,7}. However if I pick "Blue" or "Green", this set of #'s are shown - {2,6,8,5}. Once a number is selected then I would like to display a fortune from the "fortune array" that is tied to that number on a statusbar(JLabel) at the bottom of the screen. This is my code so far:-

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

public class Example2 extends JFrame implements ItemListener {


    private JComboBox maincombobox;
    private JComboBox subcombobox;
    private JLabel labels;

    public Example2(){
        String[] items = {"Select a Color","Red","Blue","Yellow","Green"};
        maincombobox =  new JComboBox(items);
        maincombobox.addItemListener(this);

        getContentPane().add(maincombobox);

        subcombobox = new JComboBox();
        subcombobox.addItemListener(this);      
        subcombobox.setEnabled(true);
        subcombobox.setPrototypeDisplayValue("XXXXXXXXXXXXX");
        getContentPane().add(subcombobox,BorderLayout.EAST);

        labels =  new JLabel("Default");
        getContentPane().add(labels, BorderLayout.SOUTH);


             }
    public void itemStateChanged(ItemEvent e) {
        String[] subitems1 = {"Choose a number","1","3","4","7"};
        String[] subitems2= {"Choose a number","2","6","8","5"};

        String[] fortune = {"Today is you lucky day", "You will get strange looks from people", "Don't touch your left foot today", 
                "You will forget a crucial thing today", "You will meet a mysterious person", "Will win a million dollars", 
                "Good day in the financial market", "Get a life","Think hard, you will find the answer"};


        if (e.getSource() == maincombobox) {

            if (maincombobox.getSelectedItem().equals("Select a Color")) {
               subcombobox.setEnabled(false);
            }    
            else if (maincombobox.getSelectedItem().equals("Red") ||
                maincombobox.getSelectedItem().equals("Yellow") ){

                subcombobox.setEnabled(true);
                subcombobox.removeAllItems();
                for (int i = 0; i < subitems1.length; i++) {
                    subcombobox.addItem(subitems1[i]);
                }

            }

            else if (maincombobox.getSelectedItem().equals("Blue") ||
                maincombobox.getSelectedItem().equals("Green") ){


                subcombobox.setEnabled(true);
                subcombobox.removeAllItems();
                for (int i = 0; i < subitems2.length; i++) {
                    subcombobox.addItem(subitems2[i]);
                }

            }
        }

    }

    public static void main(String[] args){
        JFrame frame = new Example2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null); 
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
huntingGirl
  • 55
  • 2
  • 8
  • 1
    See also this [Q&A](http://stackoverflow.com/q/3191837/230513). – trashgod Jun 28 '12 at 19:42
  • so if I understand correctly what you want to do is depending on what is selected from the combo boxes you want to set the text of the label to one of the items in the fortune array? – Michael Jun 28 '12 at 19:45
  • What works? What doesn't? What does the program do so far? – Puce Jun 28 '12 at 19:57
  • 1
    You may also find [this thread](http://stackoverflow.com/q/10367834/1048330) useful. – tenorsax Jun 28 '12 at 21:46
  • `getSelectedItem()` returns an `Object`, and you cannot display an `Object` (unless autoboxing takes care of this for you). If the item is automatically autoboxed to a `String` value, nothing needs to be done. If not, then you will need to call the `toString()` method in order for the label to update: `labels.setText(getSelectedItem().toString())` – fireshadow52 Jun 28 '12 at 22:10
  • @Michael: Yes that is what I am trying to do. – huntingGirl Jun 28 '12 at 23:12
  • @Puce: So far, I am able to select a color from the first box, then select a number based on what I picked from the second box. But I do not know how to display a fortune yet. – huntingGirl Jun 28 '12 at 23:12

2 Answers2

0

add the following code:

if(e.getSource()==subcombobox){
    int choice=Integer.parseInt(subcombobox.getSelectedItem());
    //here the selected number gets stored into the int variable choice
    labels.setText(fortune[choice]);
    //here the text for "labels" is set from your fortune string array
}

Change the code according to your liking but this itself is self-explanatory

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
0

i wrote the logic perfectly check this

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

public class Example2 extends JFrame implements ItemListener {

private JComboBox maincombobox;
private JComboBox subcombobox;
private JLabel labels;

public Example2(){
    String[] items = {"Select a Color","Red","Blue","Yellow","Green"};
    maincombobox =  new JComboBox(items);
    maincombobox.addItemListener(this);

    getContentPane().add(maincombobox);

    subcombobox = new JComboBox();
    subcombobox.addItemListener(this);      
    subcombobox.setEnabled(true);
    subcombobox.setPrototypeDisplayValue("XXXXXXXXXXXXX");
    getContentPane().add(subcombobox,BorderLayout.EAST);

    labels =  new JLabel("Default");
    getContentPane().add(labels, BorderLayout.SOUTH);


         }
public void itemStateChanged(ItemEvent e) {
    String[] subitems1 = {"Choose a number","1","3","4","7"};
    String[] subitems2= {"Choose a number","2","6","8","5"};

    String[] fortune = {"Today is you lucky day", "You will get strange looks from people", "Don't touch your left foot today", 
            "You will forget a crucial thing today", "You will meet a mysterious person", "Will win a million dollars", 
            "Good day in the financial market", "Get a life","Think hard, you will find the answer"};

    if (e.getSource() == maincombobox)
    {

        if (maincombobox.getSelectedItem().equals("Select a Color"))
        {
            subcombobox.setEnabled(false);
        }
        else if (maincombobox.getSelectedItem().equals("Red") || maincombobox.getSelectedItem().equals("Yellow"))
        {

            subcombobox.setEnabled(true);
            subcombobox.removeAllItems();
            for (int i = 0; i < subitems1.length; i++)
            {
                subcombobox.addItem(subitems1[i]);

            }

        }

        else if (maincombobox.getSelectedItem().equals("Blue") || maincombobox.getSelectedItem().equals("Green"))
        {

            subcombobox.setEnabled(true);
            subcombobox.removeAllItems();
            for (int i = 0; i < subitems2.length; i++)
            {
                subcombobox.addItem(subitems2[i]);

            }
        }

    }


    else if (subcombobox.getItemCount()>0)
    {
        for(int i=0;i<8;i++)
        {
                if (subcombobox.getSelectedItem().equals(Integer.toString(i)))
                {
                    labels.setText(fortune[i]);
                }

        }
    }

}

public static void main(String[] args){
    JFrame frame = new Example2();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null); 
}

}

I just added the code at the bottom is

else if (subcombobox.getItemCount()>0)
{
    for(int i=0;i<8;i++)
    {
            if (subcombobox.getSelectedItem().equals(Integer.toString(i)))
            {
                labels.setText(fortune[i]);
            }

    }
}

if it works tick it as correct answer

padman
  • 491
  • 1
  • 3
  • 14