Results 1 to 3 of 3
  1. #1
    MichaelG is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Question about java.awt.List

    Hello guys,

    I have added a java.awt.List in my applet with 3 items that would represent 3 different collors.
    The thing is that I must double clieck on the color name to really make the selection.

    This is how I check if a color was selected from the list:

    public void actionPerformed(ActionEvent evt) {

    String userChoice = evt.getActionCommand();

    if(userChoice=="red")
    {
    myColor = Color.red;
    }

    if (userChoice == "black")
    {
    myColor = Color.black;
    }

    if (userChoice == "green")
    {
    myColor = Color.green;
    }

    }

    How can I select the color from the list with a single click?


    Thank you all!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Question about java.awt.List

    Does that actually work?
    You're comparing Strings using '==' which is a mistake...it should be 'equals()'
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Question about java.awt.List

    you could create JButtons, so 3 buttons 1 for each colour and when the user click the green button it sets my colour to green? pretty easy to implement, or are you not allowed to use buttons and such.

    You could also say left mouse click sets my colour to green, right mouse sets it to red and mouse scroll sets it to black, But for this you would need a MouseListener and such.

    if you were to use the JButton example you could then say:

    Java Code:
    public void actionPerformed (ActionEvent e) {
    
    if (e.getSource == blackButton) {
    mycolor = Color.black;
    }
    else if (e.getSource == redButton) {
    //etc etc
    }
    }
    so you could use the '=='. As Tolls said you cant compare Strings with '==' you can only compare primitive types like char or int using '=='.

Similar Threads

  1. Hello All a little List question...
    By AnteeN in forum New To Java
    Replies: 11
    Last Post: 12-07-2011, 12:36 PM
  2. a linked list question
    By smacker in forum New To Java
    Replies: 5
    Last Post: 05-04-2011, 12:57 PM
  3. Question for a list of objects
    By johnhelen in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 09:23 PM
  4. List question, I don't understand why...
    By jigglywiggly in forum New To Java
    Replies: 14
    Last Post: 03-21-2010, 08:59 AM
  5. A simple List question
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 03:37 AM

Posting Permissions

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