I want to highlight different sections of text when mouse is hovered over them. I'm currently using styleddocument with jTextPane. Can somebody please help me in defining style so that individual strings/paragraphs can be highlighted when mouseover occurs?
Thanks,
You can define a custom highlighter to show necessary colors. Add a MouseListener to the jTextPane. You can get mouse coordinates and use viewToModel() method of jTextPane to detect offset in the Document and process whether current section should be highlighted or not.
Related
I am creating a text editor using JTextPane that allows the user to bold, color and align the text by highlighting the text then selecting the option to either bold, color or align. After the adjustment is made, the selection goes away, but I want it to stay. Is there a way to get it to stay, or should I manually reselect the text? If so, how can I do that?
The example in Oracle's Java Tutorials does what you need and provides the source code.
I have a JTextPane which has the content type text/plain. I set some texts to that JTextPane and it contain some texts which display URLs. I want to change the mouse pointer when I point the mouse to that text only into the hand pointer.
Is this function achievable?
Note: I have the content of the JTextPane as text/plain. It cannot be changed to text/html
thanx
You could try this:
pane.setCursor(new Cursor(Cursor.HAND_CURSOR));
Where pane is your JTextPane.
Did you read my answer in your posting on Adding tooltips to JTextPane?
Well the concept is the same. You use a MouseListener and convert the mouse point to get the text at the caret position. When you are over a url text then you change the cursor.
The Utilities class might help you access the text at the caret location.
If you need more help then post your SSCCE that shows what you have tried and shows what problems you are having.
I have a JTable displays the event accordingly, I want to do like when mouse over the table cell will pop out a small box show the event details. Something like tooltip how can i do that? is there any component in swing doing that?
Have a read about How to Use Tables: Specifying Tool Tips for Cells.
Use JToolTip and HTML. More info here:
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html
I have a very simple Swing GUI with just a JTetxtArea. I am trying to programmatically select a part of text using:
textArea.select(startSelection,endSelection);
This work. However as soon as I add some other components to the GUI I do not see selection anymore
frame.getContentPane().add(button);
frame.getContentPane().add(textArea);
textArea.select(startSelection,endSelection);
I suspect that during layouting the gui, some event causes the text to be deselected. Am I right? And could anybody suggest a solution?
My goal is to have a program which displays a text, and allows the user to input start and end selection position, and a selection appears between these two position. Thank you.
Text selection only shows when the text component has focus.
Text components also support "highlighting" by using the getHighlighter().addHighlight() method. In this case the highlighting remains whether the component has focus or not.
If you need more help post your SSCCE that demonstrates the problem.
If what you really want is just a selection, not highlight (which behaves differently), you can use JTextComponent.getCaret().setSelectionVisible(true).
I'm trying to make it so when I tab to some text fields on in my JFrame the data in the text field will be highlighted. I thought I had done this in the properties before but I am not seeing the option now. Does anyone know how to do this?
You could use a FocusListener to work out when your field has focus (tutorial here).
Decide whether you want to select the text in the field, or whether you just want to change the background color. It's not quite clear what you mean by highlight.