This week's book giveaway is in the Programmer Certification forum. We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
The code on line 117 used an index past the end of the list. 4 is the max index for a list of size 5
I suspect this is from earlier code that had the Gotcha of using a 1 based list with an empty first slot allowing the code to use indexes 1 to 5 vs the normal java code with 0 based indexing with indexes 0 to 4.
If you are going to use 0 based indexing, you need to subtract 1 from the index saved in the JRatingLabel instance. So index values would be 0 to 4 vs 1 to 5
Or follow the example from the earlier code and use 1 based indexing.
A comment in the earlier code sample that described the technique could have alerted you to what the code needed to do.
I guess you never heard of the adage: one thing at a time.
I would not add a MouseMotionListener to the JRadioLabels. Is there anything useful to do when the pointer moves over a JRadioLabel? Or is taking action only necessary when there is a Mouseclick on one of the labels? Problem is that many MouseMoved events will be delivered and you do a lot of unnecessay work.
The name 'index' of the JRadioLabel class is ill-chosen. The field represents the star rating of that Label, so better have it named that way. That also prevents confusion with the index of the Labels in your labelList.
As explained, i my example of the use of JToggleButtons and ActionListeners, I wanted to make the rating of a Label equal to its index in the list. Now, just adding the Labels to the list would give them the indices 0...4, meaning you had to subtract 1 from the rating of a Label to find it in the list. Therefore I added 'null' as the very first thing in the list, so that the next thing added (JRadioLabel1 with rating 1) would get index 1.
Have you thought about the other aspects of your rating application, like: how can the user obtain an object to rate? And if a JRadioLabel has been clicked, what to do with the rating of that Label?
I hate signatures!
posted 2 weeks ago
posted 2 weeks ago
"A comment in the earlier code sample that described the technique could have alerted you to what the code needed to do."
I have now changed the code so it looks more like the one you used with the JToggleButtons and added a null value in the list method. But the outcome is not what i expected or maybe I am doing something completely wrong. I think I am almost giving up on this component and instead use a JComboBox as a rating tool. But I am of a very stubborn nature so I struggle onward.
The user obtains the rating in the RatingPanel class when she / he clicks on the insert button in my MediaTitleDialog class that extends JDialog and the value is passed into the database of the application.
Thanks to everyone for your patience. Forgive me if I do not understand that much code -or explaining wise. But I do my best.
Ok, now I think I got it to work properly now. I changed the code a little bit in the for loops in the mouseReleased method so now the labels get selected in a proper order of flow. And then i added an int value named "clicked" which will capture the current selected index of the component in question. This is how the final result looks like well the one that I am satisfied with anyway
Plenty of thanks to everyone who has replied to this post!