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.
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.
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.
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.
nothing like that.
Last edited by kpat; April 3rd, 2012 at 10:53 AM.
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.
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.
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.
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.
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?
Is this what is currently happening and it is not what you want the program to do?if an item isn't selected and the JButton is pressed all records in the database is displayed.
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.
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.
This is what i want to. this doesn't happen at the moment. this is want i want to happen to.if an item isn't selected and the JButton is pressed all records in the database is displayed.
If no item is selected but the button is pressed all data store in the database is displayed in the table.
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.
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/
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.
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.
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.
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.
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?
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.
How would i do that, sorry i dont understand
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.
problem solved
Last edited by kpat; April 3rd, 2012 at 10:52 AM.
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.
problem solved
Last edited by kpat; April 8th, 2012 at 07:27 AM.
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.
wrong code sorry new code added
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/
kpat (April 3rd, 2012)