i have this code in java swing... im getting an headaChe thinking what to do... how do i add events in my ComboBox??

wHat i want is that when i sElect a comBoBox iT will tell wether the one you selected is correct or wrong... then the comBoBox will be disAbled...

plSs.. hElp me.... im a Beginner in jAva....

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


public class CaseStudy extends JFrame {


public CaseStudy() {



String question1 = "1. What is....";
String question2 = "q - 2";
String question3 = "q - 3";
String question4 = "q - 4";
String question5 = "q - 5";
String question6 = "q - 6";
String question7 = "q - 7";
String question8 = "q - 8";
String question9 = "q - 9";
String question10 = "q - 10";
String[] answer1 = { "a" , "b" , "c" , "d"};
String[] answer2 = { "a" , "b" , "c" , "d"};
String[] answer3 = { "a" , "b" , "c" , "d"};
String[] answer4 = { "a" , "b" , "c" , "d"};
String[] answer5 = { "a" , "b" , "c" , "d"};
String[] answer6 = { "a" , "b" , "c" , "d"};
String[] answer7 = { "a" , "b" , "c" , "d"};
String[] answer8 = { "a" , "b" , "c" , "d"};
String[] answer9 = { "a" , "b" , "c" , "d"};
String[] answer10 = { "a" , "b" , "c" , "d"};



setSize(600, 400);
setResizable(false);
setTitle("Case Study");
setDefaultCloseOperation(EXIT_ON_CLOSE);



final JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();


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


JLabel label2 = new JLabel("Section: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 2;
panel.add(label2, c);


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


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


JLabel label3 = new JLabel("Questions About Information Technology 1~10");
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
panel.add(label3, c);


//-- Questions


JLabel quest1 = new JLabel(question1);
c.gridx = 0;
c.gridy = 5;
panel.add(quest1, c);


JLabel quest2 = new JLabel(question2);
c.gridx = 0;
c.gridy = 7;
panel.add(quest2, c);


JLabel quest3 = new JLabel(question3);
c.gridx = 0;
c.gridy = 9;
panel.add(quest3, c);


JLabel quest4 = new JLabel(question4);
c.gridx = 0;
c.gridy = 11;
panel.add(quest4, c);


JLabel quest5 = new JLabel(question5);
c.gridx = 0;
c.gridy = 13;
panel.add(quest5, c);


JLabel quest6 = new JLabel(question6);
c.gridx = 0;
c.gridy = 15;
panel.add(quest6, c);


JLabel quest7 = new JLabel(question7);
c.gridx = 0;
c.gridy = 17;
panel.add(quest7, c);


JLabel quest8 = new JLabel(question8);
c.gridx = 0;
c.gridy = 19;
panel.add(quest8, c);


JLabel quest9 = new JLabel(question9);
c.gridx = 0;
c.gridy = 21;
panel.add(quest9, c);


JLabel quest10 = new JLabel(question10);
c.gridx = 0;
c.gridy = 23;
panel.add(quest10, c);


//-- End of Questions


//--Answers for Questionairre


JComboBox answers1 = new JComboBox(answer1);
answers1.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 6;
panel.add(answers1, c);


JComboBox answers2 = new JComboBox(answer2);
answers2.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 8;
panel.add(answers2, c);


JComboBox answers3 = new JComboBox(answer3);
answers3.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 10;
panel.add(answers3, c);


JComboBox answers4 = new JComboBox(answer1);
answers4.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 12;
panel.add(answers4, c);


JComboBox answers5 = new JComboBox(answer2);
answers5.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 14;
panel.add(answers5, c);


JComboBox answers6 = new JComboBox(answer3);
answers6.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 16;
panel.add(answers6, c);


JComboBox answers7 = new JComboBox(answer7);
answers7.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 18;
panel.add(answers7, c);


JComboBox answers8 = new JComboBox(answer8);
answers8.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 20;
panel.add(answers8, c);


JComboBox answers9 = new JComboBox(answer9);
answers9.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 22;
panel.add(answers9, c);


JComboBox answers10 = new JComboBox(answer10);
answers10.setSelectedIndex(-1);
c.gridx = 1;
c.gridy = 24;
panel.add(answers10, c);


//-- End of Anwers


//-- ComboBox Listeners...



JButton finish = new JButton("Finish!");
c.gridx = 2;
c.gridy = 26;
c.anchor = GridBagConstraints.PAGE_END;
c.gridwidth = 3;
c.gridheight = 2;
panel.add(finish, c);



finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(panel, "Your Score is ...",
"Question", JOptionPane.INFORMATION_MESSAGE);
}
});


getContentPane().add(panel);


}


public static void main(String[] args) {



CaseStudy CsStudy = new CaseStudy();
CsStudy.setVisible(true);
CsStudy.pack();



}
}

Hello,

FOR EVENTS:
you can use java.awt.events.ItemListener or java.awt.events.ActionListener interfaces.

TO GET SELECTED OBJECTS FRMO COMBO:
you can use getSelectedItem() method to get the selected object
OR
getSelectedIndex() method to get index of selected object
OR
getSelectedObjects() method to get array of selected objects (if multiple selection is enabled).

These methods are of JComboBox class

Regards,

hmMm..

tNx 4 da tIp...

but what really my problem is...

whaT is d exAct coDe 4 d itemListener....

and how do i aPPly iT???

bcoZ im using JFrame...

alMost all tutOrials i see doesnt use JFrame....

plzz...

giVe me somE tiPs...

Hello again,

Heres a demo code:

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

public class GUIWindow implements ItemListener{
	JComboBox c;
	public GUIWindow(){
		JFrame f = new JFrame();
		f.setSize(500, 500);
		f.setLayout(new BorderLayout());
		c = new JComboBox(new Object[]{"1","2","3","4","5","6"});
		c.addItemListener(this);
		Container con = f.getContentPane();
		con.add(c,BorderLayout.CENTER);
		f.setVisible(true);
	}

	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() == c) {// matching with JComboBox Object
			if(c.getSelectedItem().equals("3")){
				System.out.println("CORRECT!");
			}else{
				System.out.println("WRONG!");
			}
		}
	}
	
	public static void main(String[] args) {
		new GUIWindow();
	}
}

Regards,

TnX!!!

daTz wat i really neEd!!!

tnX a lot...

buT is it possible in java dat after u select an item in the comboBox...

da coMboBox will be disablEd???

yes,

if(e.getSource() == c) {// matching with JComboBox Object
			if(c.getSelectedItem().equals("3")){
				System.out.println("CORRECT!");
			}else{
				System.out.println("WRONG!");
			}
		c.setEnabled(false);
}

Dont forget to set this thread as solved.

Regards,

tNx a loT!!!

my pRoblem is soLved!!

commented: Best of luck! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.