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.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Help with star rating system in java

 
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

in the need of a good rating component


Is the purpose of that component to return an int value in the range from 1 to 5?



Yes. The idea is to set an int value in the above mentioned range in a mouse listener method by saying



... hope that was understood, pardon otherwise.

Robert!

 
Rancher
Posts: 3525
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Bartender
Posts: 3616
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm
I guess you never heard of the adage: one thing at a time.

@Robert:
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?
 
Norm Radder
Rancher
Posts: 3525
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
done
 
Piet Souris
Bartender
Posts: 3616
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A comment in the  earlier code sample that described the technique could have alerted you to what the code needed to do."
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best, Mr. Souris!

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.

Best wishes,
Robert!

 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

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!

Best regards,
Robert!

 
Are we home yet? Wait, did we forget the tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!