• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Positioning slider relative to word in JtextPane.

 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am hoping to get some advice on a problem I am trying to tackle. In my application, I have a single line textpane inside a scrollpane. The user will input some text (that could be longer than the length of the textpane - hence the scrollpane!). What I eventually want to achieve is that if the user clicks on any of the words in the text, I would like to have a slider popup right next to the word. So essentially, I need to find the exact position of the word and put the slider there.

Please give me some tips to achieve this.

thanks
 
Rob Camick
Ranch Hand
Posts: 2536
9
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The MouseEvent will have the point where the mouse was clicked. So you can just position your slider relative to the mouse point.

Or if you want to position the slider to the start of the word then you will also need to do the following:

1) You can then use the convertViewToModel(...) method of the text component to get the offset where the mouse was clicked.

2) Then you can use the Utilities.getWordStart(...) method to get the offset of the first character in the clicked word.

3) Then you can use the convertModelToView(...) method to get the location of the first character in the text pane.

4) Now you can position your slider relative to the first character.
 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The MouseEvent will have the point where the mouse was clicked. So you can just position your slider relative to the mouse point.

Or if you want to position the slider to the start of the word then you will also need to do the following:

1) You can then use the convertViewToModel(...) method of the text component to get the offset where the mouse was clicked.

2) Then you can use the Utilities.getWordStart(...) method to get the offset of the first character in the clicked word.

3) Then you can use the convertModelToView(...) method to get the location of the first character in the text pane.

4) Now you can position your slider relative to the first character.


Awesome Rob, Thanks. I am not sure which technique I'll use (position slider at the mouse click or at the start of the word) I'll try both. Thanks.
 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what is going on. I am using the location of the mouse using the mouseevent. I am trying to place the slider at the location of the mouse. But when i pass the mouse location information to the jSlider.setLocation(), the x position is correct, but the y position of the slider is way below the position of the mouse. Any clues?

thanks
 
Rob Camick
Ranch Hand
Posts: 2536
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your SSCCE that demonstrate the problem and maybe someone will take a closer look.
 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have created an SSCCE. I use netbeans to develop my swing application. I have tried to remove most of the automatically generated code which I saw as unnecessary. Hopefully you should see this behaviour when you run the file. Once you run the file click anywhere in the textpane, and you should see the slider move to the same x position as the mouse, but is lower below.



 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You took out too much. The controls aren't added to the frame anymore.

The call to jSlider1.setLocation(evt.getLocationOnScreen()); will not do what you want though. The layout manager (a GroupLayout) will ignore the position you set, and set it where it thinks it belongs. You will get this behaviour with just about every layout manager.
 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have set the layout manager to null, and I think the code below should be complete in order to view the example.

 
Rob Camick
Ranch Hand
Posts: 2536
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a null layout for you main components.

Use a Glass Pane with a null layout to hold the slider so it paints on top of other components. For example:

 
Poorav Chaudhari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Don't use a null layout for you main components.

Use a Glass Pane with a null layout to hold the slider so it paints on top of other components. For example:




Thanks Rob for your suggestion here. It took me a while to get back to this. I worked with Glasspane and it resolved my issue. I am having some issues with some additional behaviour i am trying to implement. but I am working throught it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic