This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I have a main JCombobox in which are stored some values. When a user clicks on a selection (jcbox.getSelectedItem()) in this JComboBox, two other JComboBoxes are to get some set of values. The mystery is that I am able to set the values in the two other JComboxes, but the values are being repeated more than once. To counter this problem, I used the jcbox1.insertItemAt(<value>,<index> method. The second mystery is that on usage of such a command, the first value in the list does not appear on the JComboBox. The third mystery is that when I select any value in the second or third JCombobox, it does not appear on the top of the JCombobox. Please help!
First of all I don't think Master Tiger is your real name, but your moderator will get to you soon. 1. I cant really answer this with the exact problem, but it appears that the Action you are using for the first JComboBox is running twice. Which type of listener are you using. Are you using ListSelectionListener. I think this fires each time something is selected. Or maybe you can have the first line of code in your response to the action to clear out the other JComboBoxes first. 2. The default of JComboboxes I thought was to be blank, or no index, and after you select it would appear. But I could be wrong there. I just remember having to code for something or another. Great help I am there huh? 3. I think this one is a personal problem. Just Kidding. Unfortunately I don't have an answer here. Mark
Hey there Mark, Thanks for being the only reply as of now. As for my real name, it is as specified. 1. The event handler is ActionEvent and the listener is ActionListener. I have the method actionPerformed(ActionEvent ae) to capture the event on the JComboBox. It is in this method I set the list values for JComboboxs 2 & 3. 2. When the event is captured, I check which element in the first box is selected. Based on that, I use the removeAllItems() method to clear the boxes 2 & 3. Then I insert the approrpiate values. Anything more, just let me know.
I'd try to use the ListSelectionListener rather than the ActionListener. I think ActionPerformed is being run twice. But you are calling removeAllItems. Hmm. Is there a query or something that you use to fill the data, maybe it's in the query. Again these are just guesses. Mark
There does appear to be quite a few bugs reported on the JComboBox especially in the Windows L&F. It may be firing an extra action event when the box gets focus in addition to the selection. Or it may be that the updating of the editable area of the combo box after the selection triggers the event. Does it happen if the combo box is non-editable?