Java PropertyResolutionException when binding netbeans combobox selected item - java

I have a JComboBox which I designed in the Netbeans GUI designer. I have successfully bound the "elements" of the box to an ArrayList<String> so that the box populates properly. However, it currently automatically displays the first element of the list as the selectedItem.
I tried to bind the selectedItem to an int at first, thinking it would just show the item at the indicated index. No go. I got a ginormous exception that I didn't understand.
Then, I noticed that in the "Binding" window, it said the element had to be an Object. So, I bound it to a String, thinking that it would find the element that matched that string and show that element. No dice. Now I'm getting some sort of "PropertyResolutionException". I've even tried to use an Integer object with the same result.
Any ideas would be appreciated.

Absent your sscce, it's hard to say where things have gone awry. For reference, setSelectedItem() works as advertised in this example that selects L&F by name.

Related

Getting the text of all the Selected Checkbox in Java

So I'm doing a project that requires a lot of checkboxes. And all those selected checkboxes will go to the texture or something trigger by a button. I already browse here and I came up with this.
The code actually is almost what I'm looking for. But the problem is the way it displays.
What I want is to remove those [ , ] because I want to be the one who controls the way the text is display.
Well, passing the checkboxes List to the showMessageDialog method is implicitly calling List#toString(). The implementation of this method in class ArrayList is to print a comma-separated list of the values, just as you see it. If you want to display it in another way, you'd have to iterate over the elements of your list and build a string according to your likings.

jComboBox changes selectedItem when selecting repeated itens to the first on list

i have a jComboBox, it happens that i may have the same value for more than on item. In this case, when selecting one of them, , the selection goes always to the first item on the list. Right after the click.
Have someone experienced that?Have some solution for that, so the selection doesn't change?
When i select:
http://i.stack.imgur.com/IjlYM.png
Checking again:
http://i.stack.imgur.com/c1lcQ.png
it happens that i may have the same value for more than on item.
Then it sounds like you are adding a custom object to the combo box.
If it displays the same value but is a different item, then you need to implement the equals() method in your Object so the proper Object can be selected.
If you need more help then post a proper SSCCE that demonstrates the problem because we don't have enough information to keep guessing what you might be doing.
A JComboBox always tries to synchronize what is selected in the lists with what is shown in the display field. To do that, it searches the list sequentially for a match of the editor field. So it will always find the first one if there are identical items in the list. Thus you can't just use String objects if there is a possibility for identical Strings in the list. You need to do what #camickr said and use a custom object that has some way to distinguish between two objects whose toString() method returns the same thing (assuming you use the default model and editor).

What is a good practice to handle a lot of checkboxes in JavaFX

I need to build a View which has a 4 layer nested Multi-Accordion with a lot of checkboxes inside them. All together there might be around 30-40 Checkboxes all through the Accordions.
The next step will be, that i have some sort of logic behind all this. Depending on the selection combination of the checkboxes I will change a text label accordingly.
My thought process was: I put up all these checkboxes and give them a numeric fx:id representing there position in the nested accordion graph. Something like "1_1" or "2_4_1".
After that, I build one ChangeListener calling a Method on Selection of a Checkbox. I can look up the Id of the checked box, look it up in my data (to see which Text belongs to it and if any rules interfer with other boxes) and handle the logic accordingly while putting the id and its text in a Map or List to keep it for later and to keep track of the checked boxes.
Now I came to know, that getting the fx:id isnt something JavaFX wants me to do. I cant deliver a custom id in custom property inside the FXML either (couldnt find anything regarding this).
I am now pretty much at the end of my knowledge (I did just start with JavaFX and have some basic Java knowledge) and it seems to me, that I tackle this topic from the wrong side.
My question is now: What would be a best practice to handle dozens of checkboxes and trigger logic in the code according to the box that was checked without writing a ChangeListener for every single Check Box leaving me with some (imo) ugly code all the way.
EDIT: I forgot to mention: I did achieve some sort of functional solution by writing a custom CheckboxChangeListener with a reference to the Element the addListener method was called on and using "getId()" on this reference. I came to know though, that this method references the css:id of the fxml element and not its fx:id and I am not quite sure if this is a proper way to go
You should look into databinding with javafx. For example:
CheckBox cb1 = new CheckBox("1");
CheckBox cb2 = new CheckBox("2");
BooleanProperty isCb1Selected = cb1.selectedProperty();
BooleanProperty isCb2Selected = cb2.selectedProperty();
Textfield foo = new TextField().visibleProperty().bind(isCb1Selected.and(isCb2Selected));
This would hide the textfield foo if atleast one of the checkboxes isn't selected.
You can find other examples here and here an oracle tutorial

Creating a checkbox java list

i am trying to generate a checkbox list feeded by some records from a database just like the image (JAVA Swing), initially i tried with several chexkboxes but it didn't work, now i am trying with a multiple selection list but it didn't work as well and none of the answered questions here looks like to solve the specific needs i have.
Specifications:
Multiple selection list.
Every node of the list must have a checkbox object.
When checked every node must stay highlighted.
It must have an scrollbar if the content is bigger to the initially setted dimentions.
It must have an scrollbar if the content is bigger to the initially set dimensions.
Put the JList inside a JScrollPane.
When checked every node must stay highlighted.
This is going to confuse the user. The check marks are sufficient.
Every node of the list must have a check box object.
You'll have to extend a JList and a ListCellRenderer to gain this functionality. The ListSelectionListener that I have is over 100 lines of code.
You might find an already existing check box JList on the web. I have one in a book.

SWT ComboBoxCellEditor editable

I have a TableViewer where the values in one column should typically come from a dynamic list.
I'm currently using org.eclipse.jface.viewers.ComboBoxCellEditor , which is actually a Select-List: it stores the index of the selected value. If I change the underlying list (calling setItems(String[]), it's clumsy to keep the previous selected value... (specially if it's not included in the list anymore!) What I'd wish is actually a cell editor that stores, not the index from the list, but the string (perhaps letting the user edit it freely, perhaps not), where the list is just used as a suggestion at input time - like a "combobox" was supposed to work in the good old days... Is this possible?
I would suggest you to have your CellEditor to mimic the behavior that you are looking for. Extend ComboBoxViewerCellEditor and override doGetValue() method. Add modify listener on Combo control and also filter (which filters list items based on input text) to comboviewer.
You should look at :
org.eclipse.wst.xml.ui.internal.properties.StringComboBoxCellEditor This class comes from WTP project; It's an extended ComboBoxCellEditor that selects and returns Strings.
codemirror.eclipse.ui.xquery.viewers.StringComboBoxCellEditor It's the copy/paste of WTP StringComboBoxCellEditor; it adds the capability to add the item in the combo when it is not found.

Categories

Resources