-1

I'm working on a extremely basic Java video player project which is my coursework. I've 2 JTextPanes: 1 for listing all videos and 1 for creating playlist.

So far the only way I could add videos to the playlist is to put video id(from database) in a text field and click add button. But I would like my player to have the functionality to add videos to the JTextPane, or anything else if you think is better(for playlist), by clicking on the column of the JTextPane(all videos). I guess I have to add mouse listener to the JTextPane.

But how do I add mouse listener to each of the column of the JTextPane? Any help & suggestions greatly appreciated!

Thanks in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
alien45
  • 306
  • 2
  • 8
  • 1
    In your list of all videos, do you list one video per line? If so, don't you mean you want to add a mouse listener to each row (line), instead of to each column? A list of all videos would probably be better managed in a JList, instead of a JTextPane. – Mike Clark Feb 25 '12 at 00:41

2 Answers2

4

You probably want to change to using a JList, since you are handling a list of all videos.

You can then easily find which video the user has selected, usig JList.getSelectedItem() and add it to the playlist.

You cannot add a MouseListener to specific rows (did you mean rows, not columns?) of a JTextPane, only to the pane as a whole. You can, however, find out where in the text the user has clicked, using JTextPane.viewToModel(Point p). This is probably a needlessly complex way of doing lists, however.

DNA
  • 42,007
  • 12
  • 107
  • 146
  • Thanks for the replies! Firstly, sorry for my mistake. I actually ment rows! I'm currently using ArrayList to list videos in a textarea(looking to change to a suitable one) and create a list in a jtextpane. If i use JList.getSelectedItem() or JTextPane.viewToModel(Point p) where should I add it(to the constructor or to actionperformed?), since there is no button/mouse listener associated? A sample would be nice:-) – alien45 Feb 25 '12 at 00:56
  • When you click a button to put a video into the playlist, the mouselistener attached to the button can call getSelectedItem() on the JList, to see what's selected (if anything). Since this is your coursework I think you should have a go yourself, and ask a new question if you get stuck. – DNA Feb 25 '12 at 01:01
  • I'll try again when in morning and probably will get back again:-) Anyway, thanks for your help! – alien45 Feb 25 '12 at 01:18
  • @StanislavL thank you soo much! I can just click on the row of the video and directly add to the playlist without pressing anything else! Next I'll try to do is highlight/change the background color of the row when clicked and probably adding multiple videos at once. Thanks again:-) – alien45 Feb 26 '12 at 02:10
2

You can use this to get row/col position in JTextPane http://java-sl.com/tip_row_column.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98