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
Hello, ranch!

I must develop a star rating system in java for an advanced media manager application that I am working on. This is heavy stuff for me. So far I only got a class that extends JPanel which I call RatingPanel.java. I don't know where to start from here because this is really advanced coding for me. I can imagine that the panel should have a group of five JButton objects in it with Icons one for unselected state, hover state and selected state. I have three star-icons that is called starSelectedIcon, starHoverIcon and starUnselectedIcon. I am wondering if I am on the right track. So far I have no code samples to post here since this is a question of an investigating nature. What do I first need to achieve this task? Could anyone out there guide me through this on a basic level if this is of a proper nature here. This is quite urgent for me.

Many thanks and regards in advance!
Robert!
 
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your design for the project has gotten into too much detail too soon.
Can you describe the project at a higher level?  Once there is a design for the project then worry about panels and buttons.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Your design for the project has gotten into too much detail too soon.
Can you describe the project at a higher level?  Once there is a design for the project then worry about panels and buttons.



Best, Mr. Radder!

It's a media database application for movies and videos on -and offline. I wan't to offer a rating system for the user to rate each movie he or she enter in to the database. I can't give a deeper description than that so far I am trying to figure out the proper gui for this project. I hope that makes sence.

Best regards,
Robert!
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worry about the GUI after you have defined what inputs are needed from the user and what data needs to be read and displayed from the database and how the data from the user is to be saved.
 
Saloon Keeper
Posts: 10783
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the UI, I would create a RateButtonGroup class that extends JPanel and is composed of a ButtonGroup. In the constructor, add one JToggleButton per star to the button group. Set rolloverEnabled(true) on each button, and also set the rollover icon and the rollover selected icon.

To each button, add an action listener that will iterate over all the buttons in the button group and sets the selected icon if the button is the selected button, or comes before the selected button and sets the unselected icon if the button comes afterwards. You may also want to set the value of an OptionalInt field here, so that your RateButtonGroup class can be queried for the current rating.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Worry about the GUI after you have defined what inputs are needed from the user and what data needs to be read and displayed from the database and how the data from the user is to be saved.



Best, Mr. Radder!

When it comes to the database part I have a database configured for the project using derby embedded database. I have one main table called tbl_mediatitle and several tables with foreign keys that are linked together. One of the linked tables looks like this, by the way, all the tables are stored in text files with ";"-delimited fields



... and so on. I have just started on the project in question so my tables are not yet completed but that's how they will look like. In the projects main frame I have a JTable in which I display the title, original title, media type, runtime and production year. Below the JTable I have a JTabbedPane that contains scrollable panels with forms in which the data displayed. The forms concists of many components like textfields, checkboxes, radiobuttons, textareas, lists and other tables. That pretty much describes how far I have gotten in the process. Hope it sounds some what clear.

Best regards,
Robert!

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

Stephan van Hulst wrote:For the UI, I would create a RateButtonGroup class that extends JPanel and is composed of a ButtonGroup. In the constructor, add one JToggleButton per star to the button group. Set rolloverEnabled(true) on each button, and also set the rollover icon and the rollover selected icon.

To each button, add an action listener that will iterate over all the buttons in the button group and sets the selected icon if the button is the selected button, or comes before the selected button and sets the unselected icon if the button comes afterwards. You may also want to set the value of an OptionalInt field here, so that your RateButtonGroup class can be queried for the current rating.




Best, Mr. van Hulst!

I am totally lost to be honest. So far I have "tried" this approach, I think it is the start of the approach you are describing. Correct me if I am wrong, but I code in the dark for now. This is how my RatingPanel-class looks like now, totally out there  



Sorry for the amount of code posted, but I had no choice than to show at what level I am at. Sorry.

Best regards,
Robert!

 
Stephan van Hulst
Saloon Keeper
Posts: 10783
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you're already well on your way. What problem are you having exactly?

Instead of keeping your buttons as separate fields, I would just save them in a list or array. You actually don't even have to do that, you can just traverse the elements of the ButtonGroup field in a loop.

Why do you have a reference to your main frame? Components should not have a dependency on their containers.

Why are you using the system class loader? Use the current class loader:
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Instead of keeping your buttons as separate fields, I would just save them in a list or array. You actually don't even have to do that, you can just traverse the elements of the ButtonGroup field in a loop.



I am so lost. I know how to traverse elements in a ButtonGroup like below



I don't even know where to use this functionality in my class RatingPanel. How do I keep the JToggleButton objects selected when the user moves the mouse over them or click on them? Do I use an ActionListener for this or a MouseListener? When you say "save them in a list or array", do you mean something like below:



Also I corrected my RatingPanel-class and removed the instance of the main JFrame in it's constrcutor and used

as a classloader. I must say that this is way too advanced for me. Maybe I should use a JComboBox with star - JLabels in it, as a rating widget. But that Is not what I wan't for this project so I must continue to work more on this component. What should I do next?

 
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to keep references to your JToggleButtons and your ButtonGroup.

In your method: createJButton(ImageIcon icon) {...}
I would also add the parameters: boolean isSelected, ButtonGroup bg, int nrOfStars
so that you can add the correct ActionListener and set the ButtonGroup, so that you do not need a reference to that ButtonGroup. The parameter: isSelected enables you to set the initial state.

The ActionListener to be added may be of the form:

where the method setNrOfStars deals with that number in some way.

The ActionListeners will then take care of everything, and so you do not need the references. The same hods for the ButtonGroup. Once created and the Buttons added to it, it will take care of the selections, and you don't need the reference anymore.

This way many things will go automatically. So, is there any reason that requires a reference to a Button or the Group?
 
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!

Thank you so much for your kind reply and hints. I have read them through very carefully, they are very clear and concist. Sorry to say that I am very confused at the moment. Currently I am trying to figure out a way to keep track of the mouse movements. I have changed the code in my RatingPanel a bit and added the buttons in a List. How can I select the buttons in a flow order? I am sorry if I am off topic at this very now, but overall I am struggling very hard to get a grip on the very basic functionality of a rating system. This is how my RatingPanel looks like now, I haven't gotten any further - struggling.  



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, ranchers!

I don't know if I am on the right track, but this is what I got so far. I am working on a ActionListener and a MouseListener for my buttons and trying to set the proper Icon on them but I know that is not the path. I am so off grid at the moment allthough I can imagine the final solution so clearly, It's really cruel.





My createJButton() method



I am trying not to give up because I am so set on creating a rating widget. More to come.

Regards,
Robert!


 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Group should control which button is selected.  The setSelectedIcon  method would be called BEFORE the button is clicked.  The  button class will then use that icon when the button is selected.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:The Group should control which button is selected.  The setSelectedIcon  method would be called BEFORE the button is clicked.  The  button class will then use that icon when the button is selected.



Best, Mr. Radder!



Forgive me I am a terrible coder, it's too sad because I see the solution in front of me and understand exactly what you are hinting at. Have I instantiated the ButtonGroup correctly? Shouldn't it go inside the constructor and not inside the method? How can the ButtonGroup remember which button is clicked or not?  

Best regards,
Robert!
 
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Ingmarsson wrote:. . . Have I instantiated the ButtonGroup correctly?

Have you found a tutorial about button groups? This is an example. Does your instantiation code look like that in the tutorial? Have you added all the buttons you need to that group? If so, yes you have

Shouldn't it go inside the constructor and not inside the method?

If the group is a field, then instantiate it in the constructor. Most of the time, however, you have an initGUI() method or similar, and the group works well as a local variable inside that method, so it is instantiated there too.

How can the ButtonGroup remember which button is clicked or not? . . .

Don't know. You can find the original code easily enough and read that. Maybe there is a listener automatically added to the radio button which tells the button group to un‑click all the other buttons. Maybe something different. But you only need to know that the group does remember which button has been clicked most recently.
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Robert,

I don't know if it is wise to trouble you with more information, but I want to add some tips, and in the end I show you a minimum rating application, to illustrate what I had in mind.

First of all: I found out I needed a reference to the buttongroup. In case of a new item that must be rated, one has to unselect all the togglebuttons, and that only worked with: buttonGroup.clearSelection();

In your method 'createButton' you can set the icons for the states: selected, unselected, disabled and rollover. Saves you from setting these later on.

In my tiny application I simulated the rating and, when having an object that has not been rated yet, a clear rating button to reset the current rating.

Just a demo, you might have look later on if you get running what you have now. This is my code:

 
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!

Thank you so much for your reply. I have studied your code carefully and applied it to my RatingPanel class but I can't get a handle on how it works. Meanwhile I have another idea. I have attached an MouseListener and MouseMotionListener to my RatingPanel class. I wan't to figure out a way to use the classes Rectangle and Point to handle the selection of JToggleButtons in my panel.





Could anyone help me out here I would be so much greatful. Am I on the right track in any way? Please let me know.

Best regards,
Robert!
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of this panel?  Is it to get a user's choice of 1 of 5 different rating values?
The standard/default behavior of the buttons in a group will provide that.  No need for any mouse listeners.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What is the purpose of this panel?  Is it to get a user's choice of 1 of 5 different rating values?
The standard/default behavior of the buttons in a group will provide that.  No need for any mouse listeners.



Best, Mr. Radder!

I am having trouble with the selection of the buttons in the panel. I somehow wan'ts them to stay selected in a certain order of flow if that is the correct expression? In the Rectangle class there is a grow method. I wan't to expand and retract it and say select/deselect current number of buttons that the mouse pointer visit. I can't explain it more carefully for now, I am experimenting for the moment.

Hope that sounds clear enough.

Best wishes,
Robert!
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I somehow wan'ts them to stay selected  


Adding the buttons to the same group will keep more than one of them being selected at a time.  The previous code had a new group for each button which does not work.  Use only 1 group for all.

Post your current code if it allows more than 1 button to be selected at a time.

The posted code does not have comments describing what it is trying to do so anyone reading it has to guess what it is supposed to do.
 
Campbell Ritchie
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want several buttons selected simultaneously, consider whether a radio button is the correct control. Maybe tick boxes/check boxes would work better. Have you been through any tutorials or books about radio buttons?
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you want ...


Why not put comments in the code describing what it is supposed to do.
I am continually surprised by posters on this site that do not see comments as a required part of programming.   I think I am the only poster that asks OPs to design their programs and write comments in the code so when others read the code, the code's purpose is described and gives some direction for what the code is trying to do.
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm
The adage here is very much towards self-documenting code that lets itself read like a novel ("telling a story"). And if there is comment, it should be about the why and not about the what. Well, the opinions about that are diverse. Writing self-documenting code is certainly not easy.

@Robert
The problem is that we do not have a good picture of what it is that you want, other than that it should be a rating system. For instance: in my code I used the toggle buttons only as indication of the number of stars, and I used a Label to show that number of stars. Then it makes sense to have only one button selected. It is possible to use the buttons themselves to indicate the rating, for instance: if the first three buttons from the left are selected, that means a rating of three stars, and maybe the icons on the selected buttons show a star. Then having more than one button selected makes sense. Or maybe you have some completely different ideas.

So, like Norm asked, can you shoe us your current code concerning the GUI? If you can, leave the database part out. And if possible, describe how you want the code to indicate a rating. Comments in the code are okay, but I can do without (hpoefully).
 
Stephan van Hulst
Saloon Keeper
Posts: 10783
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:I think I am the only poster that asks OPs to design their programs and write comments in the code so when others read the code, the code's purpose is described and gives some direction for what the code is trying to do.


If you are talking about method documentation, then I partly agree. In your source repository, you definitely should write method documentation in your code to document the behavior and pre- and post-conditions. However, I don't find that a strict necessity when demonstrating code on this site for either code that isn't working or example code of how something should work, although adding it may definitely improve results for both. Additionally, people can comment on the quality of your documentation.

The fact of the matter is that if you can see the actual code and you need documentation to understand it, it's already an indication that the code is bad and needs to be rewritten. This is more important than writing documentation.

Probably the most important application of code comments is not even method documentation, but explanations giving the historical reasons WHY a confusing bit of code was written. These must be few, and typically should only appear when maintaining older code-bases.
 
Stephan van Hulst
Saloon Keeper
Posts: 10783
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you want several buttons selected simultaneously, consider whether a radio button is the correct control. Maybe tick boxes/check boxes would work better.


I recommend creating instances of JToggleButton instead of radio button or check boxes. Write a custom implementation of JToggleButton.ToggleButtonModel, and set an instance of of the custom button model for each button. You can then control the isSelected() state for each button, which may depend on whether a button for a higher rating is selected.

You can also control the rollover state of the button with the isRollover() method of the model.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best ranchers!

I am having so much trouble with this task that I have outlined here in this post. To all that have replied; a great thank you for your help and guidance. I am a slow learner and I may have reached my limits as the noob coder that I am and I am also a lousy poster here. My main passions in life are film and programming. Allthough I have had programming as a main passion for as at least two and a half decades ago I have not accomplished much in the area.

What I am trying to do now with my rating panel is to have mouse listeners take care of the selection of buttons. I wan't to select a button after the mouse pointer's location back and forward and then select all the buttons that comes before the selected one. I don't believe that you can accomplish using a ToggleButtonModel or ButtonGroup as suggested earlier in this post by Stephan van Hulst.

Does anyone understand what I am trying to do or is it impossible to accomplish this? Please forgive me for not showing any code, I am just expermenting with mouse events at the moment.

Best regards,
Robert!  
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

trying to do now with my rating panel is to have mouse listeners take care of the selection of buttons.


Why use buttons then as that is the main purpose of a button.  Mouse listeners would be used if you wanted to customize your interface with images or other location defining objects that are drawn on the GUI.

select all the buttons that comes before the selected one.


I would think that can be done with buttons.  Don't use a button group which limits the selections.  If a button detects its selection then it can also select the ones BEFORE it and deselect those AFTER it.
Forget about mouse events.
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

select all the buttons that comes before the selected one.



I would think that can be done with buttons.  Don't use a button group which limits the selections.  If a button detects its selection then it can also select the ones BEFORE it and deselect those AFTER it. Forget about mouse events.



Best, Mr.Radder!

Thank you so much for your reply. I have removed my buttons from the ButtonGroup and skipped the mouse events, I was experimenting with the Point and Rectangle classes totally out there. But how can one button detect the selection of a previous one. I have five buttons, how can button 3 know if button 1-2 is selected on so on or how button 5-3 can be aware of the selection of button 1-2. Ok, now I am confused.   Can you show me an example? Thank you so much for your time.

Best regards,
Robert!
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are references to the buttons in an array: 1 2 3 4 5
If the user selects button 3, then the code would know that 3 was selected and could select 1 and 2 and deselect 4 and 5

There is a state in each button that remembers if it is selected.  
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another example, with buttons. It uses an extension of the JToggleButton class, to remember the index. If a button is unselected, then a mouseclick on it sets all the buttons to the left, including the button clicked (and deselects any selected to the right), and a click on a selected button deselects that button and all buttons to the right. No ButtonGroups, just ActionListeners.

Is that anything like you had in mind?
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the documentation for the program not in the program as comments?
 
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 am so greatful for your help I am both laughing and crying. Thank you so much for your generous example. I have applied some of your code to my own RatingPanel class if that is okay? The selection seems to be working now in a what I call "flow order". But sometime a button is pretty slow when I press it, it won't select as easy that I wan't it to. Maybe one should try with a MouseListener here? If I dare to say so? I am an expert at complicating things both for me and others. But thank you again for your example I had fun studying it.  Thank you, Piet!

Now I just have to figure out what variable in the RatingPanel I need to pass to the database functionality so that the rating can be stored there. This is how my RatingPanel looks like now



Kind regards,
Robert Togglesson!
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Robert,

glad to hear you go it working! And of course, all code that is published at this site can be used freely.

I would create an instance field 'currentRating', initially 0. Then, when a button is clicked, you can maintan that field in your ActionListener. If the button is selected, that means that currentRating can be set to the index of the button. Otherwise, it can be set to 'index - 1'. See the ActionListener method and the field 'index' of JRatingButton.

There is one tiny problem with this: if currentRating = 0, does that mean there's no rating yet, or is the object that bad that it gets a rating of 0?

And indeed, it is a bit slow. If button 3 is unselected and it gets clicked, then you enter the 'isSelected' part of the ActionListener. Now, setting button 3 to 'isSelected' is done for you (you defined the icon in case of selected), but also buttons 1 and 2 must be set. Currently, I set them bluntly, but you can check if they are already selected. In that case, you do not need any action for them. Likewise for the other buttons and for the case when you click a selected icon. That may make the update a tad faster.

In my code snippet, I use "windows classic" as look&feel. I think this look&feel shows superior JToggleButtons. My default l&f is "metal" (I think) and that has an horrible presentatopn of togglebuttons. But that is my opinion.

Last remark: if you do want to use MouseListeners, then you could use JLabels instead of JToggleButtons, and add MouseListeners to them. When clicked, just change the icons of the labels.

Well, if you have finished the application or have other questions, let us know. Meanwhile, happy togglin'!
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Piet!

Thank you so much for your help and tips. I have some questions though. I think you're right about JToggleButtons, they are sort of "clumpsy" in a way no matter what l&f you use. When I think closer to the issue in question I might have changed my mind about using JToggleButtons. I think I am gonna go with JLabels instead from now on but hence I get stuck in different ideas. I wan't to use the code you showed above but I also wan't to use the Point and Rectangle classes and the listeners MouseListener and MouseMotionListener. The listeners should be attached to the panel and not the labes. Is there a way to then capture the bounds of a JLabel with the Rectangle and the Point classes and have them "animate" the appearance of selected and unselected image icons? Think of a piano, I can't describe the idea any better than that. I wan't to grow and retract a Rectangle over the labels with the mouse pointer and select / deselct it's icons along with it. Is this possible? Or am I not making any kind of sense here?

I am just brainstorming at this moment so please forgive me if I am not that clear about my ideas concerning this topic.

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

Here's a short example of what I am trying to accomplish.



Regards,
Robert!

 
Stephan van Hulst
Saloon Keeper
Posts: 10783
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Components accept a MouseListener instead of a MouseMotionListener as well. Pass a MouseAdapter and override the mouseEntered and mouseExited events. No need to muck around with rectangles and coordinates.
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following reply while Stephan gave his reply. So I didn't want to post it, since it has the same advice. But I had written the code anyway, so here goes:

hi Robert,

what is that affection of yours with Points and Rectangles?

What you have in mind IS possible. Whenever you get a MouseMoved Event, you can get the coordinates of the pointer, and asking whether that Point is within the bounds of one of the labels, and act accordingly. But believe me, that is like going from Denmark to Sweden, not via the bridge, but via Tokyo.

This work can all be done by adding simple MouseListeners to each label.
 
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!

Thank you so much for your reply and the code you showed me. I am trying to apply that in my project but it is not working well. I am a very slow learner and I can't call myself a programmer, not even by a longshot. I am learning through trial and error and through the study of internet articles and examples and of course from here. I am developing a complex media manager which is why I am so desperate in the need of a good rating component. I've read an article about a star rating system example once and they were using the Point and Rectangle classes. But I remember I wasn't satisfied with the result from that article somehow the first star didn't chaned it's icon at all but showed the selected icon after a mouse event. That was not I wanted. I am pretty much stuck here but I have accepted only to use the mouselistener event methods and labels but it's impossible for me to achieve what i am set up to develop on my level of knowledge. Hope that it is understandable what the problem is.

Best regards,
Robert!
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Robert Ingmarsson
Ranch Hand
Posts: 86
1
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, ranch!

This is how far I have gotten for now in this current project of mine. However the last label won't show the selected star icon when clicked on.



The above code results in the following error:



Hope someone could see more clearer than me what is going on here or if I am on the right track or not. Please have patience with a slow noobish learner like I am  

Best regards,
Robert!
 
Oh, sure, you could do that. Or you could eat some pie. While reading this 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!