Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Quick JComboBox Event Question

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quick JComboBox Event Question

    Hi! I'm working on a program that displays a list of formulas in a combo box where the user will select one, and then two text boxes will show up where they enter the variables and submit for the solution. It's a pretty basic concept and I could do it no problem in VB/C#/C++ but I can't seem to find the Java syntax for it..

    Here's my code from the section where the condition needs to be:

    box.addItemListener(
    			new ItemListener(){
    				public void itemStateChanged(ItemEvent event)
    				{
    					add(button);
    					Select Case(box.selectedIndex)
    						Case 1: 
    							add(text1);
    							add(text2);
    						Case 2:
    							add(text3);
    							add(text4);
    						etc. 
    				}						
    			}
    		);
    I'm pretty sure that's not the correct syntax for the case statement either but that's the basic idea of what I'm trying to do. Thanks for any help!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Quick JComboBox Event Question

    see The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    I made a few changes to the code below
    box.addItemListener(
    			new ItemListener(){
    				public void itemStateChanged(ItemEvent event)
    				{
    				        if ( event.getStateChange() != ItemEvent.SELECTED ){return;}//if this is NOT a selection event
    					add(button);
    					switch (box.getSelectedIndex())://get the selected index of the JComboBox
    						case 1: 
    							add(text1);
    							add(text2);
    							break;
    						case 2:
    							add(text3);
    							add(text4);
    							break;
    						etc. 
    				}						
    			}
    		);

    Note that if you are adding actual Swing components to your GUI, you need to call revalidate() on the container to which you are adding them

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Quick JComboBox Event Question

    Alright, that's a good bit closer. However, none of the cases execute until after I change the selection a second time. Then it will display perfectly while switching between the first and second selections, but will display an extra set of labels and boxes after switching down to the third selection and back up to the others.

    Also the revalidate function wasn't being recognized for some reason unless there's an import I missed but I put in the invalidate, validate statements which is evidently what the revalidate does.

    Sorry for the noob questions but I appreciate the help.

Similar Threads

  1. Quick binary tree question.
    By Sinensis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 09:28 PM
  2. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 12:57 PM
  3. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM
  4. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  5. Replies: 6
    Last Post: April 14th, 2009, 08:02 AM