Results 1 to 13 of 13

Thread: choice

  1. #1
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default choice

    Java Code:
    public void x()
    {
    Choice c1 = new Choice();
    Choice c = new Choice();
    //filling c with items
    c.addItem(name);
    ...
    //then
      c.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent e){
    //filling de c1
    c1.addItem(..)
    }
      });
    JButton button;
    button.addActionListener(new ButtonBus(c.getSelectedItem(),c1.getSelectedItem()));
    }
    my problem is c1.getelectedItem() =null and out itemstatechange nloc
    how could i have the selected item
    please help me

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    What does getSelectedItem return if there are no selections?

  3. #3
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: choice

    so how do get the showed item??

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    Does the Choice class have a get... method you could use?

  5. #5
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: choice

    tha's me how is asking does there any method that help me to have the showed item in the Choice Component

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    how is asking does there any method that help me
    You are expected to read the API doc for the Choice class and find a method that will do what you want.
    Have you done that?
    Which of the get... methods do you think would be useful?
    If you have any specific questions about any of the get... methods, ask them here.

  7. #7
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: choice

    i read Choice api but i didn't have any method that help to have the showed item

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    If the Choice class does not have any methods to get items from its menu list then I don't think you can do it.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    If you are not sure which get.... method to use. Try using several of them and print out the values that they return.
    Then look at what is printed to see which method returned what you want.

  10. #10
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: choice

    my problem is that when i print result into "itemstatechange" bloc of c1.getSelectedItem is ok
    but out this bloc i have null as result
    so how could i have the selected item out "itemstatechange" bloc

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    when i print result into "itemstatechange" bloc of c1.getSelectedItem is ok
    but out this bloc i have null as result
    By "print" do mean assign?
    This problem sounds like you have two variables with the same name at different scopes.
    You assign the local variable a value which goes away when you exit a method. The variable at the other level of scope does not get any value and stays null.

  12. #12
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: choice

    Java Code:
    public void x()
    {
    String x="";
    Choice c1 = new C
    Choice();
    Choice c = new Choice();
    //filling c with items
    c.addItem(name);
    ...
    //then
      c.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent e){
    //filling de c1
    c1.addItem(..)
    x=c1.getSelectedItem();
    System.out.println("x1="+x);
    }
      });
    System.out.println("x2="+x);
    }
    that's have as result
    x1="the selected item"
    x2=null
    my question is how to get the value of xthe selected item of c1(Choice) out of itemstatechange

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: choice

    Define x at the class level, not inside of a method.

    Your sample code is not very helpful. The indentation is very poor. The alignment of { and } is bad.
    These 2 lines of code are junk:

    Choice c1 = new C
    Choice();

Similar Threads

  1. LayoutManager Best Choice
    By 67726e in forum AWT / Swing
    Replies: 2
    Last Post: 11-10-2010, 02:59 AM
  2. Syntax for choice box(combo box)
    By NickkicN in forum New To Java
    Replies: 2
    Last Post: 08-14-2008, 10:26 PM
  3. Need help with T/F and Multiple Choice
    By sayso36 in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 04:39 PM
  4. Using java.awt.Choice
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:33 PM

Posting Permissions

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