how to add events in ComboBox

Thread Solved

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

how to add events in ComboBox

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


}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: how to add events in ComboBox

 
0
  #2
Jan 9th, 2009
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 14
Reputation: mercury113 has a little shameless behaviour in the past 
Solved Threads: 0
mercury113 mercury113 is offline Offline
Newbie Poster

Re: how to add events in ComboBox

 
0
  #3
Jan 9th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: how to add events in ComboBox

 
0
  #4
Jan 9th, 2009
Hello again,

Heres a demo code:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ItemEvent;
  4. import java.awt.event.ItemListener;
  5.  
  6. public class GUIWindow implements ItemListener{
  7. JComboBox c;
  8. public GUIWindow(){
  9. JFrame f = new JFrame();
  10. f.setSize(500, 500);
  11. f.setLayout(new BorderLayout());
  12. c = new JComboBox(new Object[]{"1","2","3","4","5","6"});
  13. c.addItemListener(this);
  14. Container con = f.getContentPane();
  15. con.add(c,BorderLayout.CENTER);
  16. f.setVisible(true);
  17. }
  18.  
  19. public void itemStateChanged(ItemEvent e) {
  20. if(e.getSource() == c) {// matching with JComboBox Object
  21. if(c.getSelectedItem().equals("3")){
  22. System.out.println("CORRECT!");
  23. }else{
  24. System.out.println("WRONG!");
  25. }
  26. }
  27. }
  28.  
  29. public static void main(String[] args) {
  30. new GUIWindow();
  31. }
  32. }

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 14
Reputation: mercury113 has a little shameless behaviour in the past 
Solved Threads: 0
mercury113 mercury113 is offline Offline
Newbie Poster

Re: how to add events in ComboBox

 
0
  #5
Jan 9th, 2009
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???
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: how to add events in ComboBox

 
0
  #6
Jan 9th, 2009
yes,
  1. if(e.getSource() == c) {// matching with JComboBox Object
  2. if(c.getSelectedItem().equals("3")){
  3. System.out.println("CORRECT!");
  4. }else{
  5. System.out.println("WRONG!");
  6. }
  7. c.setEnabled(false);
  8. }

Dont forget to set this thread as solved.

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 14
Reputation: mercury113 has a little shameless behaviour in the past 
Solved Threads: 0
mercury113 mercury113 is offline Offline
Newbie Poster

Re: how to add events in ComboBox

 
1
  #7
Jan 9th, 2009
tNx a loT!!!

my pRoblem is soLved!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1452 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC