adding events in iTemlistner

Reply

Join Date: Jan 2009
Posts: 8
Reputation: mercury113 has a little shameless behaviour in the past 
Solved Threads: 0
mercury113 mercury113 is offline Offline
Newbie Poster

adding events in iTemlistner

 
-1
  #1
Jan 10th, 2009
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();

}
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: adding events in iTemlistner

 
1
  #2
Jan 10th, 2009
There are two ways how to get about this problem

1. One general listener for associated components like now in your case ItemListern
  1. public void itemStateChanged(ItemEvent e) {
  2.  
  3. if(e.getSource() == choice1) {
  4. if(choice1.getSelectedItem().equals("a")) {
  5. System.out.println("CORRECT!"); }
  6. else {
  7. System.out.println("WRONG!");
  8. }
  9. choice1.setEnabled(false);
  10. }
  11. else if(e.getSource() == choice2) {
  12. if(choice2.getSelectedItem().equals("b")) {
  13. System.out.println("CORRECT!"); }
  14. else {
  15. System.out.println("WRONG!");
  16. }
  17. choice2.setEnabled(false);
  18. }
  19. }

2.Each components handles his own events, in this case you can remove "implements ItemListener" from start of your class
  1. choice1 = new JComboBox(new Object[]{"a","b","c","d"});
  2. x.gridx = 0;
  3. x.gridy = 3;
  4. con.add(choice1, x);
  5. choice1.addItemListener(new ItemListener(){
  6. public void itemStateChanged(ItemEvent ie){
  7. if(choice1.getSelectedItem().equals("a")) {
  8. System.out.println("CORRECT!"); }
  9. else {
  10. System.out.println("WRONG!");
  11. }
  12. choice1.setEnabled(false);
  13. }
  14. });
  15.  
  16. choice2 = new JComboBox(new Object[]{"a","b","c","d"});
  17. x.gridx = 0;
  18. x.gridy = 5;
  19. con.add(choice2, x);
  20. choice2.addItemListener(new ItemListener(){
  21. public void itemStateChanged(ItemEvent ie){
  22. if(choice2.getSelectedItem().equals("a")) {
  23. System.out.println("CORRECT!"); }
  24. else {
  25. System.out.println("WRONG!");
  26. }
  27. choice2.setEnabled(false);
  28. }
  29. });

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]
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: adding events in iTemlistner

 
0
  #3
Jan 10th, 2009
Hello,

Heres another way to do it.

  1. JComboBox[] comboList = new JComboBox[10]; // array of JComboBox type. length =10
  2. Object[] answerList = new Object[10]; // array of Object type. length = 10, this is hold the correct answers
  3.  
  4. public GUIWindow(){
  5.  
  6. // now when you create a JComboBox
  7. comboList[0] = new JComboBox(new Object[]{"1","2","3"});
  8. // and save correct option in answerList array
  9. answerList[0] = "2";
  10. comboList[0].addItemListener(this);
  11.  
  12. // and so on, till 10
  13. }
  14.  
  15. @Override
  16. public void itemStateChanged(ItemEvent e) {
  17. if(e.getSource() instanceof JComboBox){
  18. for(int i = 0; i < comboList.length; i++){
  19. if(comboList[i] == e.getSource()){
  20. if(comboList[i].getSelectedItem().equals(answerList[i])){
  21. System.out.println("Correct!");
  22. }else{
  23. System.out.println("False!");
  24. }
  25. }
  26. }
  27. }
  28.  
  29. }
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC