i have this code in java...
my first combobox when u choose letter a will be correct...

but how will apply in the other combobox that when u choose it...

the letter b will be the correct answer??

plzz help me...

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


public class CaseStudyFirst implements ItemListener{


JComboBox choice1, choice2;
int score;


public CaseStudyFirst(){


JFrame f = new JFrame();
f.setSize(500, 500);


f.setLayout(new GridBagLayout());
GridBagConstraints x = new GridBagConstraints();
Container con = f.getContentPane();


// JLabel 2 be put here....


JLabel label1 = new JLabel("Name: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.insets = new Insets(3, 2, 2, 3);
x.gridx = 0;
x.gridy = 0;
con.add(label1, x);


JLabel label2 = new JLabel("Section: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.weighty = 0.0;
x.gridx = 0;
x.gridy = 1;
con.add(label2, x);


JLabel question1 = new JLabel("Questions: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.gridx = 0;
x.gridy = 2;
con.add(question1, x);



//JTextField to be put here...


JTextField TextField1 = new JTextField();
x.gridx = 1;
x.gridy = 0;
x.ipadx = 100;
x.ipady = 1;
x.gridwidth = 5;
TextField1.setToolTipText("Enter Your Name Here:  ");
con.add(TextField1, x);


JTextField TextField2 = new JTextField();
x.gridx = 1;
x.gridy = 1;
x.ipadx = 100;
x.ipady = 1;
x.gridwidth = 5;
TextField2.setToolTipText("Enter Your Section Here:  ");
con.add(TextField2, x);


choice1 = new JComboBox(new Object[]{"a","b","c","d"});
x.gridx = 0;
x.gridy = 3;
con.add(choice1, x);
choice1.addItemListener(this);


choice2 = new JComboBox(new Object[]{"a","b","c","d"});
x.gridx = 0;
x.gridy = 5;
con.add(choice2, x);
choice2.addItemListener(this);


f.pack();
f.setVisible(true);


}
public void itemStateChanged(ItemEvent e) {


if(e.getSource() == choice1) {
if(choice1.getSelectedItem().equals("a"))   {
System.out.println("CORRECT!");     }
else    {
System.out.println("WRONG!");
}
choice1.setEnabled(false);
}
}
)



public static void main(String[] args) {
new CaseStudyFirst();


}
}

Edited 3 Years Ago by pritaeas: Fixed formatting

Comments
No code tags and using chat-speak.

There are two ways how to get about this problem

1. One general listener for associated components like now in your case ItemListern

public void itemStateChanged(ItemEvent e) {
	
		if(e.getSource() == choice1) {
			if(choice1.getSelectedItem().equals("a")) {
				System.out.println("CORRECT!"); }
			else {
				System.out.println("WRONG!");
			}
			choice1.setEnabled(false);
		}
		else if(e.getSource() == choice2) {
			if(choice2.getSelectedItem().equals("b")) {
				System.out.println("CORRECT!"); }
			else {
				System.out.println("WRONG!");
			}
			choice2.setEnabled(false);
		}
	}

2.Each components handles his own events, in this case you can remove "implements ItemListener" from start of your class

choice1 = new JComboBox(new Object[]{"a","b","c","d"});
		x.gridx = 0;
		x.gridy = 3;
		con.add(choice1, x);
		choice1.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent ie){
				if(choice1.getSelectedItem().equals("a")) {
					System.out.println("CORRECT!"); }
				else {
					System.out.println("WRONG!");
				}
				choice1.setEnabled(false);
			}
		});
		
		choice2 = new JComboBox(new Object[]{"a","b","c","d"});
		x.gridx = 0;
		x.gridy = 5;
		con.add(choice2, x);
		choice2.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent ie){
				if(choice2.getSelectedItem().equals("a")) {
					System.out.println("CORRECT!"); }
				else {
					System.out.println("WRONG!");
				}
				choice2.setEnabled(false);
			}
		});

Preferences in usage are on the programmer. For long time I was going with first option, but now I turned to second as it is easier to find in the code and it does avoid complication of if/else if/else statements inside (one can get easily lost in that web)

PS: You are not closing frame properly you need to add this to your JFrame f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); let say after layout declaration
PS2: Please start using code tags as [code] YOUR CODE HERE [/code] or [code=Java]YOUR CODE HERE [/code]

Hello,

Heres another way to do it.

JComboBox[] comboList = new JComboBox[10]; // array of JComboBox type. length =10
	Object[] answerList = new Object[10]; // array of Object type. length = 10, this is hold the correct answers
	
	public GUIWindow(){
		
		// now when you create a JComboBox
		comboList[0] = new JComboBox(new Object[]{"1","2","3"});
		// and save correct option in answerList array
		answerList[0] = "2";
		comboList[0].addItemListener(this);
		
		// and so on, till 10
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() instanceof JComboBox){
			for(int i = 0; i < comboList.length; i++){
				if(comboList[i] == e.getSource()){
					if(comboList[i].getSelectedItem().equals(answerList[i])){
						System.out.println("Correct!");
					}else{
						System.out.println("False!");
					}
				}
			}
		}
		
	}
This article has been dead for over six months. Start a new discussion instead.