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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Thread: JComboBox issue. If user selects any item do this... (if statment)

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JComboBox issue. If user selects any item do this... (if statment)

    Hi,

    I'm having trouble writing a piece of code. It's probs simple but i can't seem to be able to do it, i've tried everything i could think of. If you can provide some help i would appreciate it.
    Last edited by kpat; April 8th, 2012 at 07:28 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Can you explain what variables have what values that you could test for the "condition"?
    Like x > 10 or String not equal to "ASD"
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    nothing like that.
    Last edited by kpat; April 3rd, 2012 at 10:53 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    You can add a listener to the JComboBox class that will be called when the user selects an item.
    It also has methods about what has been selected.

    Read the API doc for the methods.
    Last edited by Norm; April 2nd, 2012 at 01:28 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    i did that at the start but i want the selected item only when the button is pressed.

    but when I press the Button without selecting an item from the JComboBox for some reason an item is selected for me and that item is the first one in the JComboBox item list.
    Last edited by kpat; April 3rd, 2012 at 10:53 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Please explain the problem with using the methods of the JComboBox class to determine what the user has done or not done.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    don't fully understand what your saying, but, ok.

    The user selects an item from the JComboBox. Once they have selected item they press a JButton and an action is preformed that searchs the item the user has select against data in a database and display records that match the item selected. But if an item isn't selected and the JButton is pressed all records in the database is displayed. However this doesn't happen, instead the first item in the JComboBox is used and only data with that item select is displayed.

    Hope it helps.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Can the program know if an item was selected? Suppose it could determine that?
    If you know that, can you decide what the program should do when the user press the button?

    if an item isn't selected and the JButton is pressed all records in the database is displayed.
    Is this what is currently happening and it is not what you want the program to do?
    Please explain what the code should do if the user does not select a item?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Yer the program knows what item is selected and i got it to search the database and get and display data with the item in it, but i also want it, so when no item is selected and the button is pressed all data is displayed.

    if an item isn't selected and the JButton is pressed all records in the database is displayed.
    This is what i want to. this doesn't happen at the moment. this is want i want to happen to.

    If no item is selected but the button is pressed all data store in the database is displayed in the table.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    What do the methods of the JComboBox return when nothing has been selected?
    Can your code remember if a selection has been made or not?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Wait, can you even "not select" an item in a JComboBox? Isn't the first item selected, by default, when the JComboBox gets created?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #12
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Norm - how would i find that out?

    aussiemcgr - Thats what i think is happening but i dont know how to stop that. Any ideas.
    Last edited by kpat; April 3rd, 2012 at 03:21 AM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Write test code that prints out the values returned by the JComboBox methods to see what they are.
    Try using one/some of the JComboBox methods to set a condition that could be tested for.
    Have a boolean flag that is set when an item is selected. Test if it was set. If not set, nothing was selected.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    i know that an item is being selected because values for that item is being printed out.
    Last edited by kpat; April 3rd, 2012 at 07:19 AM.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    I thought your problem was detecting when nothing had been selected in the JComboBox before the button was pressed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    no, the problem is that even when the user doesn't select an item from the JComboBox, by default the JComboBox selects the first item in the list. i dont want that to happen. do you know how to stop that?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Determine if THE USER has selected anything by using a listener that sets a flag if the user has selected anything.
    When the button is pressed, test if the flag is set. If the flag is not set, the user has not selected anything.

    Look at the methods of the JComboBox. Are there any you could use to help detect if the user has selected any item or not?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    How would i do that, sorry i dont understand

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Do you have any listeners for the JComboBox? When are they called?
    Is there a listener for when a user selects an item?
    If that listener is not called, does that mean that the user has not selected anything.

    Is that what you want to detect? That the user has not selected anything?

    I don't have a test program for this. Can you make a small simple program that compiles and executes for testing. Just a GUI with a JComboBox and some listeners.
    Last edited by Norm; April 3rd, 2012 at 08:13 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    problem solved
    Last edited by kpat; April 3rd, 2012 at 10:52 AM.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    For testing the program must compile and execute. The code you posted will not compile.

    The purpose of the test program is to find a way to detect if the user has made a selection in the JComboBox. It should not include any unnecessary code not related to that problem.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    problem solved
    Last edited by kpat; April 8th, 2012 at 07:27 AM.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    What is the purpose of that code? It is useless for testing.
    The message seems unrelated to anything that the user has selected.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Mar 2012
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    wrong code sorry new code added

  25. #25
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: JComboBox issue. If user selects any item do this... (if statment)

    Wait, so you just want an option in the JComboBox that you can use to indicate that nothing has been selected? Just add a blank option as the first in the JComboBox and check if that item is selected. There's no way to "not select" an item, but you can add an item that can tell you that the user doesn't want to select an item.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    kpat (April 3rd, 2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. If/Else Statment Help
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2012, 03:10 PM
  2. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 05:31 AM
  3. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  4. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  5. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 05:11 PM