943,925 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2545
  • Java RSS
Jan 9th, 2009
0

how to add events in ComboBox

Expand Post »
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();


}
}
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
mercury113 is offline Offline
15 posts
since Jan 2009
Jan 9th, 2009
0

Re: how to add events in ComboBox

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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 9th, 2009
0

Re: how to add events in ComboBox

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...
Reputation Points: 6
Solved Threads: 0
Newbie Poster
mercury113 is offline Offline
15 posts
since Jan 2009
Jan 9th, 2009
0

Re: how to add events in ComboBox

Hello again,

Heres a demo code:
java Syntax (Toggle Plain Text)
  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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 9th, 2009
0

Re: how to add events in ComboBox

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???
Reputation Points: 6
Solved Threads: 0
Newbie Poster
mercury113 is offline Offline
15 posts
since Jan 2009
Jan 9th, 2009
0

Re: how to add events in ComboBox

yes,
java Syntax (Toggle Plain Text)
  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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 9th, 2009
1

Re: how to add events in ComboBox

tNx a loT!!!

my pRoblem is soLved!!
Reputation Points: 6
Solved Threads: 0
Newbie Poster
mercury113 is offline Offline
15 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: linked stack embedded in a sequencial vector?
Next Thread in Java Forum Timeline: Standard input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC