File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Positioning slider relative to word in JtextPane. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Positioning slider relative to word in JtextPane. " Watch "Positioning slider relative to word in JtextPane. " New topic
Author

Positioning slider relative to word in JtextPane.

Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
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

Poorav Chaudhari
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2157
    
    7
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

Joined: Sep 17, 2002
Posts: 50
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

Joined: Sep 17, 2002
Posts: 50
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

Joined: Jun 13, 2009
Posts: 2157
    
    7
Post your SSCCE that demonstrate the problem and maybe someone will take a closer look.
Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
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

Joined: Jun 13, 2009
Posts: 2157
    
    7
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

Joined: Sep 17, 2002
Posts: 50
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.
 
wood burning stoves
 
subject: Positioning slider relative to word in JtextPane.
 
Similar Threads
problem: JTextPane doesn't wrap
Adding an Image to a JTextPane
control JTextPane
having help menuitem
Word wrap and other issues with JTextPane