Nested JComboBoxes in Java - java

I've created a JComboBox and when I choose an option from the combo box I want to add another combo box (for choosing more specific details) for the selected option. Is this possible?
I've tried creating another JComboBox in the original combo box's actionPerformed class, but I didn't solve the problem.
The result should look something like this:

Regarding adding a new JComboBox: Rather than actionPerformed try with JComboBox.addItemListener(ItemEvent). This is the right place to get the selected item and therefore to create and populate a new JComboBox with details you want to include.
Here a link to a demo project in GitHub

Related

What text to put to a JComboBox where you want to prompt the user to selection?

I am using multiple JComboBox in a single JPanel. I was wondering what is the go to initial JComboBox selection text?
For example I can put "None" but that will seem as if some value is already selected in the JComboBox. I am using labels for which type of data they are so writing their type is unnecessary.
I can either put "" but something like "----" seems better. Is there a standard text when you want the user to select some value from JComboBox?
If you want to set an initial value to JComboBox that should be non-selectable (as soon as user selects another option, it will not be possible to select the initial one again), than it has been already answered here.
One way is to use a custom renderer. The renderer will simply display a default prompt when no item of the combo box has been selected.
Check out Combo Box Prompt for an example of this approach.

Populate a JComboBox with list of Users from a text file

I have created a JFrame application in Netbeans, which has user creation and user log in for a tutorial application I am making, how would I populate a JComboBox from a text file when a user is created?
I have created the application/user creation, But I am stuck on the JComboBox selection from the file... I have seen multiple things on how to, but nothing is working at this time.
The application takes a persons:
First Name
Last Name
Desired Username (The thing I want in the JComboBox)
And the log-in is literally just selecting the user from the JComboBox and listing all users created and clicking a button to log-in. The text file is located: F:\ProjectDecember\Users.txt
Any help would be appreciated.
Thank You.
Check out Combo Box With Custom Renderer.
It shows how you can create a custom renderer for your custom Object that you add to the combo box. It also shows an approach that you can use so you don't break the default functionality of the combo box when you use this custom renderer.

Jcombobox with Jcheckboxes

how can I create (with JAVA) Combobox that contains Checkboxes for multiple selection and display the selected items in the Combobox like this picture:
click to see the pic
and thnx for advance.
EDIT:
I found this API (JAPURA API) and it's great but when I select multiple things I want to display the selected items instead of "* multiple items *".
Here's the link to achieve your goal:
JComboBox Providing a Custom Renderer
I would suggest you create a JButton and style it in the form of a Combobox if you want that.
Then, at the onclick function (actionperformed), you create a JPanel and make it visible under the button. In the panel, you can put whatever you want, so you just put checkboxes there.
I know this is kind of a workaround of your problem, but it should be easy for you to do so, and the actual user does not see a difference at all.
Hope I could help you.
Cheers,
Lucky

Changing the contents of a JComboBox during runtime

Alright so I'm trying to create a combo box that will update it's contents during runtime except I have no clue how to do this without receiving a bunch of errors. Is there some sort of method that I can use in order to accomplish this? For example, I have a vector that might start out with the name in drawers 1 and 2 be hi and bye. Then during runtime the program will change drawer one and two to eggs, sausage and add a third drawer with the name being computer. How can I go about changing the name on a JComboBox during runtime?
You want to clear the combobox of all entries using removeAllItems(), then re-add the items from the Vector using addItem().
The data shown in the ComboBox actually lives in its model - some subclass of ComboBoxModel.
DefaultComboBoxModel has methods for adding and removing elements. If you want to completely replace the combo box's contents at runtime, the simplest way might be to just build a new model and call theComboBox.setModel(theNewModel) with it. Also see setSelectedItem() for setting the selection.

how to set text to a jcombobox, but the text set should not be an element of jcombobox

is their any way to set text for a combo box, which is not an element of the combobox.
cboSubjects=new JComboBox();
cbo.addItem("Maths"); // and few more subjects are added
cbo.setSelectedItem("subjects"); // this does not set the default text of combobox
Is their any way to solve this problem ? I need something which works like combobox.text
property of combobox in visual basic
I am working on school management system. I need help.
I'd like to show you a possible alternative :)
Note, you need to call label.setDisplayedMnemonic('s'); and label.setLabelFor(combo) to complete the effect!
You can make jComboBox editable, cbo.setEditable(true);, and after that set the text you want: cbo.setSelectedItem("subjects");

Categories