-
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.
-
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"
-
Re: JComboBox issue. If user selects any item do this... (if statment)
-
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.
-
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.
-
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.
-
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.
-
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?
Quote:
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?
-
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.
Quote:
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.
-
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?
-
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?
-
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.
-
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.
-
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.
-
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.
-
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?
-
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?
-
Re: JComboBox issue. If user selects any item do this... (if statment)
How would i do that, sorry i dont understand
-
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.
-
Re: JComboBox issue. If user selects any item do this... (if statment)
-
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.
-
Re: JComboBox issue. If user selects any item do this... (if statment)
-
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.
-
Re: JComboBox issue. If user selects any item do this... (if statment)
wrong code sorry new code added
-
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.