Results 1 to 5 of 5

Thread: how do i get the text of a dropdown menu?[urgent]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    88

    how do i get the text of a dropdown menu?[urgent]

    Code:
    static String[] list = new String[20];
    
    for (int i=0; i<20; i++)
    {
           if (choice.getSelectedIndex() = list[i])
          {
               qty.setText(list[i+1]);
                price.setText(list[i+2]);
          }
    }
    how do i get the text of a dropdown menu?
    i need to do comparison of value between the choice n array
    e.g
    in dropdown menu choice, the value is "red" then compare against the array list[20] -->list[5]="red"

    can show code example?

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    I'm not sure what choice is an instance of, but if you want to compare two Strings, you shouldn't do like
    if (choice.getSelectedIndex() = list[i])
    because that's not even comparing.

    if (choice.getSelectedIndex() == list[i])
    is comparing, but it won't work on String variables, since they are not primitive types, they are objects.

    if (choice.getSelectedIndex().equals(list[i])
    is what you should use...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    88
    but i cant get it work! do u mind help me solve this prob? i attach the file here please help me debug. thks!
    Attached Files Attached Files

  4. #4
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    Code:
    choice.getSelectedIndex()
    returns the index of the currently selected item.
    My suggestion is that you use
    Code:
    choice.getSelectedItem()
    since that returns a string representative of the selected item.

    So the code would be
    Code:
    if (choice.getSelectedItem().equals(list[i]))
    Hope this helps...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    88
    how to add listener to a combo box? such that whenever the item is changed. the other textfield will change it value too?

    my code cant work the error keep saying cannot be applied

    Code:
    static Choice choice = new Choice();
    choice.addItemListener(this);
    public void itemStateChanged(ItemEvent e) 
    {
    	choice.getSelectedItem();
    	for (int i=0; i<20; i++)
    	{
    		if (choice.getSelectedItem().equals(list[i]))
    		{
    			qty.setText(list[i+1]);
    			price.setText(list[i+2]);
    			float p = Float.parseFloatprice.getText());
    			float q = Float.parseFloatqty.getText());
    			float total = p*q;
    			String t = String.valueOf(total)	;			totalprice.setText(t);
    			text1.setText("");
    		}
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width