| | |
adding events in iTemlistner
![]() |
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
There are two ways how to get about this problem
1. One general listener for associated components like now in your case ItemListern
2.Each components handles his own events, in this case you can remove "implements ItemListener" from start of your class
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
PS2: Please start using code tags as [code]YOUR CODE HERE[/code] or [code=Java]YOUR CODE HERE[/code]
1. One general listener for associated components like now in your case ItemListern
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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 declarationPS2: Please start using code tags as [code]YOUR CODE HERE[/code] or [code=Java]YOUR CODE HERE[/code]
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Hello,
Heres another way to do it.
Heres another way to do it.
java Syntax (Toggle Plain Text)
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!"); } } } } }
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
![]() |
Other Threads in the Java Forum
- Previous Thread: transparent jframe
- Next Thread: Regarding JContentPane
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






