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 11 of 11

Thread: JCombobox help

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default JCombobox help

    Can somebody help me...............

    I actually dont know how to explain this but here it goes........

    I created a program which is a shipment calculator. There is part there were I have a Combobox which you can choose what type of Service you want to have.

    Now the thing that I need help on is how do you this:
    when you select a certain item in a combobox I the delivery time and price will appear on one of my textfields.
    example: I selected express service after selecting it 24hrs willl apear in my textbox and if I slect Critical 5 hrs will appear in my text box.

    I dont know which method to use I tried getItemAt but it wont work on a If statement.

    here is my code for that portion
     servi.setBounds(10,340,199,20);
            add(servi);
            servi.addItemListener(new ItemListener(){
           	public void itemStateChanged(ItemEvent event){
          	if(event.getStateChange()==ItemEvent.SELECTED){
     
     
     	}
     
           	}
           }
           	);
    Last edited by helloworld922; August 9th, 2010 at 09:10 AM. Reason: added my code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JCombobox help

    it wont work on a If statement
    Can you explain? Do you get errors? Please copy and paste full text of the error message here.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sman36 (August 11th, 2010)

  4. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JCombobox help

    to make my explanation easiser I just want to translate this into a java code:

    if I select item 1 from my drop down list 24 hrs will appear in my textfield.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JCombobox help

    pseudo code:
    if(selected item is #1) {
    set textfield to 24 hrs
    }

  6. The Following User Says Thank You to Norm For This Useful Post:

    sman36 (August 11th, 2010)

  7. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JCombobox help

    ^^ yup thats what I mean but whats the real code for that?? what API should I use I'm very confused

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JCombobox help

    Look at the doc for the JComboBox: what method will tell you which item was selected
    and for the textfield: what method will set its value.

  9. The Following User Says Thank You to Norm For This Useful Post:

    sman36 (August 11th, 2010)

  10. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JCombobox help

    I know that getItemAt and setSelectedIndex will tell which item was selected but it wont work on if statements and I already know what method to use that will set the value of textfield its .setText

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JCombobox help

    What doc are you reading for the JComboBox class? Did you see any methods with names starting with get?

    it wont work on if statements
    Can you explain what "won't work"?

  12. The Following User Says Thank You to Norm For This Useful Post:

    sman36 (August 11th, 2010)

  13. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JCombobox help

    Is it just me, or is this redundant:
    public void itemStateChanged(ItemEvent event){
            if(event.getStateChange()==ItemEvent.SELECTED){

    Why would you have to check to see if ItemEvent.SELECTED if that will only fire in the itemStateChanged event and you cant deselect an item in a JComboBox (or can you?). What if instead of doing that search, you instead just called a reference to your JComboBox asking for the selected item?

    What's wrong with this:
    public void itemStateChanged(ItemEvent event)
    {
    	int selected = JComboBox.getSelectedIndex();
    	if(selected==1) // or if you want the first item: if(selected==0)
    	{
    		JTextField.setText("24 hrs");
    	}
    }

  14. The Following User Says Thank You to aussiemcgr For This Useful Post:

    sman36 (August 11th, 2010)

  15. #10
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JCombobox help

    I got it !!!! thanks to everyone who posted

  16. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JCombobox help

    Quote Originally Posted by aussiemcgr View Post
    Why would you have to check to see if ItemEvent.SELECTED if that will only fire in the itemStateChanged event and you cant deselect an item in a JComboBox (or can you?).
    When you make a selection, the previously selected item gets deselected.

    What if instead of doing that search, you instead just called a reference to your JComboBox asking for the selected item?
    Then that code is liable to be executed twice.

    db

Similar Threads

  1. JMenu and JComboBox
    By javapenguin in forum AWT / Java Swing
    Replies: 1
    Last Post: July 3rd, 2010, 05:00 PM
  2. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM
  3. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  4. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 12:57 PM
  5. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM