views:

398

answers:

1

Hello. I am implementing a GUI using Swing for a Java Music Player. I'd like to know if it is possible to make a time bar like the bar in WinAmp for example that can be moved to jump to a certain position in a song and shows the current position. If possible, how would one go about this?

Also, I'm using a JList to display Playlists and I'd like to know how to get double click events. I know that there are selection events, but I'd like objects in the list to be selected and start playing upon double clicks, not single clicks. I'd also like them to only be selected once, so the the event is not continuously called.

Thank you.

+1  A: 

You surely can create such a control. You maybe want to sub-class JSlider and adapt it accordingly. You can simply override the paintComponent method to adapt the look but it it probably not done in a few minutes. You may also want to handle mouse clicks on the slider differently because I think normally clicking right or left of the thumb causes the slider to jump by a certain interval instead of jumping right to the click location.

As for double-clicks, simply use your overridden mouseClicked method of MouseListener. The MouseEvent has a method named getClickCount which will you can check to be 2 for a double-click.

I'm not quite sure what you mean with "only selected once". I'm guessing here but JList fires two selection "events" for each click: One on pressing the mouse button, another one upon release. This is annoying sometimes, especially if you fire something more expensive on selection.

Joey
Thanks. I'll keep this in mind. What I know is that JList has a ListSelectionListener that records which item on the list is selected. I only want an item to be selected when the user double clicks the item, and I don't want it to continuously fire the handler method (meaning I don't want the song to continuously stop and play, I want it to stop once and start playing the newly selected song once).Thank you.
GLRockwell
Well, then you obviously need to handle list selection yourself. I'm currently not quite sure qhether you should sub-class the JList or the ListSelectionModel for that but one of those should be the answer (Java is weird if every single problem requires sub-classing something *sigh*). However, most music players work differntly: They keep the *selection* in the list separate from what is actually playing. So you might want to just play the song upon double-click instead of using the selection "event". That way you can select other songs without affecting what's playing.
Joey